-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_functions.py
105 lines (90 loc) · 4.05 KB
/
check_functions.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
# Checks if a mouse action occurred inside a rectangle (rects must be aligned by their center)
def isWithinRect(centerX, centerY, width, height, mouseX, mouseY):
if (centerX - width//2 <= mouseX <= centerX + width//2) and (centerY - height//2 <= mouseY <= centerY + height//2):
return True
return False
# Checks if a mouse action occurred inside a rectangle (rects aligned by their top left)
def isWithinRectTopLeft(topLeftX, topLeftY, width, height, mouseX, mouseY):
if (topLeftX <= mouseX <= topLeftX + width) and (topLeftY <= mouseY <= topLeftY + height):
return True
return False
# Checks if a mouse action occurred inside a rectangle (rects aligned by their left-side)
def isWithinRectLeft(topLeftX, topLeftY, width, height, mouseX, mouseY):
if (topLeftX <= mouseX <= topLeftX + width) and (topLeftY - height//2 <= mouseY <= topLeftY + height//2):
return True
return False
# Clear a text file
def clearFile(fileName):
file = open(fileName, 'w')
return
# Write asset object info to a file
def writeObjectsAndAttributes(fileName, objectName, position, radius, color, borderColor):
file = open(fileName, 'a')
file.write(str(["NAME:"+objectName, "POSITION:("+str(position[0])+"|"+str(position[1])+")", "RADIUS:"+str(radius), "COLOR:"+str(color),
"BORDERCOLOR:"+str(borderColor)]) + ' . ')
file.close()
# Write a line to a file
def writeLine(fileName, text):
clearFile(fileName)
file = open(fileName, 'a')
file.write(str(text))
file.close()
def readLine(fileName):
file = open(fileName, 'r')
contents = file.read()
file.close()
return contents
def writeLineCoord(fileName, coord):
file = open(fileName, 'a')
file.write(str(coord) + ' . ')
file.close()
# Read path coordinates
def readLineCoords(fileName):
file = open(fileName, 'r')
contents = file.read()
coordsList = []
for stringCoord in contents.split(" . "):
if not stringCoord.isspace() and stringCoord != '':
left = stringCoord.find("(")
comma = stringCoord.find(",")
right = stringCoord.find(")")
x, y = int(stringCoord[left+1:comma]), int(stringCoord[comma+1:right])
coordsList.append((x, y))
return coordsList
# Gets the distance between two coordinates
def getDistance(coord1, coord2):
coord1X, coord1Y = coord1
coord2X, coord2Y = coord2
distance = ((coord1X - coord2X)**2 + (coord1Y - coord2Y)**2)**0.5
return distance
def getSlope(coord1, coord2):
coord1X, coord1Y = coord1
coord2X, coord2Y = coord2
return (coord2Y-coord1Y)/(coord2X-coord1X)
# Decode asset file and create a list of individual asset information
def getAssetsFromFile(fileName):
file = open(fileName, 'r')
contents = file.read()
assets = []
for assetInfo in contents.split(" . "):
if not assetInfo.isspace() and assetInfo != '':
left = assetInfo.find("[")
right = assetInfo.find("]")
for parameter in assetInfo.split(","):
if "NAME:" in parameter:
assetName = parameter[parameter.find(":")+1:len(parameter)-1]
elif "POSITION:" in parameter:
assetPosition = parameter[parameter.find(":")+1:len(parameter)-1]
left = assetPosition.find("(")
comma = assetPosition.find("|")
right = assetPosition.find(")")
x, y = int(assetPosition[left + 1:comma]), int(assetPosition[comma + 1:right])
assetPosition = (x, y)
elif "RADIUS:" in parameter:
assetRadius = int(parameter[parameter.find(":") + 1:len(parameter) - 1])
elif "BORDERCOLOR:" in parameter:
assetBorderColor = parameter[parameter.find(":")+1:len(parameter)-2]
elif "COLOR:" in parameter:
assetColor = parameter[parameter.find(":")+1:len(parameter)-1]
assets.append((assetName, assetPosition, assetRadius, assetColor, assetBorderColor))
return assets