/// <summary> /// Projects a point onto a circle</summary> /// <param name="p">Point to project</param> /// <param name="c">Circle to project onto</param> /// <param name="projection">Projected point</param> /// <returns>True iff projection is well defined</returns> public static bool Project(Vec2F p, CircleF c, ref Vec2F projection) { Vec2F d = Vec2F.Sub(p, c.Center); float length = d.Length; bool wellDefined = false; if (length > 0.000001f * c.Radius) { wellDefined = true; float scale = c.Radius / length; projection = Vec2F.Add(c.Center, Vec2F.Mul(d, scale)); } return(wellDefined); }
/// <summary> /// Constructs the circle containing 3 points</summary> /// <param name="p1">Point 1</param> /// <param name="p2">Point 2</param> /// <param name="p3">Point 3</param> public CircleF(Vec2F p1, Vec2F p2, Vec2F p3) { Vec2F o1 = Vec2F.Add(p1, p2); o1 *= 0.5f; Vec2F o2 = Vec2F.Add(p3, p2); o2 *= 0.5f; Vec2F d1 = Vec2F.Sub(p2, p1); d1 = d1.Perp; Vec2F d2 = Vec2F.Sub(p3, p2); d2 = d2.Perp; double t1 = 0; double t2 = 0; if (Ray2F.Intersect(new Ray2F(o1, d1), new Ray2F(o2, d2), ref t1, ref t2)) { Center = o1 + d1 * (float)t1; Radius = Vec2F.Distance(Center, p1); } else { Center = new Vec2F(float.PositiveInfinity, float.PositiveInfinity); Radius = float.PositiveInfinity; } }