hdu 4741

今天的比赛前面四个多小时没有出题,智商真是拙计;

其实前面都是水题,就是想不到思路!

知道最后40分钟,和小珺同志把这个计算几何的题给A了;

然后小琪也马上把第3题干掉了;

代码:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 using namespace std;
  5 struct point3
  6 {
  7     double x,y,z;
  8 };
  9 
 10 //向量大小
 11 double vlen(point3 p)
 12 {
 13     return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
 14 }
 15 
 16 //计算 dot product U . V
 17 double dmult(point3 u,point3 v)
 18 {
 19     return u.x*v.x+u.y*v.y+u.z*v.z;
 20 }
 21 
 22 //计算 cross product U x V
 23 point3 xmult(point3 u,point3 v)
 24 {
 25     point3 ret;
 26     ret.x=u.y*v.z-v.y*u.z;
 27     ret.y=u.z*v.x-u.x*v.z;
 28     ret.z=u.x*v.y-u.y*v.x;
 29     return ret;
 30 }
 31 //矢量差 U - V
 32 point3 subt(point3 u,point3 v)
 33 {
 34     point3 ret;
 35     ret.x=u.x-v.x;
 36     ret.y=u.y-v.y;
 37     ret.z=u.z-v.z;
 38     return ret;
 39 }
 40 
 41 point3 intersection(point3 u1,point3 u2,point3 v1,point3 v2)
 42 {
 43     point3 ret=u1;
 44     double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
 45              /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
 46     ret.x+=(u2.x-u1.x)*t;
 47     ret.y+=(u2.y-u1.y)*t;
 48     ret.z+=(u2.z-u1.z)*t;
 49     printf("%0.6lf %0.6lf %0.6lf ",ret.x,ret.y,ret.z);
 50     return ret;
 51 }
 52 
 53 double linetoline(point3 u1,point3 u2,point3 v1,point3 v2)
 54 {
 55     point3 a=subt(u1,u2);
 56     point3 b=subt(v1,v2);
 57 
 58     //point3 ans=intersection(u1,u2,a,b);
 59     point3 n=xmult(a,b);
 60     return fabs(dmult(subt(u1,v1),n))/vlen(n);
 61 }
 62 
 63 point3 add(point3 a,point3 b)
 64 {
 65     point3 ret;
 66     ret.x=a.x+b.x;
 67     ret.y=a.y+b.y;
 68     ret.z=a.z+b.z;
 69     return ret;
 70 }
 71 
 72 int main()
 73 {
 74     point3 a,b,c,d;
 75     int t;
 76     scanf("%d",&t);
 77     while(t--)
 78     {
 79         scanf("%lf%lf%lf",&a.x,&a.y,&a.z);
 80         scanf("%lf%lf%lf",&b.x,&b.y,&b.z);
 81         scanf("%lf%lf%lf",&c.x,&c.y,&c.z);
 82         scanf("%lf%lf%lf",&d.x,&d.y,&d.z);
 83         printf("%.6lf\n",linetoline(a,b,c,d));
 84         point3 x1=subt(a,b);
 85         point3 x2=subt(c,d);
 86         point3 n=xmult(x1,x2);
 87         double k=d.x-c.x;//kk:k k:K
 88         double l=d.y-c.y;
 89         double m=d.z-c.z;
 90         double h=b.x-a.x;
 91         double i=b.y-a.y;
 92         double j=b.z-a.z;
 93         double N=h*i*l-i*i*k-j*j*k+h*j*m;
 94         double O=h*h*l-h*i*k-i*j*m+j*j*l;
 95         double P=h*j*k-h*h*m-i*i*m+i*j*l;
 96         double Q=-a.x*N+a.y*O-a.z*P;
 97         double kk=(O*c.y-N*c.x-P*c.z-Q)/(N*k-O*l+P*m);
 98         point3 ss;
 99         ss.x=k*kk+c.x;
100         ss.y=l*kk+c.y;
101         ss.z=m*kk+c.z;
102         point3 ans=intersection(ss,add(ss,n),a,b);
103         printf("%.6lf %.6lf %.6lf\n",k*kk+c.x,l*kk+c.y,m*kk+c.z);
104     }
105     return 0;
106 }
View Code

 

posted @ 2013-09-15 18:35  Yours1103  阅读(359)  评论(0编辑  收藏  举报