-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_simplifier.py
55 lines (51 loc) · 1.65 KB
/
line_simplifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import svg
class LineSimplifier:
def __init__(self):
self.vertical_lines = []
self.horizontal_lines = []
def add_rectangle(self, l, r, t, b):
self.horizontal_lines.append((l, r, t))
self.horizontal_lines.append((l, r, b))
self.vertical_lines.append((l, t, b))
self.vertical_lines.append((r, t, b))
def render(self, width):
all_y = set([y for _, _, y in self.horizontal_lines])
simplified_horizontal_lines = [
(
min(x1 for x1, _, _y in self.horizontal_lines if y == _y),
max(x2 for _, x2, _y in self.horizontal_lines if y == _y),
y,
)
for y in all_y
]
all_x = set([x for x, _, _ in self.vertical_lines])
simplified_vertical_lines = [
(
x,
min(y1 for _x, y1, _ in self.vertical_lines if x == _x),
max(y2 for _x, _, y2 in self.vertical_lines if x == _x),
)
for x in all_x
]
return [
*[
svg.Line(
class_=["cut"],
x1=x1 * width,
y1=y * width,
x2=x2 * width,
y2=y * width,
)
for x1, x2, y in simplified_horizontal_lines
],
*[
svg.Line(
class_=["cut"],
x1=x * width,
y1=y1 * width,
x2=x * width,
y2=y2 * width,
)
for x, y1, y2 in simplified_vertical_lines
],
]