Printing co-ordinates

22 views Asked by At

I have the code below but the output doesn't give me co-ordinates, just [<GridNode(0:0 0x106fdadb0)> as an example, not (0,0). How do I just print co-ordinates?

from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.diagonal_movement import DiagonalMovement


matrix = [
[1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]

]

grid = Grid(matrix= matrix)


start = grid.node(0,0)              # start in top left hand corner
end = grid.node(5,2)                # end in bottom right hand corner

finder = AStarFinder(diagonal_movement=DiagonalMovement.always)              



path, runs = finder.find_path(start, end, grid)  # returns two things


print(path)
print(runs)                # ran through 17 times to find the shortest path
1

There are 1 answers

0
Mudkip.wtf On

You should iterate through the path list and convert each GridNode object to its corresponding coordinates. See the code below:

matrix = [
    [1, 1, 1, 1, 1, 1],
    [1, 0, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1]
]

grid = Grid(matrix=matrix)

start = grid.node(0, 0)  # start in top left hand corner
end = grid.node(5, 2)    # end in bottom right hand corner

finder = AStarFinder(diagonal_movement=DiagonalMovement.always)

path, runs = finder.find_path(start, end, grid)  # returns two things

# Convert GridNode objects to coordinates and print them
coordinates = [(node.x, node.y) for node in path]
print(coordinates)

# Print the number of runs
print(runs)

Output:

[(0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (5, 2)]
10