根据
Matlab计算两点直线经过的栅格坐标_matlab提取两点之间的格栅_saoyu599的博客-CSDN博客
改为三维
判断线段是否经过空间中某单元
function [point, area_p] = findblock3(start,goal,area_block)
point = [];
dis = 0.2; % 点到直线距离的约束项
x_i = 1;
y_i = 1;
z_i = 1;
if goal(1)<start(1)
x_i = -1;
end
if goal(2)<start(2)
y_i = -1;
end
if goal(3)<start(3)
z_i = -1;
end
for i = start(1):x_i:goal(1)
for j = start(2):y_i:goal(2)
for k = start(3):z_i:goal(3)
p = [i,j,k]; %当前需要验证的点
if area_block(i,j,k) == 0
d = abs(cross(goal-start,p-start)/norm(goal-start));
if d < dis
area_block(i,j,k) = 1;
point = [point;i,j,k];
end
end
end
end
end
area_p = area_block;
end
理论详见:
Point-Line Distance--3-Dimensional -- from Wolfram MathWorld