首页 > 代码库 > hdu4741

hdu4741

题意:给定两条异面直线,求公垂线交点及垂足。

思路:去年网络赛杭州站的题,实际上就是一个模板。。顺带用来整理补充一下计算几何模板库。

code:

  1 /*  2  * Author:  Yzcstc  3  * Created Time:  2014/10/2 21:52:38  4  * File Name: hdu4741.cpp  5  */  6 #include<cstdio>  7 #include<iostream>  8 #include<cstring>  9 #include<cstdlib> 10 #include<cmath> 11 #include<algorithm> 12 #include<string> 13 #include<map> 14 #include<set> 15 #include<vector> 16 #include<queue> 17 #include<stack> 18 #include<ctime> 19 #define repf(i, a, b) for (int i = (a); i <= (b); ++i) 20 #define repd(i, a, b) for (int i = (a); i >= (b); --i) 21 #define M0(x)  memset(x, 0, sizeof(x)) 22 #define Inf  0x7fffffff 23 #define MP make_pair 24 #define PB push_back 25 #define eps 1e-8 26 #define pi acos(-1.0) 27 using namespace std; 28 inline int sgn(const double& x){ 29     return (x > eps) - (x < -eps); 30 } 31 struct point{ 32      double x, y, z; 33      point(double _x = 0, double _y = 0, double _z = 0):x(_x), y(_y), z(_z){} 34      void input(){ 35         scanf("%lf%lf%lf", &x, &y, &z); 36      } 37      double len()const{ 38          return sqrt(x * x + y * y + z * z); 39      } 40      point trunc(const double &l) const{ 41            double r  = l / len(); 42            return point(x * r, y * r, z * r);  43      } 44      bool operator!=(const point& p1)const{ 45          return !(sgn(x - p1.x) == 0 && sgn(y - p1.y) == 0 && sgn(z - p1.z) == 0); 46      } 47      point operator-(const point& p1)const{ 48          return point(x - p1.x, y - p1.y, z - p1.z); 49      } 50      point operator+(const point& p) const{ 51          return point(x + p.x, y + p.y, z + p.z);       52      } 53      double operator^(const point& p1)const{ 54          return x * p1.x + y * p1.y + z * p1.z; 55      } 56      point operator*(const point& p1)const{ 57          return point(y * p1.z - z * p1.y, z * p1.x - x * p1.z, x * p1.y - y * p1.x); 58      } 59      point operator*(const double& l)const{ 60          return point(x * l, y * l, z * l);       61      } 62 } p[101]; 63  64  65 int parallel(const point& u1,const point& u2,const point& v1,const point& v2){ 66     return sgn(((u1 - u2) * (v1 - v2)).len()) == 0; 67 } 68  69 double line2line_dist(const point& u1, const point& u2,const point& v1,const point& v2){ //计算直线u与v距离 70     point n =  (u1 - u2) * (v1 - v2); 71     return fabs((u1-v1) ^ n) / n.len(); 72 } 73  74 point intersection(const point& u1,const point& u2, const point& v1,const point& v2){ 75     double t=((u1.x-v1.x)*(v1.y-v2.y) - (u1.y-v1.y)*(v1.x-v2.x))  76               /((u1.x-u2.x)*(v1.y-v2.y) - (u1.y-u2.y)*(v1.x-v2.x)); 77     point p = u1 + (u2 - u1) * t; 78     return p; 79 } 80  81 void solve(){ 82      for (int i = 0; i < 4; ++i) 83            p[i].input(); 84      point v = (p[0] - p[1]) * (p[2] - p[3]); 85      double dist = line2line_dist(p[0], p[1], p[2], p[3]); 86      point v2 = p[2] - p[0]; 87      if (sgn(v ^ v2) < 0) v = v * -1; 88      v = v.trunc(dist); 89      point p2 = intersection(v + p[0], v + p[1], p[2], p[3]); 90      point p1 = p2 - v; 91      printf("%.6f\n", dist); 92      printf("%.6f %.6f %.6f %.6f %.6f %.6f\n", p1.x, p1.y, p1.z, p2.x, p2.y, p2.z); 93       94 } 95  96 int main(){ 97  //   freopen("a.in", "r", stdin); 98  //   freopen("a.out", "w", stdout); 99     int cas;100     scanf("%d", &cas);101     while (cas--){102          solve();103     }104     return 0;105 }
View Code

 

hdu4741