-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_spiral_functional.py
More file actions
227 lines (156 loc) · 7.85 KB
/
grid_spiral_functional.py
File metadata and controls
227 lines (156 loc) · 7.85 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pygame
import math
import time
pygame.init()
window = pygame.display.set_mode((1000,1000))
sizeWindow = pygame.display.get_window_size()
centerWindowPos = (sizeWindow[0] / 2 , sizeWindow[1] /2) # To get the coords for Head and Tail of the origin line
# Line information
diffcolor = (255, 255, 0)
lineColour = (0, 0, 0)
Head = (centerWindowPos[0], centerWindowPos[1] - 100)
Tail = (centerWindowPos[0], centerWindowPos[1])
LINE_WIDTH = 5
NUM_OF_STEPS = 5
NUM_OF_RENDERED_LINES = 20
myLines = []
storePointsfrom = []
SPIRAL_STEP_SIZE = 1.5
# Function uses derived formula from L_1 metric unit sphere to find points in the 4th quadrant
def ComputeNewLineFourthQuadrant(distance, prevHead):
midPoint = (distance/ 2)
k = int(midPoint)
endPoint = ( (prevHead[0] + k), (k + prevHead[1] - distance) )
return endPoint
# Function uses derived formula from L_1 metric unit sphere to find points in the 1st quadrant
def ComputeNewLineFirstQuadrant(distance, prevHead):
print(f"Value of prevhead in FirstQuad: ______________{prevHead}")
midPoint = (distance/ 2)
k = int(midPoint)
endPoint = ( (prevHead[0] + k), (prevHead[1] + distance - k ) )
return endPoint
def ComputeNewLineThirdQuadrant(distance, prevHead):
midPoint = (distance/ 2)
k = int(midPoint)
endPoint = ( (prevHead[0] - k), (k + prevHead[1] - distance) )
return endPoint
# Function uses derived formula from L_1 metric unit sphere to find points in the 1st quadrant
def ComputeNewLineSecondQuadrant(distance, prevHead):
midPoint = (distance/ 2)
k = int(midPoint)
endPoint = ( (prevHead[0] - k), (prevHead[1] + distance - k ) )
return endPoint
# Get the length of the phantom line.
def ModifySpiralStep(length, counter, positionOfSpiral):
step_size = (length * SPIRAL_STEP_SIZE)
return step_size
def GenerateNewLinesLeftDown( prevLineHead, prevLineTail, counter):
# Identification string
positionOfSpiral = "Left-Down Hand Side"
print(f'{positionOfSpiral}: Begining points prevLineHead: {prevLineHead}. and prevLineTail {prevLineTail}-- \n')
# To get the radius of the L_1 unit sphere respective to previous head
lenOfPhantomLine = (prevLineTail[1] - prevLineHead[1])
Hypotenuse = ModifySpiralStep(lenOfPhantomLine, counter, positionOfSpiral)
radius = int(Hypotenuse)
print(f'Left-Down Hand Side: Length of the radius in Manhattan Metric unit sphere {radius} Loop No {counter}. \n')
# Counter is used as a switch to change the direction of the each line generated
if ( counter % 2 == 0):
newLineHead = ComputeNewLineThirdQuadrant(radius, prevLineHead)
else:
newLineHead = ComputeNewLineFourthQuadrant(-radius, prevLineHead)
print(f'Right-Down Hand Side: New point after function compute newline: {newLineHead} ____ The old points to be a tail now: {prevLineHead}. \n')
return newLineHead
def GenerateNewLinesRightDown( prevLineHead, prevLineTail, counter):
# Identification string
positionOfSpiral = "Right-Down Hand Side"
print(f'{positionOfSpiral}: Begining points prevLineHead: {prevLineHead}. and prevLineTail {prevLineTail}-- \n')
# To get the radius of the L_1 unit sphere respective to previous head
lenOfPhantomLine = (prevLineTail[1] - prevLineHead[1])
Hypotenuse = ModifySpiralStep(lenOfPhantomLine, counter, positionOfSpiral)
radius = int(Hypotenuse)
print(f'Right-Down Hand Side: Length of the radius in Manhattan Metric unit sphere {radius} Loop No {counter}. \n')
# Counter is used as a switch to change the direction of the each line generated
if ( counter % 2 == 0):
newLineHead = ComputeNewLineThirdQuadrant(-radius, prevLineHead)
else:
newLineHead = ComputeNewLineFourthQuadrant(radius, prevLineHead)
print(f'Right-Down Hand Side: New point after function compute newline: {newLineHead} ____ The old points to be a tail now: {prevLineHead}. \n')
return newLineHead
# Left side spiral
def GenerateNewLinesLEFT( prevLineHead, prevLineTail, counter):
# Identification string
positionOfSpiral = "Left Hand Side"
print(f'{positionOfSpiral}: Begining points prevLineHead: {prevLineHead}. and prevLineTail {prevLineTail}-- \n')
# To get the radius of the L_1 unit sphere respective to previous head
lenOfPhantomLine = (prevLineTail[1] - prevLineHead[1])
Hypotenuse = ModifySpiralStep(lenOfPhantomLine, counter, positionOfSpiral)
radius = int(Hypotenuse)
print(f'Left Hand Side: Length of the radius in Manhattan Metric unit sphere {radius} Loop No {counter}. \n')
# Counter is used as a switch to change the direction of the each line generated
if ( counter % 2 == 0):
newLineHead = ComputeNewLineSecondQuadrant(radius, prevLineHead)
else:
newLineHead = ComputeNewLineThirdQuadrant(radius, prevLineHead)
print(f'Right-Down Hand Side: New point after function compute newline: {newLineHead} ____ The old points to be a tail now: {prevLineHead}. \n')
return newLineHead
def GenerateNewLinesRIGHT( prevLineHead, prevLineTail, counter):
# Identification string
positionOfSpiral = "Right Hand Side"
print(f'{positionOfSpiral}: Begining points prevLineHead: {prevLineHead}. and prevLineTail {prevLineTail}-- \n')
# To get the radius of the L_1 unit sphere respective to previous head
lenOfPhantomLine = (prevLineTail[1] - prevLineHead[1])
print(f'Right Hand Side: Length of the Phantomlin ------------------({lenOfPhantomLine}) . \n')
#CalculateHypotenuse
Hypotenuse = ModifySpiralStep(lenOfPhantomLine, counter, positionOfSpiral)
radius = int(Hypotenuse)
print(f'Right Hand Side: Length of the radius in Manhattan Metric unit sphere {radius} Loop No {counter}. \n')
# Counter is used as a switch to change the direction of the each line generated
if ( counter % 2 == 0):
newLineHead = ComputeNewLineFourthQuadrant(radius, prevLineHead)
else:
newLineHead = ComputeNewLineFirstQuadrant(radius, prevLineHead)
print(f'Right-Down Hand Side: New point after function compute newline: {newLineHead} ____ The old points to be a tail now: {prevLineHead}. \n')
return newLineHead
def main():
global Tail
global Head
# Generate Lines
for lines in range(NUM_OF_STEPS):
newLineHead = GenerateNewLinesRIGHT(Head , Tail, lines)
myLines.append([newLineHead,Head])
Tail = Head
Head = newLineHead
Head = (centerWindowPos[0], centerWindowPos[1] - 100)
Tail = (centerWindowPos[0], centerWindowPos[1])
for lines in range(NUM_OF_STEPS):
newLineHead = GenerateNewLinesLEFT(Head , Tail, lines+1)
myLines.append([newLineHead, Head])
Tail = Head
Head = newLineHead
Head = (centerWindowPos[0], centerWindowPos[1] - 100)
Tail = (centerWindowPos[0], centerWindowPos[1])
for lines in range(NUM_OF_STEPS):
newLineHead = GenerateNewLinesRightDown(Head , Tail, lines)
myLines.append([newLineHead, Head])
Tail = Head
Head = newLineHead
Head = (centerWindowPos[0], centerWindowPos[1] - 100)
Tail = (centerWindowPos[0], centerWindowPos[1])
for lines in range(NUM_OF_STEPS):
newLineHead = GenerateNewLinesLeftDown(Head , Tail, lines+1)
myLines.append([newLineHead, Head])
Tail = Head
Head = newLineHead
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
window.fill((245, 245, 220))
# Render Lines
for lines in range(NUM_OF_RENDERED_LINES):
pygame.draw.line(window, lineColour,myLines[lines][0], myLines[lines][1] , LINE_WIDTH)
pygame.display.flip()
pygame.quit()
main()