/// <summary> /// Create a circle from three points which lie along its circumference. /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> public Circle3D(Point3D p1, Point3D p2, Point3D p3) { https: //www.physicsforums.com/threads/equation-of-a-circle-through-3-points-in-3d-space.173847/ Vector3D p1p2 = p2 - p1; Vector3D p2p3 = p3 - p2; this.Axis = p1p2.CrossProduct(p2p3).Normalize(); Point3D midPointA = p1 + 0.5 * p1p2; Point3D midPointB = p2 + 0.5 * p2p3; Vector3D directionA = p1p2.CrossProduct(this.Axis); Vector3D directionB = p2p3.CrossProduct(this.Axis); Ray3D bisectorA = new Ray3D(midPointA, directionA); Plane bisectorB = new Plane(midPointB, midPointB + directionB.Normalize(), midPointB + this.Axis); var center = bisectorA.IntersectionWith(bisectorB); if (center == null) { throw new ArgumentException("A circle cannot be created from these points, are they colinear?"); } this.CenterPoint = (Point3D)center; this.Radius = this.CenterPoint.DistanceTo(p1); }
/// <summary> /// Initializes a new instance of the <see cref="Circle3D"/> struct. /// Create a circle from three points which lie along its circumference. /// </summary> /// <param name="p1">The first point on the circle</param> /// <param name="p2">The second point on the circle</param> /// <param name="p3">The third point on the circle</param> /// <returns>A <see cref="Circle3D"/></returns> public static Circle3D FromPoints(Point3D p1, Point3D p2, Point3D p3) { // https://www.physicsforums.com/threads/equation-of-a-circle-through-3-points-in-3d-space.173847/ //// ReSharper disable InconsistentNaming var p1p2 = p2 - p1; var p2p3 = p3 - p2; //// ReSharper restore InconsistentNaming var axis = p1p2.CrossProduct(p2p3).Normalize(); var midPointA = p1 + (0.5 * p1p2); var midPointB = p2 + (0.5 * p2p3); var directionA = p1p2.CrossProduct(axis); var directionB = p2p3.CrossProduct(axis); var bisectorA = new Ray3D(midPointA, directionA); var bisectorB = Plane.FromPoints(midPointB, midPointB + directionB.Normalize(), midPointB + axis); var center = bisectorA.IntersectionWith(bisectorB); if (center == null) { throw new ArgumentException("A circle cannot be created from these points, are they collinear?"); } return(new Circle3D(center.Value, axis, center.Value.DistanceTo(p1))); }