Skip to content

feat: add GeoJSON.Polygon.triangles(maxEdgeDegrees:) and unwrapped() - #27

Open
nighthawk wants to merge 2 commits into
mainfrom
feat/refined-triangles
Open

nighthawk wants to merge 2 commits into
mainfrom
feat/refined-triangles

Conversation

@nighthawk

@nighthawk nighthawk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

triangleIndices() gives a polygon's triangles as they come out of earcut: as few as possible, and often long and thin. That's what you want for filling the polygon on a flat plane, but not for renderers that project each triangle's corners and join them with straight lines, such as a GPU vertex shader or a CoreGraphics path. Those need two more things, which this PR adds.

triangles(maxEdgeDegrees:): small triangles

A straight edge only follows a curved projection when it's short. Also, near a map's edge or across its seam, only a small triangle can be left out or cut without anyone noticing. So this keeps triangulating the same way, then:

  1. Flips long slivers. Around triangles that are too long, it flips shared edges where the other diagonal is shorter (Delaunay), so earcut's long ears become rounder triangles that need fewer splits. A strip with a corner every degree ends up with 400 triangles instead of 5,588.
  2. Refines. It splits the longest edge at its middle until every edge is under the limit. Any edge that's too long gets split at the same point from both sides, so no corner ends up on the side of a neighbouring triangle. That matters because such a T-junction leaves a crack once the triangles are projected.

All triangles come out counter-clockwise. triangleIndices() itself is unchanged, and maxEdgeDegrees: .infinity gives exactly its triangles as positions.

unwrapped(): rings that don't jump across the antimeridian

Both triangulation calls take longitude and latitude as flat coordinates, which goes wrong for:

  • a ring that crosses the antimeridian (170° → −170°);
  • a ring that goes around a pole. Natural Earth's Antarctica is one: it jumps from 180° to −180° and carries on. Triangulated flat, it leaves out the polar cap.

unwrapped() lets longitudes run on past ±180° so that each step is under 180°. It closes a ring that ends a turn away from where it started along its pole, setting off from the corner closest to the pole so the closing edge doesn't cut across the ring. It moves holes by whole turns to sit with the exterior, and drops the back-and-forth edges along the antimeridian that pre-split rings have.

Both functions were first written for one app's GPU renderer and have been used there. Moving them here lets other renderers share them.

Testing

swift test. The new RefinedTrianglesTests check that:

  • the area adds up, the triangles turn the same way and edges stay under the limit, across squares, holes, self-crossing coastline loops, figures of eight, rings past the antimeridian and an Antarctica-shaped ring;
  • no corner lands on a neighbour's side;
  • .infinity gives back what triangleIndices() gives;
  • the flips keep the strip at 400 triangles. That test fails without the flips.

For unwrapped(), they check that each step is under 180° (except along a pole) and that the triangles' area adds up. The cases are rings across the antimeridian, holes across it or a turn away, rings around either pole, one that starts over a bay, and one already split along the pole. A polygon that doesn't wrap comes back unchanged.

🤖 Generated with Claude Code

nighthawk and others added 2 commits November 7, 2026 15:10
Splits the triangulation until no edge is longer than a limit, splitting each edge at its middle from both sides, after flipping long slivers into rounder triangles. It's for renderers that project triangle corners and join them with straight lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Triangulating takes longitude and latitude as flat coordinates, so a ring that crosses the antimeridian, or goes around a pole as Antarctica's often does, needs its longitudes to run on, and the latter needs closing along the pole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nighthawk nighthawk changed the title feat: add GeoJSON.Polygon.triangles(maxEdgeDegrees:) feat: add GeoJSON.Polygon.triangles(maxEdgeDegrees:) and unwrapped() Nov 7, 2026
@nighthawk

Copy link
Copy Markdown
Member Author

Written by Claude on Adrian's behalf. Suggested squash-merge message:

feat: add GeoJSON.Polygon.triangles(maxEdgeDegrees:) and unwrapped()

- Add `triangles(maxEdgeDegrees:)`, which flips long slivers and splits edges at their middle, from both sides, until none is longer than the limit
- Add `unwrapped()`, which lets rings run on past the antimeridian and closes a ring that goes around a pole along that pole
- Add both to the README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant