-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhilbert.py
More file actions
56 lines (45 loc) · 966 Bytes
/
hilbert.py
File metadata and controls
56 lines (45 loc) · 966 Bytes
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
import sys
import matplotlib.pyplot as plt
n = int(sys.argv[1])
def matr_mult(A,B):
C = [0,0]
for i in range(len(A)):
for j in range(len(B[0])):
s = 0
for k in range(len(B)):
s += A[k]*B[k][j]
C[j] = s
return C
R1 = [[0,-1],[1,0]]
R2 = [[0,1],[-1,0]]
def rplc(n, way):
newway = ""
for ch in way:
if ch == "L":
newway += "+RF-LFL-FR+"
elif ch == "R":
newway += "-LF+RFR+FL-"
elif ch == "+" or ch =="-" or ch == "F":
newway += ch
way = newway
n -= 1
if n <= 0:
return way
else:
return rplc(n, way)
way = "L"
way = rplc(n, way)
matrix = [[0,0]]
vect = [1,0]
for i in way:
if i == "+":
vect = matr_mult(vect, R2)
elif i == "-":
vect = matr_mult(vect, R1)
elif i == "F":
matrix.append([matrix[-1][0]+vect[0], matrix[-1][1]+vect[1]])
matrix = [*zip(*matrix)]
plt.plot(matrix[0], matrix[1], "k")
plt.axis('equal');
plt.show()
#python3 hilbert.py 7