-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral_motion.py
67 lines (48 loc) · 1.38 KB
/
spiral_motion.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
#!/usr/bin/env python
'''
spiral_motion.py - Example of discretised spiral motion
Author: Eric Saunders
October 2011
'''
import turtle
turtle.shape("turtle")
def move_unit_length(pos, direction):
direction_mapping = dict (
w = (-1,0),
n = (0,1),
e = (1,0),
s = (0,-1)
)
new_pos_x = pos[0] + direction_mapping[direction][0]
new_pos_y = pos[1] + direction_mapping[direction][1]
return new_pos_x, new_pos_y
def move_across_side(pos, cur_direction, length):
for i in range(1, length+1):
pos = move_unit_length(pos, cur_direction)
print pos
return pos
rh_turn = dict(
w = 'n',
n = 'e',
e = 's',
s = 'w'
)
origin = (0,0)
n_turns = 9
cur_direction = 'w'
print "Starting position:", origin
print "Starting direction:", cur_direction
length = 1
pos = origin
for i in range(n_turns):
for j in range(2):
pos = move_across_side(pos, cur_direction, length)
turtle_pos = (pos[0]*50, pos[1]*50)
turtle.setpos(turtle_pos)
cur_direction = rh_turn[cur_direction]
turtle.right(90)
# Increment the length
length += 1
print "Final position:", pos
import time
time.sleep(5)