-
Notifications
You must be signed in to change notification settings - Fork 0
/
faster_kolam.py
157 lines (136 loc) · 6.85 KB
/
faster_kolam.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import matplotlib.pyplot as plt
import numpy as np
import math
class Node:
def __init__(self, parent_dir, is_even, length, dock_parent, color, is_root = False, end = False) -> None:
self.growth_dir = parent_dir
self.is_even = is_even
self.is_root = is_root
self.length = length
self.dock_parent = dock_parent
self.endOdd = end
self.mycords= (0,0)
self.color = color
self.draw()
def draw(self):
if self.is_even:
self.mycords = draw_even(self.length, self.growth_dir, self.dock_parent, self.is_root, self.color)
else:
self.mycords = draw_odd(self.length, self.growth_dir, self.dock_parent, self.endOdd, self.color)
def giveCenter(self):
return self.mycords
def draw_even(l, dir, p_origin, is_root, color):
mycords= (0,0)
c2 = l/math.sqrt(2) + 0.5*l
c1 = l/math.sqrt(2) + 1.5*l
x = np.array([l/2, l/2, c1 - l, c1])
y = np.array([c1, c1 - l, l/2, l/2])
if is_root:
plt.plot(x, y, color = color)
plt.plot(-x, -y, color = color)
plt.plot(x, -y, color = color)
plt.plot(-x, y, color = color)
else:
if dir == 1:
mycords = (c1 + c2 + p_origin[0], p_origin[1])
plt.plot(x + c2 + c1 + p_origin[0], y + p_origin[1], color = color)
plt.plot(x + c2 + c1 + p_origin[0], -y + p_origin[1], color = color)
plt.plot(-x[:-1] + c2 + c1 + p_origin[0], -y[:-1] + p_origin[1], color = color)
plt.plot(-x[:-1] + c2 + c1 + p_origin[0], y[:-1] + p_origin[1], color = color)
elif dir == -1:
mycords = (- c1 - c2 + p_origin[0], p_origin[1])
plt.plot(x[:-1] - c2 - c1 + p_origin[0], y[:-1] + p_origin[1], color = color)
plt.plot(x[:-1] - c2 - c1 + p_origin[0], -y[:-1] + p_origin[1], color = color)
plt.plot(-x - c2 - c1 + p_origin[0], -y + p_origin[1], color = color)
plt.plot(-x - c2 - c1 + p_origin[0], y + p_origin[1], color = color)
elif dir == 2:
mycords = (p_origin[0], p_origin[1] + c1 + c2)
plt.plot(x + p_origin[0], y + c2 + c1 + p_origin[1], color = color)
plt.plot(-x + p_origin[0], y + c2 + c1 + p_origin[1], color = color)
plt.plot(x[1:] + p_origin[0], -y[1:] + c2 + c1 + p_origin[1], color = color)
plt.plot(-x[1:] + p_origin[0], -y[1:] + c2 + c1 + p_origin[1], color = color)
elif dir == -2:
mycords = (p_origin[0], p_origin[1] - c1 - c2)
plt.plot(x[1:] + p_origin[0], y[1:] - c2 - c1 + p_origin[1], color = color)
plt.plot(-x[1:] + p_origin[0], y[1:] - c2 - c1 + p_origin[1], color = color)
plt.plot(-x + p_origin[0], -y - c2 - c1 + p_origin[1], color = color)
plt.plot(x + p_origin[0], -y - c2 - c1 + p_origin[1], color = color)
plt.axis('off')
plt.gca().set_aspect('equal')
return mycords
def draw_odd(l, dir, p_origin, end, color):
mycords= (0,0)
c2 = l/math.sqrt(2) + 0.5*l
c1 = l/math.sqrt(2) + 1.5*l
x = np.array([-c1, -(c1 - l), -l/2, -l/2] + [l/2, l/2, c1 - l, c1])
y = np.array([l/2, l/2, c1 - l, c1] + [c1, c1 - l, l/2, l/2])
if dir == 1:
if end == True: plt.plot([x[-1] + c2 + c1 + p_origin[0], x[-1] + c2 + c1 + p_origin[0]], [y[-1] + p_origin[1], -y[-1] + p_origin[1]], color = color)
mycords = (c1 + c2 + p_origin[0], p_origin[1])
plt.plot(x[1:] + c2 + c1 + p_origin[0], y[1:] + p_origin[1], color = color)
plt.plot(x[1:] + c2 + c1 + p_origin[0], -y[1:] + p_origin[1], color = color)
elif dir == -1:
if end == True: plt.plot([x[0] - c2 - c1 + p_origin[0], x[0] - c2 - c1 + p_origin[0]], [y[0] + + p_origin[1], -y[0] + p_origin[1]], color = color)
mycords = (- c1 - c2 + p_origin[0], p_origin[1])
plt.plot(x[:-1] - c2 - c1 + p_origin[0], y[:-1] + p_origin[1], color = color)
plt.plot(x[:-1] - c2 - c1 + p_origin[0], -y[:-1] + p_origin[1], color = color)
elif dir == 2:
if end == True: plt.plot([y[-1] + p_origin[0], -y[-1] + p_origin[0]], [x[-1] + c2 + c1 + p_origin[1], x[-1] + c2 + c1 + p_origin[1]], color = color)
mycords = (p_origin[0], p_origin[1] + c1 + c2)
plt.plot(y[1:] + p_origin[0], x[1:] + c2 + c1 + p_origin[1], color = color)
plt.plot(-y[1:] + p_origin[0], x[1:] + c2 + c1 + p_origin[1], color = color)
elif dir == -2:
if end == True: plt.plot([y[0] + p_origin[0], -y[0] + p_origin[0]], [x[0] - c2 - c1 + p_origin[1], x[0] - c2 - c1 + p_origin[1]], color = color)
mycords = (p_origin[0], p_origin[1] - c1 - c2)
plt.plot(y[:-1] + p_origin[0], x[:-1] - c2 - c1 + p_origin[1], color = color)
plt.plot(-y[:-1] + p_origin[0], x[:-1] - c2 - c1 + p_origin[1], color = color)
plt.axis('off')
plt.gca().set_aspect('equal')
return mycords
def driver(parent, iter, iter_done = 1):
color = 'black'
cords = parent.giveCenter()
if iter_done < iter and cords[0] >= 0 and cords[1] >= 0:
dir = [1, -1, 2, -2]
del dir[dir.index(-1 * parent.growth_dir)]
if parent.is_even:
if iter_done in odds:
ch1 = Node(dir[0], False, 10, cords, color)
ch2 = Node(dir[1], False, 10, cords, color)
ch3 = Node(dir[2], False, 10, cords, color)
if parent.is_root:
ch4 = Node((-1 * parent.growth_dir), False, 10, cords, color)
driver(ch4, iter, iter_done + 1)
del ch4
driver(ch1, iter, iter_done + 1)
del ch1
driver(ch2, iter, iter_done + 1)
del ch2
driver(ch3, iter, iter_done + 1)
del ch3
else:
lastCap = True if iter_done + 1 == iter else False
ch1 = Node(parent.growth_dir, False, 10, cords, color,end = lastCap)
del dir[dir.index(parent.growth_dir)]
ch2 = Node(dir[0], False, 10, cords, color, end = True)
ch3 = Node(dir[1], False, 10, cords, color, end = True)
if iter_done in oddsodds:
if abs(int(ch1.giveCenter()[1])) in oddsoddsodds or abs(int(ch1.giveCenter()[0])) in oddsoddsodds:
driver(ch1, iter, iter_done + 1)
else:
driver(ch1, iter, iter_done + 1)
del ch1
else:
ch1 = Node(parent.growth_dir, True, 10, cords, color)
driver(ch1, iter, iter_done + 1)
del ch1
else:
return None
iter = 7
odds = list(range(1, 2**(iter -1), 2))[::2]
oddsodds = list(range(7, 2**(iter - 1), 8))
# oddsoddsodds = [int(10*i*(2+math.sqrt(2))) for i in range(0,2**(iter-1),8)]
oddsoddsodds = [int(10*i*(2+math.sqrt(2))) for i in [0,8,16]]
p = Node(-1, True, 10, (0,0), "black", is_root = True)
driver(p, 2**(iter-1))
plt.show()