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); }
/// Calculate how far a given point is from the spiro curve private bool PointNearSegment(Point pt) { if (points == null) Init(); // go around the shape and construct a segment from consecutive // points and see how far the point is from the segment PointF pt1 = new PointF(points[0].X, points[0].Y); for (int n = 1; n < points.Length; n++) { PointF pt2 = new PointF(points[n].X, points[n].Y); Segment s = new Segment(pt1, pt2); if (LineUtil.PointToSegment(pt, s) < 5) return true; pt1 = pt2; } return false; }