网格中两点之间的最短路径(Matlab)
我试图找到网格中两点之间没有障碍的最短路径,并向各个方向移动(N NE E ES S SW W WN)。
这似乎是一个常见的任务......这不是已经在Matlab中实现了吗?当Matlab绘制由一条线连接的两个点(plot(X,Y,'-'))时,似乎在内部进行此计算,因为我猜测生成的图像也是一个网格。
示例:从 [1,1] 到 [3,6] 的一个解是 [1,1; 2,2; 2,3; 2,4; 3,5; 3,6]
我已经尝试过:
dist_x = length(linspace(p1(1),p2(1), dist(p1(1),p2(1))+1));
dist_y = length(linspace(p1(2),p2(2), dist(p1(2),p2(2))+1));
num_points = max(dist_x, dist_y);
x = round(linspace(p1(1),p2(1),num_points));
y = round(linspace(p1(2),p2(2),num_points));
但我认为它返回的分数比应有的多,也许有一个已实施的例程。
多谢
I'm trying to find the shortest path between two points in a grid with no obstacles and move in all directions (N NE E ES S SW W WN).
It seems to be a common task... Is this not implemented already in Matlab? When Matlab plots two points joined by a line ( plot(X,Y,'-') ) seems to internally do this calculation as I guess that the generated image is a grid too.
Example: From [1,1] to [3,6] one solution is [1,1; 2,2; 2,3; 2,4; 3,5; 3,6]
I have tried:
dist_x = length(linspace(p1(1),p2(1), dist(p1(1),p2(1))+1));
dist_y = length(linspace(p1(2),p2(2), dist(p1(2),p2(2))+1));
num_points = max(dist_x, dist_y);
x = round(linspace(p1(1),p2(1),num_points));
y = round(linspace(p1(2),p2(2),num_points));
But I think that it returns more points than it should and maybe there is an implemented routine.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案(由 JF Sebastian 给出)是 布雷森汉姆线算法。
The solution (given by J.F. Sebastian) is the Bresenham Line Algorithm.