-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpirals.py
More file actions
73 lines (57 loc) · 1.35 KB
/
Spirals.py
File metadata and controls
73 lines (57 loc) · 1.35 KB
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
class Point (object):
x = 0
y = 0
def __init__(self, x,y):
self.x = int(x)
self.y = int(y)
directions = ["+x", "+y","-x","-y"]
def getInitPos(size):
if (size%2 ==0): return Point(size/2-1,size/2-1)
return Point(size/2,size/2)
def emptySpiral(size):
spiral = []
for i in range(size):
row = []
for j in range(size):
row.append(0)
spiral.append(row)
return spiral
def changeDir(d):
return directions[(directions.index(d)+1)%4]
def movePos(pos,stepsUntilTurn, dir):
if (dir == directions[0]): pos.x += 1
elif (dir == directions[1]): pos.y +=1
elif (dir == directions[2]): pos.x -=1
elif (dir == directions[3]): pos.y -=1
return pos
def makeSpiral(size):
spiral = emptySpiral(size)
number = 1
stepsUntilTurn = 1
stepsUntilTurnCounter = 0
isSecond = False
pos = getInitPos(size)
dir = directions[0]
while (number <= size**2 ):
spiral[pos.y][pos.x] = number
pos = movePos(pos,stepsUntilTurn, dir)
if (stepsUntilTurnCounter == 0):
dir = changeDir(dir)
if (isSecond):
isSecond = False
stepsUntilTurn +=1
else:
isSecond = True
stepsUntilTurnCounter = stepsUntilTurn
number += 1
stepsUntilTurnCounter -=1
return spiral
def printSpiral(spiral):
for i in range(len(spiral)):
print (spiral[i])
def main():
size = int(input("How big?"))
spiral = makeSpiral(size)
printSpiral(spiral)
input()
main()