@dj2638/

Hilbert-curve-artist

Python (with Turtle)

No description

fork
loading
main.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
import turtle
from turtle import color
from turtle import forward
from turtle import right
from turtle import left
from turtle import speed
from turtle import delay
from turtle import hideturtle
from turtle import setx
from turtle import sety
from turtle import setpos
from turtle import penup
from turtle import pendown

speed(128)
print dir(turtle)
hideturtle()

penup()
setpos(248, -248)
pendown()

color('blue')

def hilbert_order_2(l, d=1):
	ang = [d*90, -d*90]
	# first quadrant
	forward(l)
	right(ang[0])
	forward(l)
	right(ang[0])
	forward(l)
	#transition
	right(ang[1])
	forward(l)
	#second quadrant
	forward(l)
	right(ang[1])
	forward(l)
	right(ang[1])
	forward(l)
	#transition
	right(ang[0])
	forward(l)
	#third quadrant
	right(ang[0])
	forward(l)
	right(ang[1])
	forward(l)
	right(ang[1])
	forward(l)
	#transition
	forward(l)
	#third quadrant
	right(ang[1])
	forward(l)
	right(ang[0])
	forward(l)
	right(ang[0])
	forward(l)
	
def hilbert_order_3(l, d=1):
	ang = [d*90, -d*90]
	#first quadrant
	hilbert_order_2(l, d=d)
	forward(l)
	right(ang[0])
	#second quadrant
	hilbert_order_2(l, d=-d)
	forward(l)
	#third quadrant
	hilbert_order_2(l, d=-d)
	right(ang[0])
	forward(l)
	#fourth quadrant
	hilbert_order_2(l, d=d)
	
def hilbert_order_4(l, d=1):
	ang = [-d*90, d*90]
	#first quadrant
	hilbert_order_3(l, d=-d)
	#transition
	right(ang[1])
	forward(l)
	#second quadrant
	hilbert_order_3(l, d=d)
	#transition
	right(ang[0])
	forward(l)
	right(ang[0])
	#third quadrant
	hilbert_order_3(l, d=d)
	#transition
	forward(l)
	right(ang[1])
	#fourth quadrant
	hilbert_order_3(l, d=-d)
	
def hilbert_order_5(l, d=1):
	ang = [-d*90, d*90]
	#first quadrant
	hilbert_order_4(l, d=-d)
	#transition
	forward(l)
	right(ang[1])
	#second quadrant
	hilbert_order_4(l, d=d)
	#transition
	forward(l)
	#third quadrant
	hilbert_order_4(l, d=d)
	#transition
	right(ang[1])
	forward(l)
	#fourth quadrant
	hilbert_order_4(l, d=-d)

done = False
while not done:
	# hilbert direction
	right(-90)
	l = 8
	ang = [90, -90]
	#first quadrant
	hilbert_order_5(l, d=-1)
	#transition
	right(ang[0])
	forward(l)
	#second quadrant
	color('red')
	hilbert_order_5(l, d=1)
	#transition
	right(ang[1])
	forward(l)
	right(ang[1])
	#third quadrant
	color('green')
	hilbert_order_5(l, d=1)
	#transition
	forward(l)
	right(ang[0])
	#fourth quadrant
	color('orange')
	hilbert_order_5(l, d=-1)
	done = True

turtle.done()
result
console