Author Topic: Algorithm for points inside a polyline inexplicably fails  (Read 868 times)

0 Members and 1 Guest are viewing this topic.

svenoaks

• Mosquito
• Posts: 3
Algorithm for points inside a polyline inexplicably fails
« on: May 13, 2013, 06:51:20 pm »

I have an algorithm (which I did not write) for determining if a point lies within a polygon (straight lines only).

I have been testing my command on the same 51 polygons repeatedly. 99% it will work perfectly. Every once in a while it will fail on 1 or more of the polygons, returning false for over 2000 points I am creating inside the bounding box of the polyline. I have seen it fail when the polyline is a simple rectangle and all of the points lie distributed in a grid within the polyline. It should have returned true over 2000 times in that case. It will never fail for just 1 of the points, it will fail all of them. I have confirmed that the points are being correctly created where I expect them to be and that the vertices of the polygon are where I expect them to be. When it fails, the angle variable for the last point is at exactly double PI.

I am not doing any multi-threading. The only possibly 'funny' thing I am doing is COM Interop with Excel. This is happening after the transaction has been committed for the part with this algorithm, and I am sure I am cleaning up all my COM objects. I have not been able to reproduce the failure without the COM Interop part but I don't think I've tested it enough yet to have enough absence of evidence.

Any ideas what could be wrong?

Code - C#: [Select]
`    bool IsInsidePolygon(Polyline polygon, Point3d pt)    {        int n = polygon.NumberOfVertices;        double angle = 0;        Point pt1, pt2;         for (int i = 0; i < n; i++)        {            pt1.X = polygon.GetPoint2dAt(i).X - pt.X;            pt1.Y = polygon.GetPoint2dAt(i).Y - pt.Y;            pt2.X = polygon.GetPoint2dAt((i + 1) % n).X - pt.X;            pt2.Y = polygon.GetPoint2dAt((i + 1) % n).Y - pt.Y;            angle += Angle2D(pt1.X, pt1.Y, pt2.X, pt2.Y);        }         if (Math.Abs(angle) < Math.PI)            return false;        else            return true;    }     public struct Point    {        public double X, Y;    };     public static double Angle2D(double x1, double y1, double x2, double y2)    {        double dtheta, theta1, theta2;         theta1 = Math.Atan2(y1, x1);        theta2 = Math.Atan2(y2, x2);        dtheta = theta2 - theta1;        while (dtheta > Math.PI)            dtheta -= (Math.PI * 2);        while (dtheta < -Math.PI)            dtheta += (Math.PI * 2);        return (dtheta);    }`

TT

• Swamp Rat
• Posts: 826
Re: Algorithm for points inside a polyline inexplicably fails
« Reply #1 on: May 13, 2013, 11:33:41 pm »
Going on the assumption that your algorithm is correct, it could be a result of roundoff errors, depending on the location of the coordinates and the vertices (e.g., extremely large coordinates).

You could also try using Regions and the Brep API to do this, which works for any polyline that can be used to create a simple region:

http://www.theswamp.org/index.php?topic=43572.msg488632#msg488632

svenoaks

• Mosquito
• Posts: 3
Re: Algorithm for points inside a polyline inexplicably fails
« Reply #2 on: May 14, 2013, 07:25:19 am »
Don't think it's round off errors, my numbers are 'normal' sized. I'll definitely give the Brep API a whirl, looks very promising - thanks.

WILL HATCH

• Bull Frog
• Posts: 384
Re: Algorithm for points inside a polyline inexplicably fails
« Reply #3 on: May 14, 2013, 11:51:57 am »
The algorithm is flawed.  See the breakout:
In this case it is the result of bounding dtheta, the removal of the two while statements would fix it.  This is because by bounding that difference we are getting the smaller angle between the two vectors (which in this case are confusingly labeled pt1 and pt2) and because you are dealing with a simple convex polygon the angle will always be in the range [0,pi] or [-pi,0] depending on rotation direction of points, and the sum will always be either -2pi or 2pi

svenoaks

• Mosquito
• Posts: 3
Re: Algorithm for points inside a polyline inexplicably fails
« Reply #4 on: May 14, 2013, 05:30:50 pm »
Wow, thanks Will - I will try to sit down and understand the next breather I get.