The idea here is to take each line segment that makes up the polyline and compute the slope and offset, i.e. change the line to a y -= mx =+ b
form. The code then subtracts the y value from the passed point to the y value computed by using the point's x value in the line formula. If the resultant value is less than the tolerance, the point is said to be on the line.
private bool PointLaysOnLine(IPolyline currentPath, IPoint iPoint, double tolerance)
{
IPointCollection points = currentPath as IPointCollection;
for (int pos = 1; pos < points.PointCount; pos++)
{
SlopeAndOffset so = new SlopeAndOffset(points.Points[pos].X,
points.Points[pos].Y,
points.Points[pos - 1].X,
points.Points[pos - 1].Y);
if (Math.Abs(iPoint.Y - (-1 * so.Slope * iPoint.X + so.B)) <
Math.Abs(tolerance))
{
return true;
}
}
return false;
}
public class SlopeAndOffset
{
public double Slope { get; private set; }
public double B { get; private set; }
SlopeAndOffset(double x1, double y1, double x2, double y2)
{
Slope = (y2 - y1) / (x2 - x1);
B = y1 - Slope * x1;
}
}