《2022年求空间直线的交 .pdf》由会员分享,可在线阅读,更多相关《2022年求空间直线的交 .pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、因为一个小任务需要用到求空间直线交点的MATLAB 函数和求空间中两个平面的相交线的函数, 但是在网上找了一下没有,只好自己写了几个函数,自己觉得还能用,在这里给大家分享一下。1.MATLAB求空间中的两个平面的相交线function flag,n,p = PlanePlane2Line(n1,p1,n2,p2)%-% calulate the line shared by two intersecting plane% input: % n1 normal vector of plane one% p1 any point on plane one% n2 normal vector of
2、plane two% p2 any point on plane two% output:% flag whether the two planes are intersecting ( 1 or 0)% n the direction vector of the expected line% p any point in the expected line% author: Lai Zhenzhou from Harbin Institute of Technology% email: % date: 2014.1.16%-if(isvector(n1) & isvector(p1) & i
3、svector(n2) & isvector(p2) error(PlanePlane2Line:the parameter is not vector); endif(length(n1)=3)|(length(p1)=3)|(length(n2)=3)|(length(p2)=3) error(PlanePlane2Line:the parameter is not 3d vector); endA = n1(1) n1(2) n1(3);n2(1) n2(2) n2(3);if(rank(A)2) flag = 0;else flag = 1;endif(flag=1)名师资料总结 -
4、- -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - % calculate the normal vector n = cross(n1,n2); c1 = n1(1) n1(2) n1(3) -dot(n1,p1); c2 = n2(1) n2(2) n2(3) -dot(n2,p2); % calculate the simplest Row echelon matrix temp1 = rref(A); temp2 = 1 2 3; index
5、(1) = find(temp1(1,:),1,first); % the first nonzero element index(2) = find(temp1(2,:),1,first); % the first nonzero element D = A(:,index(1) A(:,index(2); Y = dot(n1,p1);dot(n2,p2); X = inv(D)*Y;for i=1:3if(i=index(1) & i=index(2) index(3) = i;endend p(index(1) = X(1); p(index(2) = X(2); p(index(3)
6、 = 0;else n=; p=;end%-for test-% flag n p = PlanePlane2Line(1 2 3,1 0 1,2 3 4,0 -1 0)% flag n p = PlanePlane2Line(0 0 1,0.5 0.5 0.5,1 0 0,0 0 1)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - 2.MATLAB求空间中两条直线的交点function flag,p = LineLine2Po
7、int(n1,p1,n2,p2)%-% determine the relation between two straight lines and% calulate the intersection point if they are intersecting% input:% n1 direction vector of line one% p1 any point in line one% n2 direction vector of line two% p2 any point in line two% output:% flag the relation of the two lin
8、e% flag = 0 the two line are on different plane % flag = 1 the two line are on the same plane and they are parallel% flag = 2 the two line are on the same plane and they are intersecting% p the point shared by the two intersecting line% author: Lai Zhenzhou from Harbin Institute of Technology% email
9、: % date: 2014.1.17% reference: http:/ & isvector(p1) & isvector(n2) & isvector(p2) error(LineLine2Point: the parameter is not vector); endif(length(n1)=3)|(length(p1)=3)|(length(n2)=3)|(length(p2)=3) error(LineLine2Point: the parameter is not 3d vector); endA = p2(1)-p1(1) p2(2)-p1(2) p2(3)-p1(3);
10、n1(1) n1(2) n1(3) ; n2(1) n2(2) n2(3) ;if(det(A)=0)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - flag = 0;elseif(rank(A(2:3,:)2) flag = 1;else flag = 2;endendif(flag = 2) B = rref(A(2:3,:); index(1) = find(B(1,:),1,first); index(2) = find
11、(B(2,:),1,first);for i=1:3if(i=index(1) & i=index(2) index(3) = i;endend 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
12、 p = ;endend%-for test-% flag p = LineLine2Point(1 0 0,0 0 0,0 1 0,1 1 0)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - % flag p = LineLine2Point(1 1 0,0 0 0,0 1 0,1 1 0)% flag p = LineLine2Point(1 1 -1,0 0 1,0 1 0,1 1 0)% flag p = LineLin
13、e2Point(1 1 -1,0 0 1,1 1 -1,1 1 0)% flag p = LineLine2Point(1 1 1,0 0 0,2 0 0,2 1 1)% flag p = LineLine2Point( 0 -42050 -21025,-100 0 0,0 0 145,0 0 0)3.MATLAB求空间中直线和某一线段的交点function flag,p = LineSegment2Point(n1,p1,p21,p22)%-% determine the relation between a straight line and% a line segment and cal
14、ulate the intersecting point% if they are intersecting% input:% n1 direction vector of the straight line % p1 any point in the straight line% p21 any point in the line segment% p22 any another different point in the line segment% output:% flag the relation of the two line% flag = 0 the straight line
15、 and line segment are not intersecting% flag = 1 the straight line and line segment are intersecting% p the point shared by the two intersecting line% author: Lai Zhenzhou from Harbin Institute of Technology% email: % date: 2014.1.17%-if(isvector(n1) & isvector(p1) & isvector(p21) & isvector(p22) er
16、ror(LineSegment2Point: the parameter is not vector); endif(length(n1)=3)|(length(p1)=3)|(length(p21)=3)|(length(p22)=3) error(LineSegment2Point: the parameter is not 3d vector); endif(p21(1)=p22(1)&(p21(2)=p22(2)&(p21(3)=p22(3) error(LineSegment2Point: the two appointed points of line segment are 名师
17、资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - the same); end%calculate the direction vector of line segmentn2 = p22(1)-p21(1),p22(2)-p21(2),p22(3)-p21(3);flag0,p0 = LineLine2Point(n1,p1,n2,p21);if(flag0 = 2)if(norm(p0-p21)=norm(p22-p21) & n
18、orm(p0-p22)=norm(p22-p21) flag = 1; p = p0;else flag = 0; p=;endelse flag = 0; p=;endend%-for test-% flag p = LineSegment2Point(0 0 1,0 0 0,0 1 1,0 2 1)% flag p = LineSegment2Point(1 1 1,0 0 0,0 1 1,0 2 1)% flag p = LineSegment2Point(1 1 1,0 0 0,0 1 1,1 1 1)% flag p = LineSegment2Point(1 1 1,0 0 0,0 1 1,2 1 1)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -