-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gosper Curve.py
63 lines (45 loc) · 1.45 KB
/
Gosper Curve.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
import matplotlib.pyplot as plt
import numpy as np
axiom = "A"
rule1 = "A-B--B+A++AA+B-"
rule2 = "+A-BB--B-A++A+B"
iterations = 4
count = 0
coordinates = [(0, -1), (0, 0)]
for x in range(iterations):
last = len(coordinates)-1
coordinates = [(coordinates[last]), coordinates[last-1]]
substr = ""
for k in axiom:
if k == "A":
substr += rule1
elif k == "B":
substr += rule2
else:
substr += k
stack = []
for i in substr:
if i == "+":
stack.append(60)
elif i == "-":
stack.append(-60)
else:
if (i == "A" or i == "B") and count != 0:
angle = 0
for j in stack:
angle += j
stack = []
theta = np.radians(angle)
R = np.array(((np.cos(theta), -np.sin(theta)),
(np.sin(theta), np.cos(theta))))
last = len(coordinates)-1
line = np.array([coordinates[last][0]-coordinates[last-1]
[0], coordinates[last][1]-coordinates[last-1][1]])
vector = list(R.dot(line))
coordinates.append(
(coordinates[last][0]+vector[0], coordinates[last][1]+vector[1]))
count += 1
axiom = substr
plt.plot([i[0] for i in coordinates], [i[1] for i in coordinates], c="black")
plt.axis("equal")
plt.show()