题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1174
解题报告:就是用到了三维向量的点积来求点到直线的距离,向量(x1,y1,z1)与(x2,y2,z2)的点积是:x1*x2+y1*y2+z1*z2.
然后要注意的就是当两个向量的夹角大于等于90度时,无论如何都不能射中.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double PI = acos(-1.0);
struct Point
{
double x,y,z;
Point(double a = ,double b = ,double c = ) :x(a),y(b),z(c) {}
friend Point operator + (Point a,Point b)
{
return Point(a.x+b.x,a.y+b.y,a.z+b.z);
}
friend Point operator - (Point a,Point b)
{
return Point(a.x-b.x,a.y-b.y,a.z-b.z);
}
};
double length(Point a)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
int main()
{
// freopen("in","r",stdin);
int T;
double h1,r1,h2,r2;
Point p1,p2,p;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf",&h1,&r1,&p1.x,&p1.y,&p1.z);
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&h2,&r2,&p2.x,&p2.y,&p2.z,&p.x,&p.y,&p.z);
Point o1 = p2,o2 = p1;
o2.z += (h1 - r1);
o1.z += (0.9 * h2 - r2);
Point oo = o2 - o1;
double temp = (oo.x*p.x + oo.y * p.y + oo.z * p.z) / length(oo) / length(p);
double angle = acos(temp); //求夹角
if(angle <= )
{
puts("NO");
continue;
}
double D = sin(angle) * length(oo);
printf(D <= r1? "YES\n":"NO\n");
}
return ;
}