Can anyone tell me how I would be able to test if two polygons are intersecting? The polygons can be concave, and each point is at an integer coordinate. Any help would be most appreciated.
A simple minded approach (but slow)
which works for all polygons (convex or not) is to try the
following: call the first polygon P and the second polygon Q,
call the vertices of the first polygon v1 to
vn so that v1 =vn and they are
listed counterclockwise and the vertices of the second polygon
w1 to wm so that w1
=wm and they are listed counterclockwise. First of
all, you want to check if P is entirely contained in Q. If you
test whether v1 is in Q this is enough. If
v1 is in Q then they are intersecting, if not then P
is not entirely contained in Q. If P is not entirely contained in
Q then one of the edges of P must intersect one of the edges of
Q. So you just test each edge vi vi+1 of P
against each edge wj wj+1 of Q. Using this
method there are mn tests to do, which is not particularly
efficient. To speed it up, you can do a bounding box test to
start with. i.e. you draw a minimal box around P, a minimal box
around Q and test if they intersect. If they don't, then clearly
P and Q don't intersect. If you're doing ray tracing or collision
detection for a game then you'll want to do this, because the
number of poly-poly intersections will be small but the number of
poly-poly tests will be large, and bounding box tests are very
quick.
Do you know how to test if a point is in a polygon and how to
test if a line segment intersects another line segment? If not,
post again and I'll explain.
Thanks for the great answer. Everything I'd looked at was just using points, but now I see that using edges will do exactly what I want - speed isn't an issue either, it doesn't need to be done at run time. I haven't done line segment collision, and I'm sure there's plenty of stuff on the net already, but if you have time I'd love to see your answer.