求空间直线的交点.docx
《求空间直线的交点.docx》由会员分享,可在线阅读,更多相关《求空间直线的交点.docx(8页珍藏版)》请在冰豆网上搜索。
求空间直线的交点
因为一个小任务需要用到求空间直线交点的MATLAB函数和求空间中两个
平面的相交线的函数,但是在网上找了一下没有,只好自己写了几个函数,自己觉得还能用,在这里给大家分享一下。
1.MATLAB求空间中的两个平面的相交线
function[flag,n,p]=Plane2Line(n1,p1,n2,p2)
%
%calulatethelinesharedbytwointersectingplane
%input:
%n1normalvectorofplaneone
%p1anypointonplaneone
%n2normalvectorofplanetwo
%p2anypointonplanetwo%%output:
%flagwhetherthetwoplanesareintersecting(1or0)
%nthedirectionvectoroftheexpectedline
%panypointintheexpectedline%%author:
LaiZhenzhoufromHarbinInstituteofTechnology
%email:
%date:
2014.1.16
%
if(~(isvector(n1)&&isvector(p1)&&isvector(n2)&&
isvector(p2)))error('Plane2Line:
theparameterisnotvector');
end
if((length(n1)~=3)||(length(p1)~=3)||(length(n2)~=3)||(length(p2)~=3))error('Plane2Line:
theparameterisnot3dvector');
end
A=[n1
(1)n1
(2)n1
(3);n2
(1)n2
(2)n2
(3)];
if(rank(A)<2)
flag=0;
else
flag=1;
end
if(flag==1)
%calculatethenormalvector
n=cross(n1,n2);
c1=[n1
(1)n1
(2)n1
(3)-dot(n1,p1)];
c2=[n2
(1)n2
(2)n2
(3)-dot(n2,p2)];
%calculatethesimplestRowechelonmatrixtemp1=rref(A);
temp2=[123];
index
(1)=find(temp1(1,:
),1,'first');%thefirstnonzeroelementindex
(2)=find(temp1(2,:
),1,'first');%thefirstnonzeroelementD=[A(:
index
(1))A(:
index
(2))];
Y=[dot(n1,p1);dot(n2,p2)];
X=inv(D)*Y;fori=1:
3if(i~=index
(1)&&i~=index
(2))index
(3)=i;
end
endp(index
(1))=X
(1);p(index
(2))=X
(2);p(index
(3))=0;elsen=[];
p=[];end
fortest
%[flagnp]=Plane2Line([123],[101],[234],[0-10])%[flagnp]=
Plane2Line([O01],[0.50.50.5],[100],[001])2.MATLAB求空间中两条直线的交点
function[flag,p]=Line2Point(n1,p1,n2,p2)
%
%determinetherelationbetweentwostraightlinesand
%calulatetheintersectionpointiftheyareintersecting%%input:
%n1directionvectoroflineone
%p1anypointinlineone
%n2directionvectoroflinetwo
%p2anypointinlinetwo%%output:
%flagtherelationofthetwoline
%flag=0thetwolineareondifferentplane
%flag=1thetwolineareonthesameplaneandtheyareparallel%
flag=2thetwolineareonthesameplaneandtheyareintersecting
%pthepointsharedbythetwointersectingline%%author:
LaiZhenzhoufromHarbinInstituteofTechnology
%email:
%date:
2014.1.17
%reference:
http:
%
if(~(isvector(n1)&&isvector(p1)&&isvector(n2)&&isvector(p2)))error('Line2Point:
theparameterisnotvector');
end
if((length(n1)~=3)||(length(p1)~=3)||(length(n2)~=3)||(length(p2)~=3))
error('Line2Point:
theparameterisnot3dvector');
end
A=[p2
(1)-p1
(1)p2
(2)-p1
(2)p2
(3)-p1
(3);
n1
(1)n1
(2)n1
(3);
n2
(1)n2
n2
(2)
(3);];if(det(A)~=0)flag=0;elseif(rank(A(2:
3,:
))<2)flag=1;elseflag=2;endendif(flag==2)B=rref(A(2:
3,:
));
index
(1)=find(B(1,:
),1,'first');index
(2)=find(B(2,:
),1,'first');
fori=1:
3
if(i~=index
(1)&&i~=index
(2))
index
(3)=i;
end
end
Y(1,1)=-p1(index
(1))+p2(index
(1));
Y(2,1)=-p1(index
(2))+p2(index
(2));
D=[n1(index
(1))-n2(index
(1));n1(index
(2))-n2(index
(2))];t=inv(D)*Y;p
(1)=p1
(1)+n1
(1)*t
(1);p
(2)=p1
(2)+n1
(2)*t
(1);p(3)=p1
(3)+n1
(3)*t
(1);
else
p=[];
end
end
%fortest
%[flagp]=Line2Point([100],[000],[010],[110])%[flagp]=Line2Point([110],[000],[010],[110])
%[flagp]=Line2Point([11-1],[001],[010],[110])%[flagp]=Line2Point([11-1],[001],[11-1],[110])%[flagp]=Line2Point([111],[000],[200],[211])
%[flagp]=Line2Point([0-42050-21025],[-100],[00145],[000])
3.MATLAB求空间中直线和某一线段的交点
function[flag,p]=LineSegment2Point(n1,p1,p21,p22)
%
%determinetherelationbetweenastraightlineand
%alinesegmentandcalulatetheintersectingpoint
%iftheyareintersecting%%input:
%n1directionvectorofthestraightline
%p1anypointinthestraightline
%p21anypointinthelinesegment
%p22anyanotherdifferentpointinthelinesegment%%output:
%flagtherelationofthetwoline
%flag=0thestraightlineandlinesegmentarenotintersecting%flag=1thestraightlineandlinesegmentareintersecting%pthepointsharedbythetwointersectingline%%author:
LaiZhenzhoufromHarbinInstituteofTechnology
%email:
%date:
2014.1.17
%
if(~(isvector(n1)&&isvector(p1)&&isvector(p21)&&isvector(p22)))error('LineSegment2Point:
theparameterisnotvector');
end
if((length(n1)~=3)||(length(p1)~=3)||(length(p21)~=3)||(length(p22)~=3))
error('LineSegment2Point:
theparameterisnot3dvector');end
if((p21
(1)==p22
(1))&&(p21
(2)==p22
(2))&&(p21
(3)==p22
(3)))
error('LineSegment2Point:
thetwoappointedpointsoflinesegmentarethesame');
end
%calculatethedirectionvectoroflinesegment
n2=[p22
(1)-p21
(1),p22
(2)-p21
(2),p22
(3)-p21
(3)];
[flag0,p0]=Line2Point(n1,p1,n2,p21);
if(flag0==2)
if(norm(p0-p21)<=norm(p22-p21)&&norm(p0-p22)<=norm(p22-p21))flag=1;p=p0;
else
flag=0;
p=[];
end
else
flag=0;
p=[];
end
end
%fortest
%[flagp]=LineSegment2Point([001],[000],[011],[021])%[flagp]=LineSegment2Point([111],[000],[011],[021])%[flagp]=LineSegment2Point([111],[000],[011],[111])%[flagp]=LineSegment2Point([111],[000],[011],[211])