Detecting an Intersection between Simple Shapes

8.7k Views Asked by At

I have a circle, ellipse, square or a rectangle, and I want to determine if it intersects a given triangle.

I am looking for the easiest way to determine if there exists a geometrical intersection between them.

I am only interested in a true/false result. (One point counts as an intersection, more than one point is also intersection.)

Is there such a formula, and, if so, what is it?

Edit: I need to account for any point within as well, not just the lines around each shape, as in two edge cases where the triangle is either completely within the other shape or the shape is within the triangle. In both cases there are no intersections of sides.

1

There are 1 best solutions below

2
On BEST ANSWER

There are a few different approaches.

If the answer doesn't need to be very accurate, you can just "scan convert" each shape into a pixel array (in computer graphics terms). If there is an intersection, you will find that some pixel has been "painted" twice. You can do this sort of thing very very quickly on modern hardware. You don't need to limit yourself to the kind of resolution/accuracy found on typical graphics screens -- your pixel array can be as large as you like, as long as you have enough memory to hold it. The nice thing about this approach is that it's extremely fast and you don't have to write very much special code for each type of shape (just the scan conversion, which is typically very easy).

The other approach is the mathematical one. You compute intersections of curves, basically. This is much more difficult, but the answers will be much more accurate. You'll have to treat each problem individually, so you will end up with a large number of functions, one for each pair of shape types. From a mathematical point of view, most of the functions are easy to implement; intersecting two ellipses is the only one that requires any special knowledge. But writing reliable software is surprisingly difficult. Even intersecting two line segments can get messy because there are so many special cases that have to be handled (near concidences of one sort or another, for example). A good start would the software on this web site. I highly recommend the associated book, too.

Also, take a look at this answer.

Do I get some sort of "badge" for writing an answer that doesn't have a single mathematical symbol in it?? :-)