Coverage for app / documents / visuals / maps / clip.py: 97%
77 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""Cutting shapes to the map's frame, before they are drawn.
3A country's outline runs far past the frame. Cut here, it never reaches the
4PDF — smaller files, and nothing drawn over the legend below the map.
5"""
7from __future__ import annotations
9Point = tuple[float, float]
10Rect = tuple[float, float, float, float]
13def _inside(p: Point, edge: int, r: Rect) -> bool:
14 x0, y0, x1, y1 = r
15 return (p[0] >= x0, p[0] <= x1, p[1] >= y0, p[1] <= y1)[edge]
18def _cross(a: Point, b: Point, edge: int, r: Rect) -> Point:
19 x0, y0, x1, y1 = r
20 (ax, ay), (bx, by) = a, b
21 if edge in (0, 1):
22 x = x0 if edge == 0 else x1
23 t = (x - ax) / (bx - ax)
24 return x, ay + t * (by - ay)
25 y = y0 if edge == 2 else y1
26 t = (y - ay) / (by - ay)
27 return ax + t * (bx - ax), y
30def clip_ring(points: list[Point], r: Rect) -> list[Point]:
31 """A closed ring cut to ``r`` (Sutherland–Hodgman): what lies outside
32 becomes the frame's edge, which an even-odd fill draws correctly."""
33 out = points
34 for edge in range(4):
35 if not out:
36 break
37 source, out = out, []
38 previous = source[-1]
39 for point in source:
40 if _inside(point, edge, r):
41 if not _inside(previous, edge, r):
42 out.append(_cross(previous, point, edge, r))
43 out.append(point)
44 elif _inside(previous, edge, r):
45 out.append(_cross(previous, point, edge, r))
46 previous = point
47 return out
50def clip_line(points: list[Point], r: Rect) -> list[list[Point]]:
51 """An open line cut to ``r`` (Liang–Barsky per segment): the pieces
52 that lie inside."""
53 pieces: list[list[Point]] = []
54 current: list[Point] = []
55 x0, y0, x1, y1 = r
56 for a, b in zip(points, points[1:]):
57 dx, dy = b[0] - a[0], b[1] - a[1]
58 t0, t1 = 0.0, 1.0
59 keep = True
60 for p, q in (
61 (-dx, a[0] - x0),
62 (dx, x1 - a[0]),
63 (-dy, a[1] - y0),
64 (dy, y1 - a[1]),
65 ):
66 if p == 0:
67 if q < 0:
68 keep = False
69 break
70 continue
71 t = q / p
72 if p < 0:
73 t0 = max(t0, t)
74 else:
75 t1 = min(t1, t)
76 if t0 > t1:
77 keep = False
78 break
79 if not keep:
80 if current:
81 pieces.append(current)
82 current = []
83 continue
84 start = (a[0] + t0 * dx, a[1] + t0 * dy)
85 end = (a[0] + t1 * dx, a[1] + t1 * dy)
86 if not current or current[-1] != start:
87 if current:
88 pieces.append(current)
89 current = [start]
90 current.append(end)
91 if t1 < 1.0:
92 pieces.append(current)
93 current = []
94 if current:
95 pieces.append(current)
96 return [p for p in pieces if len(p) > 1]
99def thinned(points: list[Point], tolerance: float = 0.5) -> list[Point]:
100 """Points closer than ``tolerance`` to the last kept one dropped: a
101 1:50m coastline at a world view is thousands of points per inch."""
102 out: list[Point] = []
103 for p in points:
104 if not out or abs(p[0] - out[-1][0]) + abs(p[1] - out[-1][1]) >= tolerance:
105 out.append(p)
106 return out