-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPattern.py
More file actions
220 lines (186 loc) · 8.35 KB
/
Pattern.py
File metadata and controls
220 lines (186 loc) · 8.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
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
from graphics import *
import math
def main():
size, colourList = getInput()
window = drawFullPatchwork(size, colourList)
colourChanger(window, size, colourList)
def getInput():
validColours = ["red", "green", "blue", "orange", "brown", "pink"]
validwindowdowSize = ["5", "7", "9"]
colourList = []
counter = 0
while counter < 1:
size = input("please enter the patchwork size either 5, 7 or 9: ")
if size in validwindowdowSize:
size = int(size)
counter = counter + 1
else:
print("The size can ONLY be the digits 5, 7 or 9: ")
print("This program requires 3 different colours to create the patchwork")
while len(colourList) < 3:
colour = input("Please enter a colour : ")
if colour.isalpha() and colour in validColours and colour \
not in colourList:
colourList.append(colour)
else:
print("error valid colours are red, green, blue, orange, ",\
"brown and pink. Please use each colour only once")
return(size, colourList)
def drawTriangles(window,vertices,fill):
triangle = Polygon(vertices)
triangle.setFill(fill)
triangle.setOutline("")
triangle.draw(window)
def trianglePatch(window, topLeftX, topLeftY, fill):
anchorPointX = topLeftX -10
for i in range(5):
anchorPointY = topLeftY + (20 * i)
for j in range(6):
anchorPointX = topLeftX-10
if i % 2 != 0:# if i is odd offset
pass
anchorPointX = anchorPointX + (20*j)
if j == 5:
pass
else:
# right handed right angle triangle
left = Point(anchorPointX+10, anchorPointY)
right = Point(anchorPointX + 20, anchorPointY)
bottom = Point(anchorPointX + 10, anchorPointY + 20)
vertices = [left, right, bottom]
drawTriangles(window,vertices,fill)
if j == 0 :
pass
else:
# left handed right angle triangle
left = Point(anchorPointX, anchorPointY)
right = Point(anchorPointX+10, anchorPointY)
bottom = Point(anchorPointX + 10, anchorPointY + 20)
vertices = [left, right, bottom]
drawTriangles(window,vertices,fill)
else:
if j == 5:
pass
else:
anchorPointX = topLeftX + (20*j)
# equilateral triangle
left = Point(anchorPointX, anchorPointY)
right = Point(anchorPointX + 20, anchorPointY)
bottom = Point(anchorPointX + 10, anchorPointY + 20)
vertices = [left, right, bottom]
drawTriangles(window,vertices,fill)
def linesPatch(window, topLeftX, topLeftY, colour):
end = Point(topLeftX + 100, topLeftY + 100)
for x in range(topLeftX,110 + topLeftX, 10):
line = Line(Point(x,topLeftY), end)
end.move(-10,0)
line.setOutline(colour)
line.draw(window)
end = Point(topLeftX + 100, topLeftY + 100)
for y in range(topLeftY,110 + topLeftY, 10):
line = Line(Point(topLeftX,y), end)
end.move(0,-10)
line.setOutline(colour)
line.draw(window)
def drawTrianglePatch(window, rows, coulombs, anchorPointX, anchorPointY,
colourList, section):
for i in range(rows):
topLeftY = anchorPointY + 100*i
for j in range(coulombs):
topLeftX = anchorPointX + 100*j
if section == "right":
colourChoice = 2
else:
colourChoice = colourChoser(colourList, topLeftX,
topLeftY, coulombs, i)
trianglePatch(window, topLeftX, topLeftY, colourList[colourChoice])
def drawLinesPatch(window, rows, coulombs, colourList):
for i in range(rows+1):
topLeftY = rows*100 + 100*i
for j in range(coulombs):
topLeftX = 0 + 100*j
colourChoice = colourChoser(colourList, topLeftX,
topLeftY, coulombs, i)
linesPatch(window, topLeftX, topLeftY, colourList[colourChoice])
def drawFullPatchwork(size, colourList):
window = GraphWin("Patchwork", size*100, size*100)
# loop controls and the top of the patturn
coulombs = size
rows = int(size - size*0.45)
anchorPointX = 0
anchorPointY = 0
# draw the patchwork for the lower left of the screen
coulombs = coulombs - rows
drawLinesPatch(window, rows, coulombs, colourList)
# draw the patchwork for the top section of the screen
coulombs = size
section = "top"
drawTrianglePatch(window, rows, coulombs, anchorPointX, anchorPointY,
colourList,section)
# draw the patchwork for the lower right section of the screen
anchorPointY = rows*100
rows = int(size - size*0.45) + 1
coulombs = coulombs - rows
anchorPointX = rows * 100
section = "right"
drawTrianglePatch(window, rows, coulombs, anchorPointX, anchorPointY,
colourList, section)
return(window)
def colourChoser(colourList, topLeftX, topLeftY, coulombs, i):
colourChoice = 1
if topLeftX < (coulombs*100 - (100*i))-100:
colourChoice = 0
elif topLeftX > (coulombs*100 - (100*i))-100:
colourChoice = 2
else:
colourChoice = 1
return (colourChoice)
def redrawer(mouseX, mouseY, listX, window, size, colourList, section):
simulatedI = int(math.floor(mouseY/100))
listX.append(mouseX)
clickCount = listX.count(mouseX)
if clickCount >= 3:
for i in range(clickCount):
listX.remove(mouseX)
clickCount = 0
startColour = colourChoser(colourList, mouseX, mouseY, size, simulatedI)
finalColour = startColour + clickCount
if finalColour == 4:
finalColour = 1
elif finalColour == 3:
finalColour = 0
if section == "left":
linesPatch(window, mouseX, mouseY, colourList[finalColour])
else:
trianglePatch(window, mouseX, mouseY, colourList[finalColour])
def colourChanger(window, size, colourList):
previousClickX = [[]for i in range(size)]
while not window.isClosed():
mouseClickPoint = window.getMouse()
mouseX = mouseClickPoint.getX()
mouseY = mouseClickPoint.getY()
if mouseX >=100*size or mouseY >= size*100:
print("oops! Thats off the grid! Try again a little closer in")
elif mouseY <= int(size -(size*0.45))*100:
mouseX = int(mouseX/100)*100
mouseY = int(mouseY/100)*100
section = "top"
listLocation = int(mouseY/100)
listX = previousClickX[listLocation]
redrawer(mouseX, mouseY, listX, window, size, colourList, section)
elif mouseY >= int(size -(size*0.45))*100 \
and mouseX >=int(size+1 -(size*0.45))*100 :
mouseX = int(mouseX/100)*100
mouseY = int(mouseY/100)*100
section = "right"
listLocation = int(mouseY/100)
listX = previousClickX[listLocation]
redrawer(mouseX, mouseY, listX, window, size, colourList, section)
else:
mouseX = int(mouseX/100)*100
mouseY = int(mouseY/100)*100
section = "left"
listLocation = int(mouseY/100)
listX = previousClickX[listLocation]
redrawer(mouseX, mouseY, listX, window, size, colourList, section)
main()