/// <summary> /// Finds the distance from the triangle to a line segment /// </summary> public double DistanceFrom(LineSegment segment) { if (Intersects(segment)) { return(0); } var nearestPointInPlane = ContainingPlane.NearestPoint(segment); if (IsInside(nearestPointInPlane)) { return(ContainingPlane.Distance(segment)); } var line1 = new LineSegment(Points[0], Points[1]); var line2 = new LineSegment(Points[1], Points[2]); var line3 = new LineSegment(Points[2], Points[0]); double p1 = segment.DistanceSquared(line1); double p2 = segment.DistanceSquared(line2); double p3 = segment.DistanceSquared(line3); double minDistance2 = UtilityFunctions.Min(p1, p2, p3); return(Math.Sqrt(minDistance2)); }
/// <summary> /// Whether a given line segment intersects the triangle /// </summary> public bool Intersects(LineSegment segment) { if (!ContainingPlane.Intersects(segment)) { return(false); } var point = ContainingPlane.Intersection(segment.UnderlyingLine); return(IsInside(point)); }
/// <summary> /// Returns whether a point known to be in the containing plane lies inside the triangle /// </summary> private bool IsInside(Point point) { var rotatedPoint = ContainingPlane.TransformTo2D(point); return(RotatedTriangle.Inside(rotatedPoint)); }