-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_circle_data.py
96 lines (74 loc) · 2.59 KB
/
plot_circle_data.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import matplotlib.pyplot as plt
from matplotlib.patches import Circle as CirclePatch
from ghdtimer import Timer
#############################################################################
# Collision Detection #
def check_collisions(self, circles) -> bool:
# boundary check
if self.x+self.r>=1 or self.x-self.r<=0 or self.y+self.r>=1 or self.y-self.r<=0:
return True
for circle in circles:
if circle != self:
dist_squared = (self.x-circle.x)*(self.x-circle.x) + (self.y-circle.y)*(self.y-circle.y)
# Collided with at least one circle
if dist_squared < (self.r+circle.r)*(self.r+circle.r):
return True
# No collision
return False
#############################################################################
# Circle Class #
class Circle():
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
self.r = 0.0
self.frozen = False
def is_valid(self, circles) -> bool:
collided = check_collisions(self, circles)
if collided:
return False
return True
def grow(self, dr: float=0.1) -> None:
self.r += dr
def shrink(self, dr: float=0.1) -> None:
self.r -= dr
#############################################################################
# Plotting #
def plot(circles):
fig, ax = plt.subplots(dpi=300, figsize=(5, 5), constrained_layout=True)
# make sure the plot is square
ax.set_aspect('equal')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
# Circles
counter = 0
for c in circles:
# skip small circles
if c.r <= 0.000000001:
continue
# only border of circle and change line width based on radius
ax.add_patch(CirclePatch((c.x, c.y), c.r, fill=False, color='black',))# linewidth=max(c.r*12, 0.2)))
counter += 1
print("Number of plotted circles:", counter)
# Current timestamp
import datetime
timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
plt.savefig(f"images/result_{timestamp}.png")
# plt.show()
#############################################################################
# Main #
def main():
circles = []
with open('result.txt', 'r') as f:
for line in f:
x, y, r = line.split(", ")
c = Circle(float(x), float(y))
c.r = float(r)
circles.append(c)
print("Number of circles:", len(circles))
plot(circles)
if __name__ == '__main__':
main()