Triangulation 101¶

 1"""Ear‑clipping and Delaunay examples."""
 2from geo.core import Point2D
 3from geo.primitives_2d import Polygon
 4from geo.operations.triangulation import (
 5    triangulate_simple_polygon_ear_clipping, delaunay_triangulation_points_2d
 6)
 7import matplotlib.pyplot as plt
 8
 9poly = Polygon([
10    Point2D(0, 0), Point2D(4, 0), Point2D(5, 2), Point2D(3, 4), Point2D(1, 3)
11])
12triangles = triangulate_simple_polygon_ear_clipping(poly)
13print("Ear-clipping output:", len(triangles), "triangles")
14
15pts = [Point2D(x, y) for x, y in [(0,0),(4,0),(5,2),(3,4),(1,3)]]
16tris = delaunay_triangulation_points_2d(pts)
17print("Delaunay produced", len(tris), "triangles")
18
19# crude plot
20for t in tris:
21    xs = [t.p1.x, t.p2.x, t.p3.x, t.p1.x]
22    ys = [t.p1.y, t.p2.y, t.p3.y, t.p1.y]
23    plt.plot(xs, ys, "k-")
24plt.scatter([p.x for p in pts], [p.y for p in pts])
25plt.gca().set_aspect("equal", adjustable="box")
26plt.show()