Advertisement
bueddl

Autocad intersection test -> polygone vs point

Jun 15th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1.  
  2.     static bool isPointInside(AcDbPolyline &polyline, AcGePoint2d &point)
  3.     {
  4.         // ALGORITHM: Build a horizontal line starting at the
  5.         // left side of the polyline and ending in the point
  6.         // to be checked. Then count the number of intersections.
  7.         // An even count means the point is contained within the
  8.         // polyline, an odd therefore indicates that the point is
  9.         // outside of the polygone.
  10.  
  11.         AcDbExtents extents;
  12.         polyline.getGeomExtents(extents);
  13.  
  14.         AcGePoint3d min = extents.minPoint();
  15.  
  16.         // Do a fast check first. If the point is already outside the bouding area
  17.         // we can stop right here.
  18.         if (point.x < min.x || point.x > extents.maxPoint().x
  19.             || point.y < min.y || point.y > extents.maxPoint().x)
  20.             return false;
  21.        
  22.         // This is our intersection line, we need to cound the intersections for this
  23.         AcGeLine2d line(AcGePoint2d(min.x, point.y), point);
  24.  
  25.         // Iterate over all points and build lines to check intersection with
  26.         Adesk::uint intersectionCount = 0;
  27.         for (Adesk::uint i = 0; i < polyline.numVerts() - 1; ++i)
  28.         {
  29.             // check with line from pnts[i] to pnts[i+1]
  30.             AcGeLineSeg2d test;
  31.             polyline.getLineSegAt(i, test);    
  32.  
  33.             AcGePoint2d intersection;
  34.             if (test.intersectWith(line, intersection))
  35.                 // We need to test here again if reported intersection if correct
  36.                 // as of the method tends to ignore the line boundaries.
  37.                 // Checking only the x-axis is suitable here.
  38.                 if ((min.x <= intersection.x && intersection.x <= point.x) ||
  39.                      point.x <= intersection.x && intersection.x <= min.x)
  40.                     intersectionCount++;
  41.         }
  42.  
  43.         // Even intersectionCount here means the point is intersecting
  44.         return intersectionCount % 2 == 1;
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement