public float DistanceTo(Point point, out Segment connectingSegment) { double segmentLength = GetLength(); Point normalizedLine = new Point( (Point2.X - Point1.X) / segmentLength, (Point2.Y - Point1.Y) / segmentLength); Point pointVector = new Point(point.X - Point1.X, point.Y - Point1.Y); double length = Point.Dot(pointVector, normalizedLine); connectingSegment.Point1 = point; if (length < 0) { connectingSegment.Point2 = Point1; } else if (length > segmentLength) { connectingSegment.Point2 = Point2; } else { connectingSegment.Point2 = new Point(Point1.X + length * normalizedLine.X, Point1.Y + length * normalizedLine.Y); } return((float)connectingSegment.GetLength()); }
public float DistanceTo(Point point, out Segment connectingSegment) { double segmentLength = GetLength(); Point normalizedLine = new Point( (Point2.X - Point1.X) / segmentLength, (Point2.Y - Point1.Y) / segmentLength); Point pointVector = new Point(point.X - Point1.X, point.Y - Point1.Y); double length = Point.Dot(pointVector, normalizedLine); connectingSegment.Point1 = point; if (length < 0) { connectingSegment.Point2 = Point1; } else if (length > segmentLength) { connectingSegment.Point2 = Point2; } else { connectingSegment.Point2 = new Point(Point1.X + length * normalizedLine.X, Point1.Y + length * normalizedLine.Y); } return (float)connectingSegment.GetLength(); }