ahugenerd ahugenerd - 9 months ago 63
Java Question

How do I find the overlapping area between two arbitrary polygons

I'm trying to create a method that will take in two arbitrary lists of nodes, for a subject and a clipping polygon, and output either:

a) the area of the overlap

b) a list of nodes for the resulting (clipped) polygon so that I can calculate the area

I've found lots of examples which clip an arbitrary polygon using a rectangular window (which is fairly standard in graphics) but that's not what I need. I understand that it's fairly complex, particularly when you get holes, convex polygons and the like. The only simplifying assumption which I can make is that the arbitrary polygons will not contain any holes.

I'm not at all an expert in this field, so would something like the Sutherland-Hodgman algorithm work? Are there any libraries out there which already do this, or is my best bet to simply implement the algorithm as described in pseudo-code on Wikipedia?

Thanks for the help!

Answer Source

I found that using the JavaGeom library worked very well. It integrates the code from the Java port of GPC (which is no longer available) and thus allows arbitrary polygon operations. Using SimplePolygon2D and Polygon2DUtils.intersection() I was able to get the desired operation.