(Python)在栅格地图中实现A*算法(2)

前言

上篇文章讲的是在栅格地图中实现A*算法的静态展示,本期文章实现动画的形式展示。

关于A*算法的基本原理和静态展示(附代码),想了解的朋友可以去看我的上篇文章。

A*算法的动态实现 

1. 模块导入

from matplotlib.animation import FuncAnimation

在原来的基础上添加 from matplotlib.animation import FuncAnimation。

FuncAnimation 是一个非常有用的工具,用于创建动画,它通过在给定的图形对象上重复调用一个函数来实现动画效果。

 2. A*算法函数和类

编写的函数和类与静态的时候一样,这里不做过多赘述。

3.  动画函数

# 初始化动画
    def update(frame_number):
        if frame_number < len(tmp):
            # 将路径点设置为紫色
            colored_map[tmp[frame_number].x][tmp[frame_number].y] = [255,0,255]
            ax.clear()
            ax.imshow(colored_map)
            #设置网格和坐标轴
            ax.set_xticks(np.arange(0.5, mapp.shape[0], 1))
            ax.set_yticks(np.arange(0.5, mapp.shape[1], 1))
            # 添加网格线,alpha 设置网格线的透明度
            ax.grid(which='major', axis='both', linestyle='-', linewidth=1, color='k', alpha=0.5)
            # 显示起点和终点
            plt.scatter([start_x * 1], [start_y * 1], c='red', s=50)  # 起点
            plt.scatter([target_x * 1], [target_y * 1], c='blue', s=50)  # 终点
        else:
            print("动画结束")

这里用if语句判断动画的显示,tmp代表算法寻路路径(由一串坐标点组成的列表,其中的每一个点表示一帧图像)。

    tmp_tuples = [(position.x, position.y) for position in tmp]
    print("tmp:",tmp_tuples)
将轨迹点的路径转换成坐标-列表的形式。
 # 创建动画
    ani = FuncAnimation(fig, update, frames=len(tmp), interval=100, repeat=False)
    # 显示动画
    plt.show()

4. 输出结果

tmp: [(0, 0), (1, 1), (2, 2), (3, 2), (4, 2), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3), (12, 3), (13, 4), (14, 5), (15, 6), (15, 7), (15, 8), (15, 9), (15, 10), (15, 11), (16, 12), (17, 13), (18, 14), (19, 15), (19, 16), (19, 17), (19, 18), (19, 19)]
 

图像如下:

PS:后续我会更新基于A*算法的全覆盖路径规划,想了解的小伙伴欢迎前来! 

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于栅格地图的A*算法Python代码示例: ```python import heapq class Grid: def __init__(self, width, height): self.width = width self.height = height self.walls = set() def in_bounds(self, pos): x, y = pos return 0 <= x < self.width and 0 <= y < self.height def passable(self, pos): return pos not in self.walls def neighbors(self, pos): x, y = pos results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)] if (x + y) % 2 == 0: results.reverse() # aesthetics results = filter(self.in_bounds, results) results = filter(self.passable, results) return results def cost(self, current, next): return 1 def heuristic(a, b): (x1, y1) = a (x2, y2) = b return abs(x1 - x2) + abs(y1 - y2) def a_star_search(graph, start, goal): frontier = PriorityQueue() frontier.put(start, 0) came_from = {} cost_so_far = {} came_from[start] = None cost_so_far[start] = 0 while not frontier.empty(): current = frontier.get() if current == goal: break for next in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current, next) if next not in cost_so_far or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost + heuristic(goal, next) frontier.put(next, priority) came_from[next] = current return came_from, cost_so_far class PriorityQueue: def __init__(self): self.elements = [] def empty(self): return len(self.elements) == 0 def put(self, item, priority): heapq.heappush(self.elements, (priority, item)) def get(self): return heapq.heappop(self.elements)[1] # Example usage: # g = Grid(10, 10) # g.walls = [(1, 1), (2, 1), (3, 1), (3, 2), (3, 3), (2, 3)] # came_from, cost_so_far = a_star_search(g, (1, 2), (7, 8)) # print(came_from) # print(cost_so_far) ``` 这个代码示例实现了一个基于栅格地图的A*算法,可以用于寻找两个点之间的最短路径。你可以根据自己的需要修改代码来适应不同的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值