Skip to main content
reverted previous changes; I was correct the first time
Source Link
Michael Todd
  • 2k
  • 4
  • 20
  • 32

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;
    }
}

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;
    }
}

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 - (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;
    }
}
incorrect formula changed
Source Link
Michael Todd
  • 2k
  • 4
  • 20
  • 32

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 it'sa 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;
    }
}

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 it's 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 - (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;
    }
}

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;
    }
}
spelling
Source Link
Michael Todd
  • 2k
  • 4
  • 20
  • 32

This hasn't been tested but should work (if you're looking forTesting if the point is on the polyline

The following code will take a polyline, a point that matchesyou would like to test if it falls on one of the lines that make up the polyline, and a vertex intolerance value used to say how close you need the Polyline;point to be to the line for it to be coincident with it. The tolerance value should probably not be zero due to rounding issues introduced via computer math.

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 it's won't work ify = mx + b form. The code then subtracts the y value from the passed point is just "on" one ofto the y value computed by using the point's x value in the line segments that make upformula. If the Polyline):resultant value is less than the tolerance, the point is said to be on the line.

private bool PointLaysWithinLinePointLaysOnLine(IPolyline currentPath, IPoint iPoint, double tolerance)
{
    IPointCollection points = currentPath as IPointCollection;

    for (int pos = 0;1; pos < points.PointCount; pos++)
    {
        ifSlopeAndOffset so = new SlopeAndOffset(points.Point[pos]Points[pos].X, == 
 iPoint                                               points.Points[pos].Y,
                                                points.Points[pos - 1].X, &&
                                                points.Point[pos]Points[pos - 1].Y);
 ==                                               
        if (Math.Abs(iPoint.Y - (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;
    }
}

ItTesting if the point is one of the vertices of the polyline

This code loops through each point in the polyline and compares it to the passed point. If the points match, it returns true. If it gets through the entire polyline and doesn't find the point, it returns false.

private bool PointLaysWithinLine(IPolyline currentPath, IPoint iPoint)
{
    IPointCollection points = currentPath as IPointCollection;

    for (int pos = 0; pos < points.PointCount; pos++)
    {
        if (points.Point[pos].X == iPoint.X &&
            points.Point[pos].Y == iPoint.Y)
        {
            return true;
        }
    }

    return false;
}

This hasn't been tested but should work (if you're looking for a point that matches a vertex in the Polyline; it won't work if the point is just "on" one of the line segments that make up the Polyline):

private bool PointLaysWithinLine(IPolyline currentPath, IPoint iPoint)
{
    IPointCollection points = currentPath as IPointCollection;

    for (int pos = 0; pos < points.PointCount; pos++)
    {
        if (points.Point[pos].X == iPoint.X &&
            points.Point[pos].Y == iPoint.Y)
        {
            return true;
        }
    }

    return false;
}

It loops through each point in the polyline and compares it to the passed point. If the points match, it returns true. If it gets through the entire polyline and doesn't find the point, it returns false.

Testing if the point is on the polyline

The following code will take a polyline, a point that you would like to test if it falls on one of the lines that make up the polyline, and a tolerance value used to say how close you need the point to be to the line for it to be coincident with it. The tolerance value should probably not be zero due to rounding issues introduced via computer math.

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 it's 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 - (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;
    }
}

Testing if the point is one of the vertices of the polyline

This code loops through each point in the polyline and compares it to the passed point. If the points match, it returns true. If it gets through the entire polyline and doesn't find the point, it returns false.

private bool PointLaysWithinLine(IPolyline currentPath, IPoint iPoint)
{
    IPointCollection points = currentPath as IPointCollection;

    for (int pos = 0; pos < points.PointCount; pos++)
    {
        if (points.Point[pos].X == iPoint.X &&
            points.Point[pos].Y == iPoint.Y)
        {
            return true;
        }
    }

    return false;
}
better details
Source Link
Michael Todd
  • 2k
  • 4
  • 20
  • 32
Loading
Source Link
Michael Todd
  • 2k
  • 4
  • 20
  • 32
Loading