-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopplot.py
More file actions
78 lines (63 loc) · 2.08 KB
/
loopplot.py
File metadata and controls
78 lines (63 loc) · 2.08 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import matplotlib.pyplot as plt
import re
# paste output into data string block
data = """
(1,2)(1,1)(2,1)(3,1)(3,2)(3,3)(2,3)(1,3)(1,2)
"""
def all_symmetries(loop, grid_size):
order_rotations = [tuple(loop[i:] + loop[:i]) for i in range(len(loop))]
rotations = [
lambda p: p,
lambda p: [(y, grid_size + 1 - x) for x, y in p],
lambda p: [(grid_size + 1 - x, grid_size + 1 - y) for x, y in p],
lambda p: [(grid_size + 1 - y, x) for x, y in p]
]
mirror = lambda p: [(y, x) for x, y in p]
return {
tuple(transformation)
for order in order_rotations
for rot in rotations
for transformation in (
rot(order),
mirror(rot(order)),
list(reversed(rot(order))),
list(reversed(mirror(rot(order))))
)
}
def filter_loops(loops):
grid_size = max(max(x, y) for loop in loops for (x, y) in loop)
seen = set()
unique_loops = []
for loop in loops:
loop = loop[:-1]
sym_set = all_symmetries(loop, grid_size)
if sym_set & seen:
continue
loop.append(loop[0])
unique_loops.append(loop)
seen.update(sym_set)
return unique_loops
lines = data.strip().split('\n')
loops = []
for line in lines:
loop = [(int(x), int(y)) for x, y in re.findall(r"\((\d+),(\d+)\)", line)]
loops.append(loop)
loops = filter_loops(loops)
print("\n", len(loops))
for loop in loops:
for t in loop:
print(f"({t[0]},{t[1]})", end="")
print()
figs = []
for i, loop in enumerate(loops):
x_coords, y_coords = zip(*loop)
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot(x_coords, y_coords, marker='o', linestyle='-', color='b', markersize=5)
for x, y in loop:
ax.text(x, y, f"({x},{y})", fontsize=9)
ax.grid(True)
ax.set_title(f"Loop {i+1}")
ax.set_xticks(range(min(x_coords)-1, max(x_coords)+2))
ax.set_yticks(range(min(y_coords)-1, max(y_coords)+2))
figs.append(fig)
plt.show()