views:

1558

answers:

7

I'm looking for a very simple algorithm for computing the polygon intersection/clipping. That is, given polygons P, Q, I wish to find polygon T which is contained in P and in Q, and I wish T to be maximal among all possible polygons.

I don't mind the run time (I have a few very small polygons), I can also afford getting an approximation of the polygons' intersection (that is, a polygon with less points, but which is still contained in the polygons' intersection).

But it is really important for me that the algorithm will be simple (cheaper testing) and preferably short (less code).

edit: please note, I wish to obtain a polygon which represent the intersection. I don't need only a boolean answer to the question of whether the two polygons intersect.

A: 

This can be a huge approximation depending on your polygons, but here's one :

  • Compute the center of mass for each polygon.
  • Compute the min or max or average distance from each point of the polygon to the center of mass.
  • If C1C2 (where C1/2 is the center of the first/second polygon) >= D1 + D2 (where D1/2 is the distance you computed for first/second polygon) then the two polygons "intersect".

Though, this should be very efficient as any transformation to the polygon applies in the very same way to the center of mass and the center-node distances can be computed only once.

Sylvestre Equy
And how to I get a polygon representing the intersection area?
Elazar Leibovich
+1  A: 

You could use a Polygon Clipping algorithm to find the intersection between two polygons. However these tend to be complicated algorithms when all of the edge cases are taken into account.

One implementation of polygon clipping that you can use your favorite search engine to look for is Weiler-Atherton. wikipedia article on Weiler-Atherton

Alan Murta has a complete implementation of a polygon clipper GPC.

Edit:

Another approach is to first divide each polygon into a set of triangles, which are easier to deal with. The Two-Ears Theorem by Gary H. Meisters does the trick. This page at McGill does a good job of explaining triangle subdivision.

Doug Ferguson
I googled for polygon clipping, and found those results as well. However, please note that these algorithms are meant to be efficient exact and complex. I'm aiming for a slow, possibly approximated algorithm which is SIMPLE.
Elazar Leibovich
I too wish there were a simple to use method. One would imagine that WPF and GDI+ do the sort of clipping that would be generally useful if the more primitive geometry operations were exposed through the API.When one starts simple, the program grows more complex over time as those difficult edge cases are taken into account.
Doug Ferguson
+3  A: 

You have not given us your representation of a polygon. So I am choosing (more like suggesting) one for you :)

Represent each polygon as one big convex polygon, and a list of smaller convex polygons which need to be 'subtracted' from that big convex polygon.

Now given two polygons in that representation, you can compute the intersection as:

Compute intersection of the big convex polygons to form the big polygon of the intersection. Then 'subtract' the intersections of all the smaller ones of both to get a list of subracted polygons.

You get a new polygon following the same representation.

Since convex polygon intersection is easy, this intersection finding should be easy too.

This seems like it should work, but I haven't given it more deeper thought as regards to correctness/time/space complexity.

Moron
Wow! This is *just* what I had in mind, but: (1) the polygons are represented as series of CW segments, and converting it to convex-convex is nontrivial. (2) After substructing the first convex, I get a nonconvex shape I need to handle, and I'm not sure that substructing a convex from a polygon is much easier than finding the intersection between two polygons...
Elazar Leibovich
@Elazar: To convert line segement representation to Convex - Convex, you can do the following: 1) Form the convex hull. 2) For each side of the convex hull, if it is not inside, you can find a non-convex polygon you need to subtract. You can then 'triangulate' this non-convex polygon to get a union of convex shapes. As to your point 2): you don't actually have to do any actual subtracting if you work with that representation. I suppose for the convex hull + 'triangulation', there will be packages to do that already.
Moron
A: 

Here's a simple-and-stupid approach: on input, discretize your polygons into a bitmap. To intersect, AND the bitmaps together. To produce output polygons, trace out the jaggy borders of the bitmap and smooth the jaggies using a polygon-approximation algorithm. (I don't remember if that link gives the most suitable algorithms, it's just the first Google hit. You might check out one of the tools out there to convert bitmap images to vector representations. Maybe you could call on them without reimplementing the algorithm?)

The most complex part would be tracing out the borders, I think.

Back in the early 90s I faced something like this problem at work, by the way. I muffed it: I came up with a (completely different) algorithm that would work on real-number coordinates, but seemed to run into a completely unfixable plethora of degenerate cases in the face of the realities of floating-point (and noisy input). Perhaps with the help of the internet I'd have done better!

Darius Bacon
Tracing the borders might be easier if you realize that each vertex will either be the vertex of one of the polygons, or an intersection of a line segment from each of them.
Mark Ransom
+1  A: 

Here's an approach based on triangulation that is pretty straightforward to implement and can be made to run in O(N2).

BTW, O(N2) is optimal for this problem. Imagine two polygons shaped like pitchfork blades intersecting at right angles. Each has a number of segments proportional to the number of tines; the number of polygons in the intersection is proportional to the square of the number of tines.

  1. First, triangulate each polygon.

  2. Compare all the triangles from P pairwise with all the triangles from Q to detect intersections. Any pair of intersecting triangles can be broken into smaller triangles each of which is in P, in Q, or in the intersection. (Whatever you used in step 1 can be reused to help with this.) Only keep triangles that are in the intersection.

  3. Compute the neighbors of each triangle, by comparing them pairwise, and build an adjacency graph. This graph will contain one connected subgraph for each polygon in the intersection of P and Q.

  4. For each such subgraph, pick a triangle, walk to the edge, and then walk around the edge, producing the segments bounding the corresponding output polygon.

Eric
I think O(n^2) is NOT optimal for the problem. O(nlgn+k) where k is the number of points in the resulting polygon is optimal for the problem.
Elazar Leibovich
A: 

If you use C++, and don't want to create the algorithm yourself, you can use Boost.Geometry. It uses an adapted version of the Weiler-Atherton algorithm mentioned above.

Barend
+2  A: 

I understand the original poster was looking for a simple solution, but unfortunately there really is no simple solution.

Nevertheless, I've recently created an open-source freeware clipping library (written in both Delphi and C++) which clips all kinds of polygons (including self-intersecting ones). This library is pretty simple to use: http://sourceforge.net/projects/polyclipping/ .

Angus Johnson
I came to this unfortunate conclusion myself not long ago. Every solution is agonizingly complex. Thanks for the library!
Elazar Leibovich
Perhaps I should also mention that my Clipper library also compares very favorably with other clipping libraries: http://angusj.com/delphi/clipper.php#features
Angus Johnson