计算已知空间点P到已知直线L的距离:
已知两点确定了一条空间直线,求出直线的方向向量。计算P点与直线L上其中一点的空间向量,然后计算它与直线方向向量的外积。则点到空间直线的距离就是外积的模与空间直线方向向量的模相除。
以下为简单的MATLAB代码,输入为空间点P,和直线上的两点:
function [ d ] = DisPtToLine( P, CorPts )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
l = [ CorPts(1) - CorPts(4), CorPts(2) - CorPts(5), CorPts(3) - CorPts(6) ];
pl = [ P(1) - CorPts(1), P(2) - CorPts(2), P(3) - CorPts(3) ];
tem = cross(pl, l);
d = norm( tem ) / norm( l );
end
通常要计算点到多条直线的距离,或要计算其中的最小值,则用MATLAB的最优化函数:
fminunc
function [ x, y, z ] = GetIntersection( p1, ps1, p2, ps2 )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
global p1x p1y p1z ps1x ps1y ps1z p2x p2y p2z ps2x ps2y ps2z
p1x = p1(1);