转载学习:
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const double EPS = 1e-;
  10. const int MAXN = ;
  11.  
  12. struct Point3 //空间点
  13. {
  14. double x, y, z;
  15. Point3( double x=, double y=, double z= ): x(x), y(y), z(z) { }
  16. Point3( const Point3& a )
  17. {
  18. x = a.x;
  19. y = a.y;
  20. z = a.z;
  21. return;
  22. }
  23. void showP()
  24. {
  25. printf("%f %f %f \n", x, y, z);
  26. }
  27. Point3 operator+( Point3& rhs )
  28. {
  29. return Point3( x+rhs.x, y+rhs.y, z+rhs.z );
  30. }
  31. };
  32.  
  33. struct Line3 //空间直线
  34. {
  35. Point3 a, b;
  36. };
  37.  
  38. struct plane3 //空间平面
  39. {
  40. Point3 a, b, c;
  41. plane3() {}
  42. plane3( Point3 a, Point3 b, Point3 c ):
  43. a(a), b(b), c(c) { }
  44. void showPlane()
  45. {
  46. a.showP();
  47. b.showP();
  48. c.showP();
  49. return;
  50. }
  51. };
  52.  
  53. double dcmp( double a )
  54. {
  55. if ( fabs( a ) < EPS ) return ;
  56. return a < ? - : ;
  57. }
  58.  
  59. //三维叉积
  60. Point3 Cross3( Point3 u, Point3 v )
  61. {
  62. Point3 ret;
  63. ret.x = u.y * v.z - v.y * u.z;
  64. ret.y = u.z * v.x - u.x * v.z;
  65. ret.z = u.x * v.y - u.y * v.x;
  66. return ret;
  67. }
  68.  
  69. //三维点积
  70. double Dot3( Point3 u, Point3 v )
  71. {
  72. return u.x * v.x + u.y * v.y + u.z * v.z;
  73. }
  74.  
  75. //矢量差
  76. Point3 Subt( Point3 u, Point3 v )
  77. {
  78. Point3 ret;
  79. ret.x = u.x - v.x;
  80. ret.y = u.y - v.y;
  81. ret.z = u.z - v.z;
  82. return ret;
  83. }
  84.  
  85. //两点距离
  86. double TwoPointDistance( Point3 p1, Point3 p2 )
  87. {
  88. 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) );
  89. }
  90.  
  91. //向量的模
  92. double VectorLenth( Point3 p )
  93. {
  94. return sqrt( p.x*p.x + p.y*p.y + p.z*p.z );
  95. }
  96.  
  97. //空间直线距离
  98. double LineToLine( Line3 u, Line3 v, Point3& tmp )
  99. {
  100. tmp = Cross3( Subt( u.a, u.b ), Subt( v.a, v.b ) );
  101. return fabs( Dot3( Subt(u.a, v.a), tmp ) ) / VectorLenth(tmp);
  102. }
  103.  
  104. //取平面法向量
  105. Point3 pvec( plane3 s )
  106. {
  107. return Cross3( Subt( s.a, s.b ), Subt( s.b, s.c ) );
  108. }
  109.  
  110. //空间平面与直线的交点
  111. Point3 Intersection( Line3 l, plane3 s )
  112. {
  113. Point3 ret = pvec(s);
  114. 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) );
  115. ret.x = l.a.x + ( l.b.x - l.a.x ) * t;
  116. ret.y = l.a.y + ( l.b.y - l.a.y ) * t;
  117. ret.z = l.a.z + ( l.b.z - l.a.z ) * t;
  118. return ret;
  119. }
  120.  
  121. /************以上模板*************/
  122.  
  123. void solved( Line3 A, Line3 B )
  124. {
  125. Point3 normal;
  126. double dis = LineToLine( A, B, normal );
  127. printf( "%.6f\n", dis );
  128. plane3 pla;
  129. pla = plane3( A.a, A.b, A.a + normal );
  130. Point3 u = Intersection( B, pla );
  131. pla = plane3( B.a, B.b, B.a + normal );
  132. Point3 v = Intersection( A, pla );
  133. printf("%.6f %.6f %.6f %.6f %.6f %.6f\n", v.x, v.y, v.z, u.x, u.y, u.z );
  134. return;
  135. }
  136.  
  137. int main()
  138. {
  139. int T;
  140. scanf( "%d", &T );
  141. while ( T-- )
  142. {
  143. Line3 A, B;
  144. scanf("%lf%lf%lf", &A.a.x, &A.a.y, &A.a.z );
  145. scanf("%lf%lf%lf", &A.b.x, &A.b.y, &A.b.z );
  146. scanf("%lf%lf%lf", &B.a.x, &B.a.y, &B.a.z );
  147. scanf("%lf%lf%lf", &B.b.x, &B.b.y, &B.b.z );
  148. solved( A, B );
  149. }
  150. return ;
  151. }

不知精度误差的WA

  1. #include<stdio.h>
  2. #include<math.h>
  3. #define eps 1e-12
  4. double myfabs(double x)
  5. {
  6. if(x<)x=-x;
  7. return x;
  8. }
  9. int main()
  10. {
  11. int _case;
  12. /*double xa,xb,xc,xd;
  13. double ya,yb,yc,yd;
  14. double za,zb,zc,zd;
  15. */
  16. double Xa,Xb,Xc,Xd,Ya,Yb,Yc,Yd,Za,Zb,Zc,Zd;
  17.  
  18. scanf("%d",&_case);
  19. while(_case--)
  20. {
  21. /*scanf("%lf%lf%lf%lf%lf%lf",&xa,&ya,&za,&xb,&yb,&zb);
  22. scanf("%lf%lf%lf%lf%lf%lf",&xc,&yc,&zc,&xd,&yd,&zd);*/
  23. scanf("%lf%lf%lf%lf%lf%lf",&Xa,&Ya,&Za,&Xb,&Yb,&Zb);
  24. scanf("%lf%lf%lf%lf%lf%lf",&Xc,&Yc,&Zc,&Xd,&Yd,&Zd);
  25. /*long double Xa=(long double)xa;
  26. long double Xb=(long double)xb;
  27. long double Xc=(long double)xc;
  28. long double Xd=(long double)xd;
  29.  
  30. long double Ya=(long double)ya;
  31. long double Yb=(long double)yb;
  32. long double Yc=(long double)yc;
  33. long double Yd=(long double)yd;
  34.  
  35. long double Za=(long double)za;
  36. long double Zb=(long double)zb;
  37. long double Zc=(long double)zc;
  38. long double Zd=(long double)zd;*/
  39. double F11=(Xb-Xa)*(Xb-Xa)+(Yb-Ya)*(Yb-Ya)+(Zb-Za)*(Zb-Za);
  40. double F12= (Xd-Xc)*(Xd-Xc)+(Yd-Yc)*(Yd-Yc)+(Zd-Zc)*(Zd-Zc);
  41. double F2=(Xb-Xa)*(Xd-Xc)+(Yb-Ya)*(Yd-Yc)+(Zb-Za)*(Zd-Zc);
  42. double F31=(Xb-Xa)*(Xc-Xa)+(Yb-Ya)*(Yc-Ya)+(Zb-Za)*(Zc-Za);
  43. double F32=(Xd-Xc)*(Xc-Xa)+(Yd-Yc)*(Yc-Ya)+(Zd-Zc)*(Zc-Za);
  44. double y=F11*F12-F2*F2;
  45. //if(myfabs(y)<eps)y=eps;
  46. double t1=(F31*F12-F32*F2)/y;
  47. double t2=(F32*F11-F2*F31)/(-y);
  48.  
  49. double Xm=t1*(Xb-Xa)+Xa;//=(Xb-Xa)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Xa;
  50. double Ym=t1*(Yb-Ya)+Ya;//=(Yb-Ya)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Ya;
  51. double Zm=t1*(Zb-Za)+Za;//=(Zb-Za)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Za;
  52.  
  53. 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;
  54. 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;
  55. double Zn=t2*(Zd-Zc)+Zc;
  56. double s=sqrt((Xn-Xm)*(Xn-Xm)+(Yn-Ym)*(Yn-Ym)+(Zn-Zm)*(Zn-Zm));
  57.  
  58. /*double xm=(double)Xm;//=t1*(Xb-Xa)+Xa;//=(Xb-Xa)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Xa;
  59. double ym=(double)Ym;//=t1*(Yb-Ya)+Ya;//=(Yb-Ya)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Ya;
  60. double zm=(double)Zm;//=t1*(Zb-Za)+Za;//=(Zb-Za)*[F31*F12-F32*F2]/[F11*F12-F2*F2]+Za;
  61.  
  62. 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;
  63. 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;
  64. double zn=(double)Zn;
  65. //printf("%lf\n",eps);*/
  66. printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",s,Xm,Ym,Zm,Xn,Yn,Zn);
  67. //printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",s,xm,ym,zm,xn,yn,zn);
  68. }
  69. return ;
  70. }

hdu 4741 Save Labman No.004 (异面直线的距离)的更多相关文章

  1. hdu 4741 Save Labman No.004异面直线间的距离既构成最小距离的两个端点

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. 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 ...

  3. HDU 4741 Save Labman No.004 ( 三维计算几何 空间异面直线距离 )

    空间异面直线的距离直接套模板. 求交点:求出两条直线的公共法向量,其中一条直线与法向量构成的平面 与 另一条直线 的交点即可.还是套模板o(╯□╰)o 1.不会有两条线平行的情况. 2.两条直线可能相 ...

  4. HDU 4741 Save Labman No.004 2013 ACM/ICPC 杭州网络赛

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4741 题意:给你两条异面直线,然你求着两条直线的最短距离,并求出这条中垂线与两直线的交点. 需要注意的是 ...

  5. HDU 4741 Save Labman No.004(计算几何)

    题目链接 抄的模版...mark一下. #include <iostream> #include <cstring> #include <cstdio> #incl ...

  6. hdu 4741 Save Labman No.004 [2013年杭州ACM网络赛]

    // Time 234 ms; Memory 244 K #include<iostream> #include<cstdio> #include<cmath> u ...

  7. hdu 4741 Save Labman No.004(2013杭州网络赛)

    http://blog.sina.com.cn/s/blog_a401a1ea0101ij9z.html 空间两直线上最近点对. 这个博客上给出了很好的点法式公式了...其实没有那么多的tricky. ...

  8. [HDU 4741]Save Labman No.004[计算几何][精度]

    题意: 求两条空间直线的距离,以及对应那条距离线段的两端点坐标. 思路: 有一个参数方程算最短距离的公式, 代入求即可. 但是这题卡精度... 用另外的公式(先算出a直线上到b最近的点p的坐标, 再算 ...

  9. HDU 4741 Save Labman No.004 (几何)

    题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...

随机推荐

  1. PCB 封装中的 公差符号形位公差位置度

    PCB 封装中的 公差符号形位公差位置度 0.08 旁边的 十字加圆就是位置度的形位公差.

  2. bzoj 4556 字符串 —— 后缀数组+主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4556 就是找一个 rk 在一段区间内的前驱和后继: 由于 LCP 还有区间长度的限制,所以可 ...

  3. 1120 Friend Numbers

    题意:略. 思路:水题,略. 代码: #include <iostream> #include <string> using namespace std; ; }; int m ...

  4. 1078 Hashing

    题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Sea ...

  5. Pycharm快速复制当前行到下一行Ctrl+D

    Pycharm快速复制当前行到下一行Ctrl+D

  6. angular 三大核心函数

    1.$watch  angular监听   由于angular一直在实时监听,所以比react和vue效率要低 $scope.$watch('aModel', function(newValue, o ...

  7. 配置ElasticSearch快捷启动

    在/etc/init.d目录下新建文件elasticsearch #!/bin/sh #chkconfig: 2345 80 05 #description: es #export JAVA_HOME ...

  8. python's twenty-third day for me 面向对象进阶

    普通方法:对象和类绑定的过程. class A: def func1(self):pass def func2(self):pass def func3(self):pass def func4(se ...

  9. springboot成神之——spring文件下载功能

    本文介绍spring文件下载功能 目录结构 DemoApplication WebConfig TestController MediaTypeUtils 前端测试 本文介绍spring文件下载功能 ...

  10. 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 ...