Skip to main content

Detecting Polygon Closures

·203 words·1 min

Some more debug art.

I wrote a ShapeGraph class that ensures the graph is planar. Then updated PolygonFinder to use its nodes and edges as vertices and line segments. The above was generated by clicking the mouse all over the place and filling any detected polygons with a random colour.

For my use case, once a polygon has formed I want to prevent further vertices from being added inside that polygon. Originally I thought I’d do some fancy half-edge tracking but I guess I can just check whether the click overlaps an existing polygon.

…uhh wrong again!

Had to change the implementation a lot to prevent edges from being drawn across existing polygons.

But it’s working. Here’s a simpler example:

But I’m testing all kinds of situations like starting from a point at the edge of the triangle or leaving from a point at the edge of the triangle.

Here’s another case that breaks:

If I have a dangling edge in the middle of a shape (it has to be placed before the polygon is closed) then running a new segment through the endpoint is fine (degree == 1) but running it through other points gets incorrectly interpreted as leaving the polygon.