B - Collision
There’s a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There’s also a round range which shares exact the same center as the round medal, and radius of the medal is strictly less than radius of the round range. Since that the round medal is fixed and the coin is a piece of solid metal, we can assume that energy of the coin will not lose, the coin will collide and then moving as reflect.
Now assume that the center of the round medal and the round range is origin ( Namely (0, 0) ) and the coin’s initial position is strictly outside the round range. Given radius of the medal Rm, radius of coin r, radius of the round range R, initial position (x, y) and initial speed vector (vx, vy) of the coin, please calculate the total time that any part of the coin is inside the round range.
Please note that the coin might not even touch the medal or slip through the round range.
Input
There will be several test cases. Each test case contains 7 integers Rm, R, r, x, y, vx and vy in one line. Here 1 ≤ Rm < R ≤ 2000, 1 ≤ r ≤ 1000, R + r < |(x, y)| ≤ 20000, 1 ≤ |(vx, vy)| ≤ 100.
Output
For each test case, please calculate the total time that any part of the coin is inside the round range. Please output the time in one line, an absolute error not more than 1e-3 is acceptable.
Sample Input
5 20 1 0 100 0 -1
5 20 1 30 15 -1 0
Sample Output
30.000
29.394
题意:
v在平面上0,0点,有一个半径为R的圆形区域,并且在0,0点固定着一个半径为RM(<R)的圆形障碍物,现在圆形区域外x,y,有一个半径为r的,并且速度为vx,vy的硬币,如果硬币碰到了障碍物,将会保持原有的速度向反射的方向继续前进,现在给出R,RM,r,x,y,vx,vy,问硬币的任意部分在圆形区域中滑行多少时间?
思路:
对于圆的碰撞问题,我们可以使用缩点这个技巧。把半径为 r 的圆看成一个点,让另外两个圆的半径增加 r,那么我们只需求该点经过两个圆环之间的时间即可。
可以根据t,写出点的移动坐标方程 (x+t∗vx,y+t∗vy),分别带入两个圆的方程即可。如果解得的t是负值,则证明该点不会碰撞圆。
#include<bits/stdc++.h>
using namespace std;
const double EPS=1e-10;
double R,Rm,r,x,y,vx,vy;
int ff(double a,double b,double c,double &x1,double &x2)
{
double m=b*b-4*a*c;
if(m<0.0)
return 0;
m=sqrt(m);
double xa=(-b+m)/(a*2.0),xb=(-b-m)/(a*2.0);
x1=min(xa,xb);
x2=max(xa,xb);
if(m*m<EPS)
return 1;
else
return 2;
}
int main()
{
while(cin>>Rm>>R>>r>>x>>y>>vx>>vy)
{
Rm+=r;
R+=r;
double t1=1,t2=1,t3,t4;
int num1,num2;
num1=ff(vx*vx+vy*vy,2*vx*x+2*vy*y,-R*R+x*x+y*y,t1,t2);
num2=ff(vx*vx+vy*vy,2*vx*x+2*vy*y,-Rm*Rm+x*x+y*y,t3,t4);
if(t1<0.0||t2<0.0)
{
printf("0.000000000\n");
}
else if(num2!=2&&num1!=2){
puts("0.00000000");
}
else if(num1==2&&num2!=2){
double t=t2-t1;
printf("%.10f\n",t);
}
else{
double t=t2-t1-(t4-t3);
printf("%.10f\n",t);
}
}
return 0;
}