Fighting with computers

Computers are not always friendly.

Tuesday, May 08, 2012

Union of polygons

I should have settled with a graphics library of some sort, but I did not. So now I have all sorts of problems some of these libraries have one function call away. Unfortunately many library creators do expect to be paid if you do a commercial product using their code. I find this to be fair, but not knowing how much or for how long just stir me away.

My current problem is a simple one: I have collection of 2D polygons and I want to obtain the union of all of them. Sometimes it is a bigger polygon, sometimes a set of disconnected areas. It turns out that non-convex polygons make the solution harder than I expected. As I am writing code in Processing (Java) I could used JavaGeom, but it is based in GPC library. I could use JTS library, but I was lost in the process of learning how to integrate it with my code, but this was before I learned the problem took me longer to get it coded than learning another library. You have been warned!

Update: If you can settle with integer coordinates, you can use java.awt.Polygon objects and turn them into java.awt.geom.Area objects that accept CSG primitives (union, difference, xor and intersection). It just works.

Update2: Actually, it can all be solved with Java2D framework, even for float values for coordinates. Polygon class is only for integer coordinates, but the GenericPath class accepts float coordinate values, so you can use it for defining Polygon-like contours that can later be united if turned into Area objects. The other detail is that union is not called this way in Area class but "add".

1 Comments:

  • At 3:31 pm, Anonymous Formula for Volume of a Sphere said…

    I think at the time math is very difficult subject, parents should help kids in solving math problems. and about 12 sided I want to share a simple definition as-12 sided polygon is called as dodecagon, This is one type of regular polygons, it may be regular or irregular. It’s a 12 sided closed figure.

     

Post a Comment

<< Home