HDU 4741 Save Labman No.004 (几何)
题意:求空间两线的最短距离和最短线的交点
题解:
线性代数和空间几何,主要是用叉积,点积,几何。
知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形成的向量做内积,
在除掉gamma的长度就得到投影,即是最短距离。
然后求两个点可以用gamma和s2的叉积和l2上的一个点描述一个平面,再求平面和线的交点,
把(p2-p1)*n 和(p0-p1)*n相除算出比例乘上p2-p1得到交点和p1的差,再加上p1就求出交点了
学习点:计算几何的一些东西

#include<cstdio>
#include<cmath>
inline double fun(double a, double b, double c, double d){
return a*d - b*c;
} #define squ(x) ((x)*(x)) struct Poi
{
double x,y,z;
Poi(double X = , double Y = , double Z = ){
x = X; y = Y; z = Z;
}
void input(){
scanf("%lf%lf%lf",&x,&y,&z);
}
Poi operator + (const Poi & rhs){
return Poi(x+rhs.x,y+rhs.y,z+rhs.z);
}
Poi operator - (Poi & rhs){
return Poi(x-rhs.x,y-rhs.y,z-rhs.z);
}
Poi operator ^(Poi & rhs){
return Poi(fun(y,z,rhs.y,rhs.z),-fun(x,z,rhs.x,rhs.z),fun(x,y,rhs.x,rhs.y));
}
Poi operator *(double t){
return Poi(x*t,y*t,z*t);
}
double operator *(const Poi & rhs){
return x*rhs.x+y*rhs.y+z*rhs.z;
}
};
typedef Poi Vector; double Dot(const Vector & a,const Poi& b) {
return a.x*b.x+a.y*b.y+a.z*b.z;
} Poi LinePlaneIns(Poi &p1,Poi &p2,Poi &p0,Vector &n){
Vector v = p2 - p1;
double Ratio = (Dot(n,p0-p1))/(Dot(n,v));//保证相交
return p1+v*Ratio;
}
double Length(const Vector &x){
return sqrt(squ(x.x)+squ(x.y)+squ(x.z));
} int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
Poi p1,p2,p3,p4;
p1.input();
p2.input();
p3.input();
p4.input();
Vector alpha = p2 - p1;
Vector beta = p4 - p3;
Vector gamma = alpha^beta;
double distance = fabs(Dot(gamma,(p1-p3))/Length(gamma));
Vector n1 = alpha^gamma;
Vector n2 = beta^gamma;
Poi ins1 = LinePlaneIns(p1,p2,p3,n2);
Poi ins2 = LinePlaneIns(p3,p4,p1,n1);
printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",distance,ins1.x,ins1.y,ins1.z,ins2.x,ins2.y,ins2.z);
}
return ;
}
HDU 4741 Save Labman No.004 (几何)的更多相关文章
- HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)
Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 4741 Save Labman No.004异面直线间的距离既构成最小距离的两个端点
Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4741 Save Labman No.004 2013 ACM/ICPC 杭州网络赛
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4741 题意:给你两条异面直线,然你求着两条直线的最短距离,并求出这条中垂线与两直线的交点. 需要注意的是 ...
- HDU 4741 Save Labman No.004(计算几何)
题目链接 抄的模版...mark一下. #include <iostream> #include <cstring> #include <cstdio> #incl ...
- hdu 4741 Save Labman No.004 [2013年杭州ACM网络赛]
// Time 234 ms; Memory 244 K #include<iostream> #include<cstdio> #include<cmath> u ...
- hdu 4741 Save Labman No.004(2013杭州网络赛)
http://blog.sina.com.cn/s/blog_a401a1ea0101ij9z.html 空间两直线上最近点对. 这个博客上给出了很好的点法式公式了...其实没有那么多的tricky. ...
- [HDU 4741]Save Labman No.004[计算几何][精度]
题意: 求两条空间直线的距离,以及对应那条距离线段的两端点坐标. 思路: 有一个参数方程算最短距离的公式, 代入求即可. 但是这题卡精度... 用另外的公式(先算出a直线上到b最近的点p的坐标, 再算 ...
- hdu 4741 Save Labman No.004 (异面直线的距离)
转载学习: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorit ...
- HDU 4741 Save Labman No.004 ( 三维计算几何 空间异面直线距离 )
空间异面直线的距离直接套模板. 求交点:求出两条直线的公共法向量,其中一条直线与法向量构成的平面 与 另一条直线 的交点即可.还是套模板o(╯□╰)o 1.不会有两条线平行的情况. 2.两条直线可能相 ...
随机推荐
- js中的"=="和equals()以及is()三者的区别
在 javaScript或者jQuery中字符串比较没有equals()方法,要比较两个字符串是否相等可以直接用==或者is()进行判断. 例如: "a"=="a&quo ...
- RPC原理与实践(二)----Thrift分层模型
这一节我们从一下几个方面来讲一下Thrift的分层架构,按照官方的定义这是Thrift的网络栈,其中网络栈中分为一下几个部分,(由栈顶到栈底)server,processor,protocol,tra ...
- Codeforces Round #401 (Div. 2)【A,B,C,D】
最近状态极差..水题不想写,难题咬不动..哎,CF的题那么简单,还搞崩了= =.真是巨菜无比. Codeforces777A 题意:略. 思路: 构造出3!次变换,然后输出就好. Code: #inc ...
- 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery
P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...
- cogs 1901. [国家集训队2011]数颜色
Cogs 1901. [国家集训队2011]数颜色 ★★★ 输入文件:nt2011_color.in 输出文件:nt2011_color.out 简单对比时间限制:0.6 s 内存限制 ...
- React中方法的this绑定
第一种 在组件(类)的constructor中绑定this class Demo extends Component { constructor(this) { super(this) this.st ...
- fatal pylint error : ......can't find '__main__'module in
fatal pylint error : ......can't find '__main__'module in原因是没有安装pylint,所以提示没有找到__main__模块 解决方案:1.到官网 ...
- Uva11134
#include<bits/stdc++.h> #define inf 0x3f3f3f3f ; using namespace std; int n; struct rook{ int ...
- Python大战机器学习——基础知识+前两章内容
一 矩阵求导 复杂矩阵问题求导方法:可以从小到大,从scalar到vector再到matrix. x is a column vector, A is a matrix d(A∗x)/dx=A d( ...
- Struts2 拦截器(Interceptor )原理和配置
http://blog.csdn.net/kingmax54212008/article/details/51777851