-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.py
140 lines (112 loc) · 3.49 KB
/
sketch.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
#!/usr/bin/python
import tkinter as tk
from random import random
from astar import *
# two points as user input (source cell and destination cell)
points = 2
# windows size (height x width)
height = 500
width = 500
# size of each cell(in pixels) in grid
pixel = 10
# grid (rows x cols)
rows = height // pixel
cols = width // pixel
# show open and closed list
showoc = True
# walls 0 <= x < 1
walldensity = 0.3
class Node(object):
def __init__(self, i, j, walldens=0.2):
self.i = i
self.j = j
self.x = i*pixel + pixel//2
self.y = j*pixel + pixel//2
self.f = 0
self.g = 0
self.h = 0
self.neighbors = []
self.previous = None
self.wall = False
if random() < walldens:
self.wall = True
def show(self, **kwargs):
if not self.wall:
c.create_rectangle(self.i*pixel,
self.j*pixel,
self.i*pixel + pixel,
self.j*pixel + pixel,
**kwargs)
else:
c.create_rectangle(self.i*pixel,
self.j*pixel,
self.i*pixel + pixel,
self.j*pixel + pixel,
fill='black')
def draw_line(self, **kwargs):
if self.previous:
c.create_line(self.previous.x,
self.previous.y,
self.x,
self.y,
**kwargs)
def addNeighbors(self):
i = self.i
j = self.j
if i < rows - 1:
self.neighbors.append(grid[i+1][j])
if j > 0: self.neighbors.append(grid[i+1][j-1])
if j < cols - 1: self.neighbors.append(grid[i+1][j+1])
if i > 0:
self.neighbors.append(grid[i-1][j])
if j > 0: self.neighbors.append(grid[i-1][j-1])
if j < cols - 1: self.neighbors.append(grid[i-1][j+1])
if j < cols - 1: self.neighbors.append(grid[i][j+1])
if j > 0: self.neighbors.append(grid[i][j-1])
def getNeighbors(self):
if len(self.neighbors) > 0:
return self.neighbors
else:
self.addNeighbors()
return self.neighbors
def get_run(event):
# get global values of vars
global points, c, src, dst
# get source vertex
if points == 2:
src = grid[event.x // pixel][event.y // pixel]
# ignore input if its wall
if src.wall:
return
src.show(fill='blue')
points -= 1
# get destination vertex
elif points == 1:
dst = grid[event.x // pixel][event.y // pixel]
if dst.wall:
return
dst.show(fill='green')
points -= 1
# run A* Path Finder
Astar = AStarPathFinder(src, dst)
while True:
ret = Astar.step(showoc=showoc)
# backtrack path from current node
Astar.backtrack()
if ret:
break
# update canvas at each step
c.update()
else: pass
top = tk.Tk()
c = tk.Canvas(top, width=width, height=height)
c.pack()
c.bind("<Button-1>", get_run)
grid = [[Node(i, j, walldensity) for j in range(cols)] for i in range(rows)]
for i in range(rows):
for j in range(cols):
if showoc:
grid[i][j].show(fill='white')
else:
grid[i][j].show(fill='white', outline='')
tk.mainloop()