views:

284

answers:

3

I'm looking for a packing algorithm which will reduce a regular polygon into rectangles and right triangles. The algorithm should attempt to use as few such shapes as possible and should be relatively easy to implement (given the difficulty of the challenge).

If possible, the answer to this question should explain the general heuristics used in the suggested algorithm.

A: 

It's not specifically rectangles + right triangles, but a good research point for looking into tesselating polygons is Voronoi Diagrams and Delaunay Triangulations and here and here.

In fact, if "just right triangles" is good enough, these are guaranteed to triangulate for you, and you can always split any triangle into two right triangles, if you really need those. Or you can chop off "tips" of triangles to make more right triangles and some rectangles out of the right-triangles.

You can also try ear-clipping, either by sweeping radially, if you know you have fairly regular polygons, or by "clipping the biggest convex chunk" off. Then, split each remaining triangle into two to create right triangles.

polygon

You could try to make less breaks by sweeping one way and then the other to make a trapezoid and split it differently, but you then have to do a check to make sure that your sweep-line hasn't crossed another line someplace. You can always ear-clip, even with something practically fractal.

However, this sometimes creates very slim triangles. You can perform heuristics, like "take the biggest", instead of clipping continuously along the edge, but that takes more time, approaching O(n^2). Delaunay/Vornoi will do it more quickly in most cases, with less slim triangles.

eruciform
Absolutely must be rectangles and right triangles. I know it sounds strange, but it's a user requirement.
Steve
updated. you can turn any triangle into right triangles and rectangles pretty easily.
eruciform
Delaunay triangulations aren't ideal here because they introduce additional, unnecessary points in the middle. Each of those internal points can be "dragged" to an adjacent polygon vertex and you are left with a smaller set of triangles.
j_random_hacker
update: you can also try ear-clipping...
eruciform
A: 

You can try "cuting out" the largest rectangle that can fit in the polygon, leaving behind some leftovers. Keep repeating the cutting out of rectangles on the leftovers, until you end up with triangular pieces. Then, you can split them into two right triangles if necessary. I do not know if this will always yield solutions that will give you the least amount rectangles and right triangles.

In silico
I think this is not a good idea either: eating away as much area as possible with the first rectangle is not going to leave you with the "easiest" shape afterwards.
Mau
it wasn't clear what his requirements were. least total shapes or most area in least shapes...
eruciform
For regular polygons though, it would still give you "nice" shapes. Again, I don't know if this will give you the "most optimal" solutions.
In silico
+6  A: 

I think the answer is fairly simple for regular polygons.

Find an axis of symmetry, and draw a line between each vertex and its mirror. This divides the polygon into trapezoids. Each trapezoid can be turned into a rectangle and two right triangles.

http://content.screencast.com/users/Tom/folders/Jing/media/04cb9283-7fc0-4ccd-99ad-a4e056f81b23/2010-06-21_0056.png

Tom Sirgedas
You could gain some extra credit by proving that this produces the minimum number of tiles. :)
Svante
I now believe this is actually optimal :-)
Mau
No. It is not always optimal. Take for example a regular hexagon with two sides parallel to the x=0 axis. Then this algorithm generates 2 rectangles and 4 right triangles, but 1 rectangle and 4 triangles would be enough. Nonetheless the solution is probably good enough and easy to implement.
abc
This does generate 1 rectangle + 2 triangles if you are careful enough to 'split' the polygon (generate a rectangle) along the longest chord first.
Mau
Nice! Going to wait just a bit to see if anyone else weighs in, but this looks great! PS I plan on asking the same question but for irregular polygons with a 200 point bounty.
Steve
be careful with regular but odd-numbered-vertex polygons, the algorithm changes slightly. however, this will generally provide the most rectangles with large area. it will also create small triangles on the edges, if that matters for you. let me know if you ned one for irregulars - my post was more directed at that...
eruciform
@Mau The OP specifies that the triangles need to have a right angle (i.e. 90 degrees). Hence you can't split a regular hexagon into one rectangle and 2 right triangles. There are other cases where the algorithm comes up with one extra rectangle, e.g. a rectangular octagon aligned such that the corners are at at an angle 0, 45, 90 etc away from the center. Again, this may not matter if simplicity of the solution is important.
abc
@abc: Correct, I meant 4 triangles.
Mau
One nice property is that this algo returns shapes that are axis aligned. I'm not sure this is a requirement, but often it is a helpful property.
abc
One simple case where this algorithm is not optimal is for a right triangle lying on its hypotenuse. But I like its simplicity, +1.
j_random_hacker