-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
40 lines (36 loc) · 1.24 KB
/
test.py
File metadata and controls
40 lines (36 loc) · 1.24 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
import pathfinding_python as pathfinder
from math import *
def make_circle(center_x, center_y, radius, num_points=20):
points = []
for i in range(num_points):
angle = 2 * pi * i / num_points
x = center_x + radius * cos(angle)
y = center_y + radius * sin(angle)
points.append(pathfinder.Point(x, y))
return points
circle = make_circle(50, 50, 40, 100)
a = pathfinder.Point(1, 1)
b = pathfinder.Point(3, 4)
c = pathfinder.Point(1, 5)
polygon1 = [a, b, c]
polygons = [circle]
cost_zone1 = (polygon1, 1)
cost_zones = [cost_zone1]
vg = pathfinder.constructVizGraph(polygons, cost_zones)
solution = vg.search(0, 0, 100, 100)
print("Pathfinding in python, To get from (0, 0) to (9, 9) take the following path:")
for point in solution:
print(f"{point.x} | {point.y}")
def plot_polygons_and_path(polygons, path):
import matplotlib.pyplot as plt
for polygon in polygons:
xs = [p.x for p in polygon] + [polygon[0].x]
ys = [p.y for p in polygon] + [polygon[0].y]
plt.plot(xs, ys, 'b-')
if path:
path_xs = [p.x for p in path]
path_ys = [p.y for p in path]
plt.plot(path_xs, path_ys, 'r-')
plt.axis('equal')
plt.show()
plot_polygons_and_path(polygons, solution)