hdu 4741 Save Labman No.004 (异面直线的距离)
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- const double EPS = 1e-;
- const int MAXN = ;
- struct Point3 //空间点
- {
- double x, y, z;
- Point3( double x=, double y=, double z= ): x(x), y(y), z(z) { }
- Point3( const Point3& a )
- {
- x = a.x;
- y = a.y;
- z = a.z;
- return;
- }
- void showP()
- {
- printf("%f %f %f \n", x, y, z);
- }
- Point3 operator+( Point3& rhs )
- {
- return Point3( x+rhs.x, y+rhs.y, z+rhs.z );
- }
- };
- struct Line3 //空间直线
- {
- Point3 a, b;
- };
- struct plane3 //空间平面
- {
- Point3 a, b, c;
- plane3() {}
- plane3( Point3 a, Point3 b, Point3 c ):
- a(a), b(b), c(c) { }
- void showPlane()
- {
- a.showP();
- b.showP();
- c.showP();
- return;
- }
- };
- double dcmp( double a )
- {
- if ( fabs( a ) < EPS ) return ;
- return a < ? - : ;
- }
- //三维叉积
- Point3 Cross3( Point3 u, Point3 v )
- {
- Point3 ret;
- ret.x = u.y * v.z - v.y * u.z;
- ret.y = u.z * v.x - u.x * v.z;
- ret.z = u.x * v.y - u.y * v.x;
- return ret;
- }
- //三维点积
- double Dot3( Point3 u, Point3 v )
- {
- return u.x * v.x + u.y * v.y + u.z * v.z;
- }
- //矢量差
- Point3 Subt( Point3 u, Point3 v )
- {
- Point3 ret;
- ret.x = u.x - v.x;
- ret.y = u.y - v.y;
- ret.z = u.z - v.z;
- return ret;
- }
- //两点距离
- double TwoPointDistance( Point3 p1, Point3 p2 )
- {
- return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) + (p1.z - p2.z)*(p1.z - p2.z) );
- }
- //向量的模
- double VectorLenth( Point3 p )
- {
- return sqrt( p.x*p.x + p.y*p.y + p.z*p.z );
- }
- //空间直线距离
- double LineToLine( Line3 u, Line3 v, Point3& tmp )
- {
- tmp = Cross3( Subt( u.a, u.b ), Subt( v.a, v.b ) );
- return fabs( Dot3( Subt(u.a, v.a), tmp ) ) / VectorLenth(tmp);
- }
- //取平面法向量
- Point3 pvec( plane3 s )
- {
- return Cross3( Subt( s.a, s.b ), Subt( s.b, s.c ) );
- }
- //空间平面与直线的交点
- Point3 Intersection( Line3 l, plane3 s )
- {
- Point3 ret = pvec(s);
- double t = ( ret.x*(s.a.x-l.a.x)+ret.y*(s.a.y-l.a.y)+ret.z*(s.a.z-l.a.z) )/( ret.x*(l.b.x-l.a.x)+ret.y*(l.b.y-l.a.y)+ret.z*(l.b.z-l.a.z) );
- ret.x = l.a.x + ( l.b.x - l.a.x ) * t;
- ret.y = l.a.y + ( l.b.y - l.a.y ) * t;
- ret.z = l.a.z + ( l.b.z - l.a.z ) * t;
- return ret;
- }
- /************以上模板*************/
- void solved( Line3 A, Line3 B )
- {
- Point3 normal;
- double dis = LineToLine( A, B, normal );
- printf( "%.6f\n", dis );
- plane3 pla;
- pla = plane3( A.a, A.b, A.a + normal );
- Point3 u = Intersection( B, pla );
- pla = plane3( B.a, B.b, B.a + normal );
- Point3 v = Intersection( A, pla );
- printf("%.6f %.6f %.6f %.6f %.6f %.6f\n", v.x, v.y, v.z, u.x, u.y, u.z );
- return;
- }
- int main()
- {
- int T;
- scanf( "%d", &T );
- while ( T-- )
- {
- Line3 A, B;
- scanf("%lf%lf%lf", &A.a.x, &A.a.y, &A.a.z );
- scanf("%lf%lf%lf", &A.b.x, &A.b.y, &A.b.z );
- scanf("%lf%lf%lf", &B.a.x, &B.a.y, &B.a.z );
- scanf("%lf%lf%lf", &B.b.x, &B.b.y, &B.b.z );
- solved( A, B );
- }
- return ;
- }
不知精度误差的WA
- #include<stdio.h>
- #include<math.h>
- #define eps 1e-12
- double myfabs(double x)
- {
- if(x<)x=-x;
- return x;
- }
- int main()
- {
- int _case;
- /*double xa,xb,xc,xd;
- double ya,yb,yc,yd;
- double za,zb,zc,zd;
- */
- double Xa,Xb,Xc,Xd,Ya,Yb,Yc,Yd,Za,Zb,Zc,Zd;
- scanf("%d",&_case);
- while(_case--)
- {
- /*scanf("%lf%lf%lf%lf%lf%lf",&xa,&ya,&za,&xb,&yb,&zb);
- scanf("%lf%lf%lf%lf%lf%lf",&xc,&yc,&zc,&xd,&yd,&zd);*/
- scanf("%lf%lf%lf%lf%lf%lf",&Xa,&Ya,&Za,&Xb,&Yb,&Zb);
- scanf("%lf%lf%lf%lf%lf%lf",&Xc,&Yc,&Zc,&Xd,&Yd,&Zd);
- /*long double Xa=(long double)xa;
- long double Xb=(long double)xb;
- long double Xc=(long double)xc;
- long double Xd=(long double)xd;
- long double Ya=(long double)ya;
- long double Yb=(long double)yb;
- long double Yc=(long double)yc;
- long double Yd=(long double)yd;
- long double Za=(long double)za;
- long double Zb=(long double)zb;
- long double Zc=(long double)zc;
- long double Zd=(long double)zd;*/
- double F11=(Xb-Xa)*(Xb-Xa)+(Yb-Ya)*(Yb-Ya)+(Zb-Za)*(Zb-Za);
- double F12= (Xd-Xc)*(Xd-Xc)+(Yd-Yc)*(Yd-Yc)+(Zd-Zc)*(Zd-Zc);
- double F2=(Xb-Xa)*(Xd-Xc)+(Yb-Ya)*(Yd-Yc)+(Zb-Za)*(Zd-Zc);
- double F31=(Xb-Xa)*(Xc-Xa)+(Yb-Ya)*(Yc-Ya)+(Zb-Za)*(Zc-Za);
- double F32=(Xd-Xc)*(Xc-Xa)+(Yd-Yc)*(Yc-Ya)+(Zd-Zc)*(Zc-Za);
- double y=F11*F12-F2*F2;
- //if(myfabs(y)<eps)y=eps;
- double t1=(F31*F12-F32*F2)/y;
- double t2=(F32*F11-F2*F31)/(-y);
- double Xm=t1*(Xb-Xa)+Xa;//=(Xb-Xa)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Xa;
- double Ym=t1*(Yb-Ya)+Ya;//=(Yb-Ya)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Ya;
- double Zm=t1*(Zb-Za)+Za;//=(Zb-Za)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Za;
- double Xn=t2*(Xd-Xc)+Xc;//=(Xd-Xc)*[F3(c,d)*F1(a,b)-F3(a,b)*F2()]/[F2()*F2()-F1(a,b)*F1(c,d)]+Xc;
- double Yn=t2*(Yd-Yc)+Yc;//=(Yd-Yc)*[F3(c,d)*F1(a,b)-F3(a,b)*F2()]/[F2()*F2()-F1(a,b)*F1(c,d)]+Yc;
- double Zn=t2*(Zd-Zc)+Zc;
- double s=sqrt((Xn-Xm)*(Xn-Xm)+(Yn-Ym)*(Yn-Ym)+(Zn-Zm)*(Zn-Zm));
- /*double xm=(double)Xm;//=t1*(Xb-Xa)+Xa;//=(Xb-Xa)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Xa;
- double ym=(double)Ym;//=t1*(Yb-Ya)+Ya;//=(Yb-Ya)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Ya;
- double zm=(double)Zm;//=t1*(Zb-Za)+Za;//=(Zb-Za)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Za;
- double xn=(double)Xn;//=t2*(Xd-Xc)+Xc;//=(Xd-Xc)*[F3(c,d)*F1(a,b)-F3(a,b)*F2()]/[F2()*F2()-F1(a,b)*F1(c,d)]+Xc;
- double yn=(double)Yn;//=t2*(Yd-Yc)+Yc;//=(Yd-Yc)*[F3(c,d)*F1(a,b)-F3(a,b)*F2()]/[F2()*F2()-F1(a,b)*F1(c,d)]+Yc;
- double zn=(double)Zn;
- //printf("%lf\n",eps);*/
- printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",s,Xm,Ym,Zm,Xn,Yn,Zn);
- //printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",s,xm,ym,zm,xn,yn,zn);
- }
- return ;
- }
hdu 4741 Save Labman No.004 (异面直线的距离)的更多相关文章
- 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杭州网络赛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 ( 三维计算几何 空间异面直线距离 )
空间异面直线的距离直接套模板. 求交点:求出两条直线的公共法向量,其中一条直线与法向量构成的平面 与 另一条直线 的交点即可.还是套模板o(╯□╰)o 1.不会有两条线平行的情况. 2.两条直线可能相 ...
- 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 (几何)
题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...
随机推荐
- PCB 封装中的 公差符号形位公差位置度
PCB 封装中的 公差符号形位公差位置度 0.08 旁边的 十字加圆就是位置度的形位公差.
- bzoj 4556 字符串 —— 后缀数组+主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4556 就是找一个 rk 在一段区间内的前驱和后继: 由于 LCP 还有区间长度的限制,所以可 ...
- 1120 Friend Numbers
题意:略. 思路:水题,略. 代码: #include <iostream> #include <string> using namespace std; ; }; int m ...
- 1078 Hashing
题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Sea ...
- Pycharm快速复制当前行到下一行Ctrl+D
Pycharm快速复制当前行到下一行Ctrl+D
- angular 三大核心函数
1.$watch angular监听 由于angular一直在实时监听,所以比react和vue效率要低 $scope.$watch('aModel', function(newValue, o ...
- 配置ElasticSearch快捷启动
在/etc/init.d目录下新建文件elasticsearch #!/bin/sh #chkconfig: 2345 80 05 #description: es #export JAVA_HOME ...
- python's twenty-third day for me 面向对象进阶
普通方法:对象和类绑定的过程. class A: def func1(self):pass def func2(self):pass def func3(self):pass def func4(se ...
- springboot成神之——spring文件下载功能
本文介绍spring文件下载功能 目录结构 DemoApplication WebConfig TestController MediaTypeUtils 前端测试 本文介绍spring文件下载功能 ...
- Linq入门博客系列地址http://www.cnblogs.com/lifepoem/category/330218.html
http://www.cnblogs.com/lifepoem/category/330218.html Linq及LambdaSql语句: http://kb.cnblogs.com/page/42 ...