geometrycomputational-geometrygodotgodot4geometry-surface

Calculate the outline of an abstract shape with holes


I want to code a room builder that creates a room out of selected cells. For example, here's a shape I drew:

enter image description here

To create the room, I believe I'd need to calculate the outline of this shape, including the hole in the middle. (I could generate a mesh for each square but that sounds like it'd be way too resource consuming for my case).

Taking the vertices of each cell, is it possible to calculate an outline that encompasses every vertex? Not exactly a convex hull, since the room would lose it's square properties. I've looked into the graham scan and jarvis march algorithms and they both run into that problem.


Solution

  • Sure. This is similar to, but much simpler than, the problem that the marching squares algorithm solves. Essentially, you'll look at every intersection between four squares, and output parts of the contours based on which of those four squares is filled. For instance, if the upper left, bottom left, and bottom right squares are filled, you'll output a vertical contour segment above the intersection and a horizontal contour segment to the right of the intersection. Then you can match up the segments and optionally combine runs of collinear segments.

    There are other approaches, such as flood filling or walking around the contours, but I think the one I outlined above will be the easiest to implement.