求空间直线的交点.docx
- 文档编号:3112691
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:8
- 大小:15.48KB
求空间直线的交点.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)];
%calculatethesimplestRowechelonmatrix
temp1=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:
3
if(i~=index
(1)&&i~=index
(2))
index
(3)=i;
end
end
p(index
(1))=X
(1);
p(index
(2))=X
(2);
p(index
(3))=0;
else
n=[];
p=[];
end
%-----------fortest------------------
%[flagnp]=Plane2Line([123],[101],[234],[0-10])%[flagnp]=Plane2Line([001],[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
(2)n2
(3);];
if(det(A)~=0)
flag=0;
else
if(rank(A(2:
3,:
))<2)
flag=1;
else
flag=2;
end
end
if(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')
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 空间 直线 交点