/// <summary> /// Find the closet point along this line from a_point /// </summary> /// <param name="a_point"></param> /// <returns></returns> public DVector2 ClosestPoint(DVector2 a_point) { DVector2 delta = v1 - v0; double lengthSquared = delta.SqrMagnitude(); if (lengthSquared.Equals(0f)) { return(v0); } double projection = DVector2.Dot(a_point - v0, delta); double scale = projection / lengthSquared; return(v0 + delta * scale); }
/// <summary> /// Calculates the center of a circle that intersects abc. Returns false if abc are colinear, or if the /// arc is diverging. Divergence is determine by whether the points are ordered clockwise or ccw around /// the center of the circle /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <param name="d"></param> /// <returns></returns> bool Circumcenter(DVector2 a, DVector2 b, DVector2 c, out DVector2 d) { DVector2 ba = b - a; DVector2 ca = c - a; double baLength = ba.SqrMagnitude(); double caLength = ca.SqrMagnitude(); double denominator = 2d * (ba.x * ca.y - ba.y * ca.x); if (denominator <= 0d) { d = b; return(false); } d.x = a.x + (ca.y * baLength - ba.y * caLength) / denominator; d.y = a.y + (ba.x * caLength - ca.x * baLength) / denominator; return(true); }