-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
195 lines (150 loc) · 5.92 KB
/
main.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
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
import pygame
from button import *
from a_star import *
from math import *
pygame.font.init()
WIDTH, HEIGHT = 820, 570
WINDOW: pygame.Surface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("PathFinder A*")
buttonsText = ("Start", "End", "Start Node", "End Node", "Node", "Road", "Grab", "Delete")
buttons: list[Button] = []
for i in range(len(buttonsText)):
buttons.append(Button(WINDOW, i*WIDTH/8 + i, buttonsText[i]))
buttons: tuple[Button] = tuple(buttons)
buttons[2].active = True
NODE_SIZE: int = 10
LINE_SIZE: int = 5
aStar: Astar = Astar()
def selectButton() -> None:
for button in buttons:
if button.active:
button.active = False
button.active = button.press()
def addNodes(x, y) -> None:
for button in buttons:
if button.active:
if button.txt == "Start Node":
aStar.addStartNode(x, y)
elif button.txt == "End Node":
aStar.addEndNode(x, y)
elif button.txt == "Node":
aStar.addNode(x, y)
roadNode: list[Node] = []
def addRoad(x, y) -> None:
if len(aStar.nodes) <= 1:
return None
for node in aStar.nodes:
if sqrt(pow(node.x - x, 2) + pow(node.y - y, 2)) <= 10:
if len(roadNode) == 0:
roadNode.append(node)
else:
if roadNode[0] != node:
aStar.createConnection(node, roadNode[0])
del roadNode[0]
grabbedNode: list[Node] = []
def grab():
if pygame.mouse.get_pressed()[0]:
xMouse, yMouse = pygame.mouse.get_pos()
if len(grabbedNode) == 0:
for node in aStar.nodes:
if sqrt(pow(node.x - xMouse, 2) + pow(node.y - yMouse, 2)) <= NODE_SIZE:
grabbedNode.append(node)
if len(grabbedNode) == 1:
grabbedNode[0].x = xMouse
grabbedNode[0].y = yMouse
elif len(grabbedNode) == 1:
del grabbedNode[0]
def delete(x, y) -> None:
for node in aStar.nodes:
if sqrt(pow(x - node.x, 2) + pow(y - node.y, 2)) <= NODE_SIZE:
aStar.removeNode(node)
return
#distance point-line: abs(a*x + b*y + c)/sqrt(pow(a, 2) + pow(b, 2))
for node in aStar.nodes:
for n in node.neighbors:
if (node.x - n.x) != 0:
a = (node.y - n.y)/(node.x - n.x)
else:
a = (node.y - n.y)/((node.x + 0.0001) - n.x) #approssimate the line to a vertical line
c = node.y - a * node.x
if (n.x <= x and node.x >= x) or (node.x <= x and n.x >= x):
if (n.y <= y and node.y >= y) or (node.y <= y and n.y >= y):
if abs(a*x - y + c)/sqrt(pow(a, 2) + 1) <= LINE_SIZE + 5:
aStar.removeConnection(node, n)
return
def endSearch() -> None:
for node in aStar.nodes:
node.state = None
node.path = None
aStar.activeNodes = []
if aStar.startNode != None:
aStar.startNode.state = "start"
aStar.startNode.path = []
aStar.activeNodes.append(aStar.startNode)
if aStar.endNode != None:
aStar.endNode.state = "target"
def update():
if aStar.startNode != None and aStar.endNode != None:
start = buttons[0].active
else:
start = False
if start:
aStar.searchPath()
if buttons[6].active:
grab()
if buttons[1].active:
endSearch()
buttons[1].active = False
def draw():
WINDOW.fill((200, 200, 200))
for b in buttons:
b.draw()
if len(roadNode) == 1:
pygame.draw.line(WINDOW, (0, 0, 0), (roadNode[0].x, roadNode[0].y), (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]), LINE_SIZE)
for node in aStar.nodes:
for n in node.neighbors:
if node.state == "active":
if n.state in ("start", "active", "target"):
pygame.draw.line(WINDOW, (255, 255, 0), (node.x, node.y), (n.x, n.y), LINE_SIZE)
elif node.state == n.state == "path":
pygame.draw.line(WINDOW, (0, 100, 0), (node.x, node.y), (n.x, n.y), LINE_SIZE)
else:
pygame.draw.line(WINDOW, (0, 0, 0), (node.x, node.y), (n.x, n.y), LINE_SIZE)
if aStar.startNode != None:
pygame.draw.circle(WINDOW, (0, 0, 255), (aStar.startNode.x, aStar.startNode.y), NODE_SIZE + 3)
if aStar.endNode != None:
pygame.draw.circle(WINDOW, (255, 0, 0), (aStar.endNode.x, aStar.endNode.y), NODE_SIZE + 3)
for node in aStar.nodes:
if node.state == None:
pygame.draw.circle(WINDOW, (0, 255, 255), (node.x, node.y), NODE_SIZE)
elif node.state == "active":
pygame.draw.circle(WINDOW, (255, 255, 0), (node.x, node.y), NODE_SIZE)
elif node.state == "used":
pygame.draw.circle(WINDOW, (255, 0, 255), (node.x, node.y), NODE_SIZE)
elif node.state == "path":
pygame.draw.circle(WINDOW, (0, 100, 0), (node.x, node.y), NODE_SIZE)
pygame.display.update()
def main():
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[1] > buttons[0].y + buttons[0].height + 10:
if buttons[5].active:
addRoad(event.pos[0], event.pos[1])
elif buttons[7].active:
delete(event.pos[0], event.pos[1])
else:
addNodes(event.pos[0], event.pos[1])
else:
selectButton()
update()
draw()
pygame.quit()
if __name__ == "__main__":
main()