2013 ACM区域赛长沙 C Collision HDU 4793

题意:在平面上0,0点,有一个半径为R的圆形区域,并且在0,0点固定着一个半径为RM(<R)的圆形障碍物,现在圆形区域外x,y,有一个半径 为r的,并且速度为vx,vy的硬币,如果硬币碰到了障碍物,将会保持原有的速度向反射的方向继续前进,现在给出R,RM,r,x,y,vx,vy,问硬币的任意部分在圆形区域中滑行多少时间?

思路:首先把R,RM加上r,就可以把硬币看做一个点来讨论了,然后算一下射线与这两个圆交点的个数,记作C1,C2,CM1,CM2,若与两个圆的交点数都是2,答案就是dis(c1,c2)-dis(cm1,cm2)之后再除以合成后的速度,若与大圆的交点数为2,小圆的交点数是0或者1,答案就是 dis(c1,c2)/速度,否则肯定是0了.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll; int dcmp(double x) {
return (x>eps)-(x<-eps);
} typedef struct Point {
double x, y;
Point(double x = , double y = ) : x(x), y(y) {}
Point operator+(const Point& p) const {
return Point(x+p.x, y+p.y );
}
Point operator-(const Point& p) const {
return Point(x-p.x, y-p.y );
}
Point operator*(const double d) const {
return Point(x*d, y*d );
}
Point operator/(const double d) const {
return Point(x/d, y/d );
}
void read() {
scanf("%lf%lf", &x, &y);
}
} Vector; inline double Dis(const Point& a, const Point& b) {
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
} struct Line {
Point P;
Vector v;
double ang;
Line() {}
Line(const Point& P, const Vector& v):P(P),v(v) {
ang = atan2(v.y,v.x);
}
bool operator<(const Line& L) const {
return ang<L.ang;
}
Point point(double t) {
return P+v*t;
}
}; struct Circle {
Point c;
double r;
Circle() {}
}; int GetLineCircleIntersection(Line L, Circle C, double &t1, double &t2, vector<Point> &sol) {
double a = L.v.x, b = L.P.x-C.c.x, c = L.v.y, d = L.P.y - C.c.y;
double e = a*a+c*c, f = *(a*b+c*d), g = b*b+d*d-C.r*C.r;
double delta = f*f-*e*g;
if(dcmp(delta)<) return ;
if(dcmp(delta)==) {
t1 = t2 = -f/(*e);
sol.push_back(L.point(t1));
return ;
}
t1 = (-f-sqrt(delta))/(*e);
sol.push_back(L.point(t1));
t2 = (-f+sqrt(delta))/(*e);
sol.push_back(L.point(t2));
if(dcmp(t1)< || dcmp(t2)<) return ;
return ;
} double R,RM,r,x,y,vx,vy;
int n,m,k; int main() {
// freopen("in.txt","r",stdin);
while(~scanf("%lf%lf%lf%lf%lf%lf%lf",&RM,&R,&r,&x,&y,&vx,&vy)) {
Line l1(Point(x,y),Point(vx,vy));
Circle c1,c2;
c1.r=RM+r;
c1.c.x=c1.c.y=0.0;
c2.c.x=c2.c.y=0.0;
c2.r=R+r;
double db1=0.0,db2=0.0;
vector<Point> crs1;
vector<Point> crs2;
int num2=GetLineCircleIntersection(l1,c1,db1,db2,crs2);
int num1=GetLineCircleIntersection(l1,c2,db1,db2,crs1);
// printf("num1:%d num2:%d\n",num1,num2);
double len=0.0,ans=0.0;
double vv=sqrt(vx*vx+vy*vy);
if (num1== && num2==) {
len=Dis(crs1[],crs1[])-Dis(crs2[],crs2[]);
printf("%.4lf\n",len/vv);
} else if (num1==) {
len=Dis(crs1[],crs1[]);
printf("%.4lf\n",len/vv);
} else {
printf("0.0000\n");
}
}
return ;
}
上一篇:对象图 Object Diagram


下一篇:JS(AS)中的原子操作