题目链接
题目大意
有一个球是障碍物不能走,给你两个点,从起点到终点的最短路程是多少
解题思路
我真的是被这个题精度卡懵了(哭辽)
先说这个线段和球相交的问题,我们比赛的时候写的是线段和圆心的距离和半径比大小,莫名其妙的wa。(我写的真的是线段!!不是直线和圆心的距离,是线段!!!)
然后还有一种判断方式是:∠TSO和∠STO都是锐角(单独写还是wa)
所以要以上两个判断方法同时写!
然后就是相交如何走最短:
就是红色的路径了,从两个点做切线,然后再走弧线是最短的
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-8;
double pi=acos(-1.0)/2.0;
int sgn(double x)
{
if(fabs(x)<eps)
return 0;
if(x<0)
return -1;
else
return 1;
}
struct point3
{
double x,y,z;
point3 (double X=0,double Y=0,double Z=0)
{
x=X;
y=Y;
z=Z;
}
double len()
{
return sqrt(x*x+y*y+z*z);
}
double len2()
{
return x*x+y*y+z*z;
}
point3 operator - (const point3 &b) const
{
return point3(x-b.x,y-b.y,z-b.z);
}
point3 operator + (const point3 &b) const
{
return point3(x+b.x,y+b.y,z+b.z);
}
point3 operator ^ (const point3 &b) const
{
return point3(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
}
double operator * (const point3 &b) const
{
return x*b.x+y*b.y+z*b.z;
}
point3 operator / (const double &k) const
{
return point3(x/k,y/k,z/k);
}
double dis(const point3 &a) const
{
return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y)+(z-a.z)*(z-a.z));
}
double len2(const point3 &a) const
{
return (x-a.x)*(x-a.x)+(y-a.y)*(y-a.y)+(z-a.z)*(z-a.z);
}
point3 trunc(double r)
{
double l=len();
if(!sgn(l))
return *this;
r/=l;
return point3(x*r,y*r,z*r);
}
};
struct line3
{
point3 s,e;
line3(point3 S=0,point3 E=0)
{
s=S;
e=E;
}
point3 lineprog(point3 p)
{
if((e-s).len2()==0)
return 0;
return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));
}
double diss(point3 p)
{
if(s.dis(e)==0)
return 0;
return ((e-s)^(p-s)).len()/s.dis(e);
}
double disss(point3 p)
{
if(sgn((p-s)*(e-s))<0||sgn((p-e)*(s-e))<0)
return min(p.dis(s),e.dis(p));
return diss(p);
}
};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
point3 V;
double r;
line3 L;
scanf("%lf %lf %lf %lf",&V.x,&V.y,&V.z,&r);
scanf("%lf %lf %lf %lf %lf %lf",&L.s.x,&L.s.y,&L.s.z,&L.e.x,&L.e.y,&L.e.z);
double d=L.disss(V);
double dd1=L.s.len2(V);
double dd2=L.e.len2(V);
double dd3=L.e.len2(L.s);
double jj1=acos((dd1+dd3-dd2)/(2.0*sqrt(dd1*dd3)));
double jj2=acos((dd2+dd3-dd1)/(2.0*sqrt(dd2*dd3)));
if(pi-jj1>eps&&pi-jj2>eps)
{
double d1=L.s.dis(V);
double d2=L.e.dis(V);
double d3=L.e.dis(L.s);
double xx1=sqrt((d1*d1-r*r));
double xx2=sqrt((d2*d2-r*r));
double j1=acos(r/d1);
double j2=acos(r/d2);
double j11=acos((d1*d1+d2*d2-d3*d3)/(2.0*d1*d2));
double jj=j11-j1-j2;
double ans=r*jj;
ans+=xx1+xx2;
printf("%.7f\n",ans);
}
else
{
printf("%.7f\n",L.s.dis(L.e));
}
}
return 0;
}