判断一个长方体和一个球是否相交
找到一个离求最近的点,然后算这个点到这个球圆心的距离,就能判断出是否相交
代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 5 using namespace std; 6 7 typedef struct point 8 { 9 double x,y,z; 10 }point; 11 12 point a[10]; 13 14 double judge(double x,double y,double z,point q) 15 { 16 double sum = (x-q.x)*(x-q.x) + (y-q.y)*(y-q.y) +(z-q.z)*(z-q.z); 17 sum = sqrt(sum); 18 return sum; 19 } 20 21 int main() 22 { 23 int T; 24 scanf("%d",&T); 25 while(T--) 26 { 27 int i = 0; 28 for(i = 0; i < 8; i++) 29 scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z); 30 point b; 31 double r1; 32 scanf("%lf%lf%lf%lf",&b.x,&b.y,&b.z,&r1); 33 double flag = 0; 34 double kk; 35 double xmin,xmax,ymin,ymax,zmin,zmax; 36 double x,y,z; 37 38 xmin = a[0].x;xmax = a[0].x; 39 ymin = a[0].y;ymax = a[0].y; 40 zmin = a[0].z;zmax = a[0].z; 41 42 for(i = 1; i < 8; i++) 43 { 44 if(a[i].x < xmin) xmin = a[i].x; 45 if(a[i].x > xmax) xmax = a[i].x; 46 if(a[i].y < ymin) ymin = a[i].y; 47 if(a[i].y > ymax) ymax = a[i].y; 48 if(a[i].z < zmin) zmin = a[i].z; 49 if(a[i].z > zmax) zmax = a[i].z; 50 51 } 52 if(b.x < xmin) x = xmin; 53 else if(b.x > xmax) x= xmax; 54 else x = b.x; 55 56 if(b.y < ymin) y = ymin; 57 else if(b.y > ymax) y= ymax; 58 else y = b.y; 59 60 if(b.z < zmin) z = zmin; 61 else if(b.z > zmax) z= zmax; 62 else z = b.z; 63 64 if( judge(x,y,z,b) > r1) 65 printf("No\n"); 66 else 67 printf("Yes\n"); 68 } 69 return 0; 70 }