The Martinez-Rueda algorithm performs polygon intersections, unions, differences, and XORs by selecting relevant segments during a line sweep, but its output is segments rather than ordered paths. Converting those segments into polygons becomes unreliable when more than two edges meet at a vertex, as can happen in self-intersecting or otherwise improper polygons. The usual contour-following and segment-chaining approaches assume exactly two edges per vertex, which can leave chains unfinished or produce inconsistent results. The proposed replacement computes graph faces, handles improper polygons, gives consistent orientations, and does not depend on the algorithm’s starting point.
The method turns each segment into two oppositely directed half-edges, snaps nearby coordinates to a grid, and sorts half-edges clockwise around each node. A local neighbor rule links edges into cycles, which are then walked to enumerate faces in linear time, assuming modest numbers of edges at each node. Counter-clockwise and clockwise orientation distinguishes exterior from interior faces, but graph geometry alone cannot reveal whether a region is filled or a hole. The method therefore retains the Martinez-Rueda fill annotations on edges and uses them to test whether a face’s inner region is filled, with majority voting across nonvertical edges for robustness. Finally, it offers MERGE_FACES, which tends to form fewer, larger polygons with explicit holes, and SPLIT_FACES, which produces more, smaller regions, sometimes with implied holes.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.