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.两条直线可能相 ...
随机推荐
- windows64下安装MySQLdb连接数据库
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 1.首先要下载:http://www.codegood.com/downloads,由于我的python是2 ...
- Python 在windows上安装BeautifulSoup和request以及小案例
Python以及PyCharm安装成功后,操作如下: 此时,代码import requests不报错了. 那么,Python 在windows上安装BeautifulSoup,怎么操作呢? 1. 打开 ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- echarts3.0 实例容器不实时更新页面的问题
var instanceId = document.getElementById(option.echartId).getAttribute('_echarts3_instance_'); if (i ...
- pytest框架(二)
一.示例代码一 D:YOYO\ __init__.py test_class.py # content of test_class.py class TestClass: def test_one(s ...
- 00 | Two Sum
Question Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 【VueJS】windows环境安装vue-cli及webpack并创建VueJS项目
1. 安装node.js, Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 这次node.js不是主角,默认已安装好了,通过npm –v查看no ...
- 小技巧(updating)
小技巧 我们要算一个点集中所有点到另一个点集中所有点的一些量的时候,可以建立一个超级源点和超级汇点,从多->多变成单->单 整体二分的时候,操作要可以撤销,才能保证复杂度,每一层到左边区间 ...
- 湖南大学新生赛C,G,J题解
C: 思路:做几组数据就基本能发现规律,奇数为-1,偶数为1 代码: #include<cstdio> #include<iostream> #include<cstri ...
- 一篇关于完全动态凸包的paper(侵删)
先放原文,挖个坑,到时候再来说人话ε=(´ο`*))) 作者:Franco P. Preparata 出处:Computational geometry An introduction The tec ...