tags:

views:

97

answers:

2

In Illustrator, you can drag a rectangle and it will select all objects in it. It does beyond a bounding box test since it ensures its touching an actual part of the polygon. How does it efficiently do this then? (A C or C++ implementation would be preferable)

Thanks

+3  A: 

If you want to check if any part of polygon P is within a rectangle R, then you can do this:

  • If any vertex of P is within R, then return TRUE;
  • If any vertex of R is within P, then return TRUE;
  • If any edge of P (line between adjacent vertexes) intersects an edge of R, then return TRUE.
  • Otherwise, return FALSE.
caf
How would this look like as a function? Thanks
Milo
@Jex: Well, the primitives you're going to need are a point-lies-within-bounding-box test, and a line-segment-intersection-test (the first is trivial and the second is easy to search for). Testing if a point lies within a polygon is the hardest part - one way to do it is test if a line from the point to the bounding box of the polygon crosses an edge of the polygon an even or odd number of times.
caf
I have the point in poly one and point in rect one, I guess all thats left is to find the intersection one. Thanks
Milo
Jex, so you to select when there is ANY or FULL intersection? What about a spline, the ends of which do not intersect, but the middle part does? This would correspond to the third bulleted item by caf.
Hamish Grubijan
A: 

You could also just construct the intersection of the rectangle with the object (a function which programs like Illustrator already have for many other purposes) and check that it's non-empty. More efficient algorithms are available (see caf's answer), but mine has the one advantage that it requires no additional code.

R..