Skip to content

Commit 8652bf5

Browse files
committed
change from int to float
1 parent a61b67f commit 8652bf5

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

pathfinding/core/grid.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def build_nodes(width, height, matrix=None, inverse=False):
2525
# while others will be obstacles
2626
# if inverse is False, otherwise
2727
# it changes
28-
weight = int(matrix[y][x]) if use_matrix else 1
29-
walkable = weight <= 0 if inverse else weight >= 1
28+
weight = float(matrix[y][x]) if use_matrix else 1
29+
walkable = weight <= 0 if inverse else weight != 0
3030

3131
nodes[y].append(Node(x=x, y=y, walkable=walkable, weight=weight))
3232
return nodes
@@ -42,8 +42,8 @@ def __init__(self, width=0, height=0, matrix=None, inverse=False):
4242
self.passable_left_right_border = False
4343
self.passable_up_down_border = False
4444
if isinstance(matrix, (tuple, list)) or (
45-
USE_NUMPY and isinstance(matrix, np.ndarray) and
46-
matrix.size > 0):
45+
USE_NUMPY and isinstance(matrix, np.ndarray)
46+
and matrix.size > 0):
4747
self.height = len(matrix)
4848
self.width = self.width = len(matrix[0]) if self.height > 0 else 0
4949
if self.width > 0 and self.height > 0:
@@ -58,17 +58,17 @@ def set_passable_up_down_border(self):
5858
self.passable_up_down_border = True
5959

6060
def node(self, x, y):
61-
"""
62-
get node at position
61+
"""Get node at position.
62+
6363
:param x: x pos
6464
:param y: y pos
6565
:return:
6666
"""
6767
return self.nodes[y][x]
6868

6969
def inside(self, x, y):
70-
"""
71-
check, if field position is inside map
70+
"""Check, if field position is inside map.
71+
7272
:param x: x pos
7373
:param y: y pos
7474
:return:
@@ -82,9 +82,9 @@ def walkable(self, x, y):
8282
return self.inside(x, y) and self.nodes[y][x].walkable
8383

8484
def neighbors(self, node, diagonal_movement=DiagonalMovement.never):
85-
"""
86-
get all neighbors of one node
87-
:param node: node
85+
"""Get all neighbors of one node.
86+
87+
:param node: current node that we like to get the neighbors of.
8888
"""
8989
x = node.x
9090
y = node.y
@@ -171,8 +171,7 @@ def grid_str(self, path=None, start=None, end=None,
171171
border=True, start_chr='s', end_chr='e',
172172
path_chr='x', empty_chr=' ', block_chr='#',
173173
show_weight=False):
174-
"""
175-
create a printable string from the grid using ASCII characters
174+
"""Create a printable string from the grid using ASCII characters.
176175
177176
:param path: list of nodes that show the path
178177
:param start: start node
@@ -202,7 +201,12 @@ def grid_str(self, path=None, start=None, end=None,
202201
line += path_chr
203202
elif node.walkable:
204203
# empty field
205-
weight = str(node.weight) if node.weight < 10 else '+'
204+
if node.weight > 10:
205+
weight = '+' # weight bigger than 10
206+
elif node.weight < 1 and node.weight > 0:
207+
weight = '-' # weight between 0 and 1
208+
else:
209+
weight = str(int(node.weight))
206210
line += weight if show_weight else empty_chr
207211
else:
208212
line += block_chr # blocked field

pathfinding/finder/finder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def calc_cost(self, node_a, node_b):
6060
"""
6161
if node_b.x - node_a.x == 0 or node_b.y - node_a.y == 0:
6262
# direct neighbor - distance is 1
63-
ng = 1
63+
ng = 1.0
6464
else:
6565
# not a direct neighbor - diagonal movement
6666
ng = SQRT2
@@ -120,6 +120,7 @@ def process_node(self, node, parent, end, open_list, open_value=True):
120120
'''
121121
# calculate cost from current node (parent) to the next node (neighbor)
122122
ng = self.calc_cost(parent, node)
123+
print(node.x, node.y, ng, node.g)
123124

124125
if not node.opened or ng < node.g:
125126
node.g = ng

test/path_test_scenarios.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,23 @@
8080
"expectedDiagonalLength": 12,
8181
"inverse": false,
8282
"weighted": true
83+
},
84+
{
85+
"startX": 0,
86+
"startY": 0,
87+
"endX": 4,
88+
"endY": 0,
89+
"matrix": [
90+
[0.01, 0.02, 0.99, 0.09, 0.01],
91+
[0.01, 0.02, 0, 0, 0.01],
92+
[0.01, 0.01, 0, 0.01, 0.01],
93+
[0.08, 0.03, 0, 0.01, 0.01],
94+
[0.01, 0.01, 0, 0, 0.01],
95+
[0.01, 0.01, 0.01, 0.01, 0.01]
96+
],
97+
"expectedLength": 15,
98+
"expectedDiagonalLength": 12,
99+
"inverse": false,
100+
"weighted": true
83101
}
84102
]

0 commit comments

Comments
 (0)