public static float PointToSegment(Point p1, Segment s) { // this is from the web - don't ask exactly how it works! PointF p=new PointF(p1.X, p1.Y); Vector v = new Vector(s.P1, s.P0); Vector w = new Vector(p, s.P0); double c1 = w * v; if ( c1 <= 0 ) // this handles the case when the point is // nearer the first end point than a point on the line return Distance(p, s.P0); double c2 = v * v; if ( c2 <= c1 ) // this handles the case when the point is // nearer the second end point than a point on the line return Distance(p, s.P1); double b = c1 / c2; PointF Pb= s.P0 + (v * b); return Distance(p, Pb); }
public static float Distance(PointF p, PointF pb) { Vector v=new Vector(p, pb); return (float) Math.Sqrt(v*v); }