打印坐标

-1 投票
1 回答
19 浏览
提问于 2025-04-14 16:51

我有下面的代码,但是输出的结果并没有给我坐标,只是像这样显示 [<GridNode(0:0 0x106fdadb0)>,而不是 (0,0)。我该怎么做才能只打印出坐标呢?

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 个回答

0

你需要遍历路径列表,把每个 GridNode 对象转换成它对应的坐标。下面是代码示例:

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)

输出结果:

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

撰写回答