/// <summary> /// Shortest distance between two circles (including interior points) (approximate solution) /// <para> The output points may be not unique in case of intersecting objects.</para> /// </summary> /// <param name="s">Target sphere</param> /// <param name="p1">Closest point on circle</param> /// <param name="p2">Closest point on sphere</param> public double DistanceTo(Sphere s, out Point3d p1, out Point3d p2) { Plane3d p = this.ToPlane; if (s.Center.ProjectionTo(p).BelongsTo(this)) { p1 = s.Center.ProjectionTo(p); if (s.Center == p1) { p2 = s.Center.Translate(s.R * this.Normal.Normalized); } else { p2 = s.Center.Translate(s.R * new Vector3d(s.Center, p1).Normalized); } return(s.DistanceTo(p)); } if (this.Intersects(s)) { Object obj = s.IntersectionWith(p); if (obj.GetType() == typeof(Point3d)) { p1 = (Point3d)obj; p2 = p1; } else { Circle3d c = (Circle3d)obj; p1 = this.Center.Translate(this.R * new Vector3d(this.Center, c.Center).Normalized); p2 = c.Center.Translate(c.R * new Vector3d(c.Center, this.Center).Normalized); } return(0); } double dist = _distance_cicle_to_sphere(this, s, out p1, out p2); return(dist); }
/// <summary> /// Intersection check between circle and sphere /// </summary> public bool Intersects(Sphere s) { if (this.Center.DistanceTo(s.Center) <= this.R + s.R) { Object obj = s.IntersectionWith(this.ToPlane); if (obj != null && obj.GetType() == typeof(Circle3d)) { // Check for circle-circle intersection if (this.IntersectionWith((Circle3d)obj) != null) { return(true); } } else if (obj != null && obj.GetType() == typeof(Point3d)) { return(((Point3d)obj).BelongsTo(this)); } else { return(false); } } return(false); }
/// <summary> /// Get intersection of plane with sphere. /// Returns 'null' (no intersection) or object of type 'Point3d' or 'Circle3d'. /// </summary> public object IntersectionWith(Sphere s) { return(s.IntersectionWith(this)); }