I’ve been using the Java Topology Suite (jts) to try and solve the Tangram puzzle. The basic algorithm is the same as I posted previously (it’s item 3 on the list of algorithms). The JTS is much easer to use and the source code is actually readable compared to CGAL. But on the downside JTS does not enforce rigorous geometry like CGAL does. This has some downsides mainly I had to modify the basic algorithm for shape fitting.
The main issue is that when trying to fit for example a triangle,
, inside another polygon say
. We can have roundoff errors such as in the following image.

One slightly add-hoc way to solve this issue is to multiply
by a scaling factor
where epsilon is a small constant. Then when subtracting the new triangle
from
we won’t have the issue of a small corner of
being left over. But it does mean that we subtract slightly too much area from
. Which could cause an issue when we try to fit say another triangle,
, inside of
(e.g.
won’t fit). To ensure
fits what we can do is create a buffer of size #pieces
around
before testing if
is inside of
. Please note that #pieces is the number of for instance triangles we’re trying to fit inside of
(so in this case #pieces = 2 since we’re trying to fit
and
inside of
). With the addition of the buffer even if
is slightly too small the union of
and its buffer will be large enough to cover
. I’ve had pretty good luck using this method with the JTS library.
You can see my code at the GitHub 2dfit site.
But a new problem is that solving the tangram puzzle takes a really long time since the algorithm goes through a large number of possible solutions before finding the correct one. Even after using the heuristic of fitting the largest shapes first the algorithm still takes a lot of time (in fact I’m still waiting for it to complete while writing this post). So I’ve been thinking of possible ways to speed up the algorithm. Below I’ve listed an algorithm that might be of some help in this.
Given two simple polygons called the silhouette and the puzzle piece. What we want is the algorithm to have two possible outcomes (1) it says “does not fit” in which case the puzzle piece cannot possible fit inside the silhouette and (2) “don’t know” in which case the puzzle piece could possible fit inside the silhouette but it’s also possible that it does not fit inside the silhouette.
In the below image I’ve shown the puzzle piece,
, which is a square and the silhouette,
, which is a simple polygon.

One way to determine that the puzzle piece, p, won’t fit is to start by constructing a bounding circle around it. As in the following figure.

What we do is calculate the %area of the circle filled by the puzzle piece (in this case a square). I’ve done the calculation and it’s
Now suppose we wanted to test if the
square would not fit in the silhouette,
. Now also suppose that we try putting the bounding circle that we used for the square on the silhouette,
as in the following figure.

As is obvious the area of the bounding circle filled by the silhouette,
is much less than
. Infact if we tried all possible placements of the bounding circle on the silhouette,
we would find that the %area of the bounding circle filled by the silhouette is always less than
This implies that the square cannot fit inside the silhouette,
. Now we need to determine what placements to use of the bounding circle to let us conclude that there is no possible placement that would give an %area of
filled by the silhouette
.