三维空间中直线间距离的计算
目录
- 直线的向量方程
- 程序说明
- 点类
直线的向量方程
三维欧几里得空间中,直线的关系为:相交、平行和异面直线三种情况。设点A、B决定了直线L1L_1L1,点C、D决定了直线L2L_2L2。求直线L1L_1L1和L2L_2L2间的距离可以用以下方法实现。
令直线L1L_1L1的向量方程:x=a+λb,(a、b、x∈R3,λ∈R,a=OA‾,b=AB‾=OB‾−OA‾)x=a+\lambda{b}, ({a、b、x}\in{R^3}, \lambda\in{R}, a=\overline{OA}, b=\overline{AB}=\overline{OB}-\overline{OA})x=a+λb,(a、b、x∈R3,λ∈R,a=OA,b=AB=OB−OA)
这里,aaa是直线L1L_1L1上的一点(向量),bbb是直线L1L_1L1的方向向量。
类似地,令直线L2L_2L2的向量方程:y=c+μd,(c、d、y∈R3,μ∈R,c=OC‾,d=CD‾=OD‾−OC‾)y=c+\mu{d},({c、d、y}\in{R^3}, \mu\in{R}, c=\overline{OC}, d=\overline{CD}=\overline{OD}-\overline{OC})y=c+μd,(c、d、y∈R3,μ∈R,c=OC,d=CD=OD−OC)
同样,ccc是直线L2L_2L2上的一点(向量),ddd是直线L2L_2L2的方向向量。
令:n=b×d∣∣b×d∣∣n=\dfrac{{b}\times{d}}{||{b}\times{d}||}n=∣∣b×d∣∣b×d,则直线间距离:l=∣n⋅(c−a)∣l=|{n}\cdot{(c-a)}|l=∣n⋅(c−a)∣
注:如果∣∣b×d∣∣=0||{b}\times{d}||=0∣∣b×d∣∣=0,则表示直线L1L_1L1和L2L_2L2平行。不能用此公式计算。这时候,只要任取直线L1L_1L1上一点,例如A点,那么点A到直线L2L_2L2的距离就是两平行线L1L_1L1和L2L_2L2间的距离。
程序说明
定义了一个类:点类。定义了向量的基本操作,如类初始化、参数参数的设置和读取、重载了“+”、“-”、“*”运算符实现向量的加、减、叉乘运算、定义了向量的点乘、模等运算。点到点、点到直线、直线到直线的距离在主程序中定义。
点类
定义了向量加(+)、减(-)、叉乘(*)、点乘、向量的模等。
头文件Point.h
#ifndef POINT_H
#define POINT_H#include<iostream>
#include<cmath>class Point
{public:Point(double _x=0.0, double _y=0.0, double _z=0.0):X(_x), Y(_y), Z(_z) {};virtual ~Point() {};Point(const Point &other) { X=other.X; Y=other.Y; Z=other.Z; };Point &operator=(const Point&);Point operator+(const Point&);Point operator+=(const Point&);Point operator-(const Point&);Point operator-(); // opposite numberPoint operator-=(const Point&);Point operator*(const Point&); // cross productfriend std::ostream &operator<<(std::ostream &output, const Point &rhs);double GetX() { return X; }void SetX(double val) { X = val; }double GetY() { return Y; }void SetY(double val) { Y = val; }double GetZ() { return Z; }void SetZ(double val) { Z = val; }double dotX(const Point &pt) { return (pt.X*X+pt.Y*Y+pt.Z*Z); }double mod() { return sqrt(dotX(*this)); }protected:private:double X;double Y;double Z;
};#endif // POINT_H
类的实现:Point.cpp
#include "Point.h"Point &Point::operator=(const Point &rhs)
{if (this == &rhs) return *this; // self assignmentX=rhs.X;Y=rhs.Y;Z=rhs.Z;return *this;
}Point Point::operator+(const Point &rhs)
{return Point(X+rhs.X,Y+rhs.Y,Z+rhs.Z);
}Point Point::operator+=(const Point &rhs)
{X+=rhs.X;Y+=rhs.Y;Z+=rhs.Z;return (*this);
}Point Point::operator-(const Point &rhs)
{return Point(X-rhs.X, Y-rhs.Y, Z-rhs.Z);
}Point Point::operator-() // opposite number
{return Point(-X, -Y, -Z);
}Point Point::operator-=(const Point &rhs)
{X-=rhs.X;Y-=rhs.Y;Z-=rhs.Z;return (*this);
}std::ostream &operator<<(std::ostream &output, const Point &rhs)
{output<<"("<<rhs.X<<","<<rhs.Y<<","<<rhs.Z<<")";return output;
}Point Point::operator*(const Point &pt) // cross product
{double x1,x2,y1,y2,z1,z2,vx,vy,vz;x1=X;x2=pt.X;y1=Y;y2=pt.Y;z1=Z;z2=pt.Z;vx=y1*z2-y2*z1;vy=-x1*z2+x2*z1;vz=x1*y2-x2*y1;return Point(vx,vy,vz);
}
主程序:main.cpp
#include <iostream>
#include <cmath>
#include <fstream>
#include "Point.h"using namespace std;const double EPS=1e-10; // zerodouble dist(Point &pt1, Point &pt2) //Distance between two points
{return (pt2-pt1).mod();
}double dist(Point &ptC, Point &ptA, Point &ptB) //Distance between point C to line AB.
{double d=0.0;Point ac,ab;ac=ptC-ptA;ab=ptB-ptA;if (ab.mod() >= EPS) { d=(ac*ab).mod()/ab.mod(); }else { d=dist(ptC,ptA); } //The line is reduced to a point. point(ptC) to point(ptA) distance.return d;
}double dist(Point &ptA, Point &ptB, Point &ptC, Point &ptD) //Distance between line AB to line CD.
{double d=0.0;Point ab, cd, ad, axd;ab=ptB-ptA;cd=ptD-ptC;ad=ptD-ptA;axd=ab * cd;if ((axd.mod()) >= EPS) { d = abs((axd.dotX(ad)))/axd.mod(); } //skew lineselse { d=dist(ptA, ptC, ptD); } //parallel linesreturn d;
}int main()
{Point p0,p1(1.0, 0.0, 0.0), p2(0.0, 1.0, 0.0), p3(0.0, 0.0, 1.0), p4(1.0, 1.0, 1.0);Point p5(1.0, 1.0, 0.0), p6(0.0, 1.0, 1.0), p7(1.0, 0.0, 1.0);cout<<"\n---------------- Line to line --------------------"<<endl;cout<<"\nThe distance between the line p1p2 and the line p3p4 is : "<<dist(p1,p2,p3,p4)<<endl;cout<<"\nThe distance between the line p1p3 and the line p4p7 is : "<<dist(p1,p3,p4,p7)<<endl;cout<<"\nThe distance between the line p1p3 and the line p5p6 is : "<<dist(p1,p3,p5,p6)<<endl;cout<<"\nThe distance between the line p2p3 and the line p5p6 is : "<<dist(p2,p3,p5,p6)<<endl;cout<<"\nThe distance between the line p1p3 and the line p1p7 is : "<<dist(p1,p3,p1,p7)<<endl;return 0;
}
计算公式参考:https://en.wikipedia.org/wiki/Skew_lines
三维空间中直线间距离的计算相关推荐
- 计算三维空间中直线和三角形的交点
计算三维空间中直线和三角形的交点 前言 一.计算平面方程 1.1 平面不过原点 1.2 平面过原点 1.3 平面过 zzz 轴 1.4 平面为 yOzyOzyOz 平面 二.计算直线方程 三.计算交点 ...
- 等效距离计算公式_实用的计算方法-架空输电线路导线线间距离计算
1. 10kV 及以下架空线路导线线间距离 380V及以下沿墙敷设的绝缘导线,当档距不大于20m时,其线间距离不宜小于0.2m:3kV以下架空线路,靠近杆塔的两导线间的水平距离不应小于0.5m:10k ...
- 爆头(叉乘求点到直线的距离)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- 三维向量的简单运算(点积、叉积及点到直线的距离)
三维向量的点积(Dot Product) 点乘比较简单,是相应元素的乘积的和: V1( x1, y1, z1)·V2(x2, y2, z2) = x1*x2 + y1*y2 + z1*z2; 注 ...
- 标题使用java计算三维空间中两点的欧几里德距离
标题使用java计算三维空间中两点的欧几里德距离 public static double euclideanDistance(double[] point1, double[] point2) {d ...
- 点至直线的距离和垂足点计算
点至直线的距离和垂足点计算 //点到直线的垂足点 public static Coordinate getFootPoint(Coordinate point, Coordinate pnt1, Co ...
- 三维空间两直线/线段最短距离、线段计算算法
设有两空间线段 1. Ls L_s,其起点.终点坐标为 s0.s1 s_0.s_1 ,方向向量 u⃗ =s1−s0 \vec u = s_1-s_0 2. Lt L_t,其起点.终点坐标为 t0.t1 ...
- 点到直线的距离计算原理及MATLAB程序
在二维和三维空间,有现成的计算点到空间直线的距离的公式: 如: 三维空间有类似的计算公式. 甚至博客直接提出用叉乘和行列式计算点到直线距离的计算方法和程序.但仅限于二维和三维的情况.更高维的就不适用. ...
- 学习OpenCV3:判断两条直线平行,并计算平行距离
一.问题 已知两条直线 l 1 ( x 1 , y 1 , x 2 , y 2 ) l_1(x_1,y_1,x_2,y_2) l1(x1,y1,x2,y2)和 l 2 ( x 3 , y ...
- 13. 区域间距离计算
区域间距离计算 1.点和点之间的距离计算 distance_pp (Row1,Column1 ,Row2 ,Column2 , Distance) 2.点到直线的距离 distance_pl() 3. ...
最新文章
- 软件工程实践2017 个人技术博客
- 部署node.js的开发环境
- python使用函数的优点-Python用了这么多年,总结出超实用的功能和特点
- 21、HTML <select>标签(下拉列表)
- wxWidgets:wxFileDirPickerEvent类用法
- Span元素的 width属性 无效果原因及解决方案
- Pokémon Go火遍全球,开启全民捕捉小精灵的时代
- html实现拖拽排序,简单的jquery拖拽排序效果实现代码
- [html] 列举几种瀑布流布局的方法
- mysql 分组查询原理,MySQL分組查詢Group By實現原理詳解
- DVM的进程和Linux的进,下面关于Android dvm的进程和Linux的进程,应用程序的进程说法正确的是()...
- vue 一个组件内多个弹窗_使用vue实现各类弹出框组件
- MPLS virtual private network基础内容
- java for 变量赋值_Java 如何引用变量赋值?
- vector实现 并交差 集实例
- Spring启动异常之ConflictingBeanDefinitionException: Annotation-specified bean name ‘XXXXXService‘
- Win10-Ubuntu双系统
- windows 下搭建邮件服务器
- Glib基础——版本信息
- 港交所OMD-C对接笔记