因为在网上找了很久都没有找到合适的,所以自己写了一个,效率可能没有那么高,但是能用
我们可以将两点看作长方体顶点,其直线就是长方体的对角线,如果长方体内栅格中心点到对角线的距离小于栅格的对角线长度的二分之一,那直线就经过了这个栅格。
代码如下:
function [point] = findblock(start,goal)
point = [];
dis = 0.7071; % 一半的根号二
for i = start(1):goal(1)
for j = start(2):goal(2)
p = [i;j]; %当前需要验证的点
% p点到start和goal点形成直线的距离,goal、start、p都必须为列向量
d = abs(det([goal-start,p-start])/norm(goal-start));
% 如果都是行向量,则是下面这个公式
% d = abs(det([goal-start;p-start])/norm(goal-start));
if d < dis
point = [point;i,j];
end
end
end
end
代码运行结果: