c# - Algorithm for points inside a polyline inexplicably fails (AutoCAD) -


i'm using .net api autocad, have algorithm (which did not write) determining if point lies within polygon (straight lines only).

i have been testing command on same 51 polygons repeatedly. 99% work perfectly. every once in while fail on 1 or more of polygons, returning false on 2000 points creating inside bounding box of polyline. have seen fail when polyline isa simple rectangle , of points lie distributed in grid within polyline. should have returned true on 2000 times in case. never fail 1 of points, fail of them. have confirmed points being correctly created expect them , vertices of polygon expect them be. when fails, last angle variable last point @ double pi.

i not doing multi-threading. possibly 'funny' thing doing com interop excel. happening after transaction has been committed part algorithm, , sure cleaning com objects. have not been able reproduce failure without com interop part don't think i've tested enough yet have enough absence of evidence.

any ideas wrong?

bool isinsidepolygon(polyline polygon, point3d pt)     {         int n = polygon.numberofvertices;         double angle = 0;         point pt1, pt2;          (int = 0; < 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);     } 

some ideas:

  • floating point comparison have done using tolerence, might cause kind of arbitrary results in case point lies on polyline (same remark point3d, must compared using tolerence)

  • maybe last point of polyline @ same location first one, in case, angle cannot computed (perhaps why double pi value last point). should test first , last points equals.

  • i'm not sure algorithm works regardless if polyline clockwise or counterclockwise (i think yes)

  • you may convert polyline region , rely on region point containment method


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -