Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static bool isPointInside(AcDbPolyline &polyline, AcGePoint2d &point)
- {
- // ALGORITHM: Build a horizontal line starting at the
- // left side of the polyline and ending in the point
- // to be checked. Then count the number of intersections.
- // An even count means the point is contained within the
- // polyline, an odd therefore indicates that the point is
- // outside of the polygone.
- AcDbExtents extents;
- polyline.getGeomExtents(extents);
- AcGePoint3d min = extents.minPoint();
- // Do a fast check first. If the point is already outside the bouding area
- // we can stop right here.
- if (point.x < min.x || point.x > extents.maxPoint().x
- || point.y < min.y || point.y > extents.maxPoint().x)
- return false;
- // This is our intersection line, we need to cound the intersections for this
- AcGeLine2d line(AcGePoint2d(min.x, point.y), point);
- // Iterate over all points and build lines to check intersection with
- Adesk::uint intersectionCount = 0;
- for (Adesk::uint i = 0; i < polyline.numVerts() - 1; ++i)
- {
- // check with line from pnts[i] to pnts[i+1]
- AcGeLineSeg2d test;
- polyline.getLineSegAt(i, test);
- AcGePoint2d intersection;
- if (test.intersectWith(line, intersection))
- // We need to test here again if reported intersection if correct
- // as of the method tends to ignore the line boundaries.
- // Checking only the x-axis is suitable here.
- if ((min.x <= intersection.x && intersection.x <= point.x) ||
- point.x <= intersection.x && intersection.x <= min.x)
- intersectionCount++;
- }
- // Even intersectionCount here means the point is intersecting
- return intersectionCount % 2 == 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement