题意:给一个圆和圆外两点A、B,在A以给定的速度(方向)出发,若碰到圆则发生完全弹性碰撞,问能否经过B。
题解:把b点对称过去,判断与a点斜率是否等于v,然后考虑一些特殊情况,比如射线交不到圆,经过b点再交圆等,最后ok函数要写好。。。
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 200010
#define MOD 1000000007
int n,k;
struct Point
{
double x, y;
Point( double x = 0, double y = 0 ):x(x), y(y) { }
}a,b,st;
typedef Point Vector;
Vector v,vv;
struct Circle
{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r){}
Point point(double a)
{
return Point(c.x + cos(a)*r,c.y + sin(a)*r);
}
}c;
struct Line
{
Point p;
Vector v;
double ang;
Line(){}
Line(Point p,Vector v):p(p),v(v)
{
ang = atan2(v.y,v.x);
}
Point point(double t)
{
return Point(p.x+v.x*t,p.y+v.y*t);
}
bool operator < (const Line& L) const
{
return ang < L.ang;
}
};
Vector operator+( Vector A, Vector B ) //向量加
{
return Vector( A.x + B.x, A.y + B.y );
}
Vector operator-( Vector A, Vector B ) //向量减
{
return Vector( A.x - B.x, A.y - B.y );
}
Vector operator*( Vector A, double p ) //向量数乘
{
return Vector( A.x * p, A.y * p );
}
Vector operator/( Vector A, double p ) //向量数除
{
return Vector( A.x / p, A.y / p );
}
bool operator<( const Point& A, const Point& B ) //两点比较
{
return A.x < B.x || ( A.x == B.x && A.y < B.y );
}
int dcmp( double x ) //控制精度
{
if ( fabs(x) < eps )
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator==( const Point& a, const Point& b ) //两点相等
{
return dcmp( a.x - b.x ) == 0 && dcmp( a.y - b.y ) == 0;
}
double Dot( Vector A, Vector B ) //向量点乘
{
return A.x * B.x + A.y * B.y;
}
double Length( Vector A ) //向量模
{
return sqrt( Dot( A, A ) );
}
double Angle( Vector A, Vector B ) //向量夹角
{
return acos( Dot(A, B) / Length(A) / Length(B) );
}
double Cross( Vector A, Vector B ) //向量叉积
{
return A.x * B.y - A.y * B.x;
}
double Area2( Point A, Point B, Point C ) //向量有向面积
{
return Cross( B - A, C - A );
}
Vector Rotate( Vector A, double rad ) //向量旋转
{
return Vector( A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad) );
}
Vector Normal( Vector A ) //向量单位法向量
{
double L = Length(A);
return Vector( -A.y / L, A.x / L );
}
Point GetLineIntersection( Point P, Vector v, Point Q, Vector w ) //两直线交点
{
Vector u = P - Q;
double t = Cross( w, u ) / Cross( v, w );
return P + v * t;
}
double DistanceToLine( Point P, Point A, Point B ) //点到直线的距离
{
Vector v1 = B - A, v2 = P - A;
return fabs( Cross( v1, v2 ) ) / Length(v1);
}
double DistanceToSegment( Point P, Point A, Point B ) //点到线段的距离
{
if ( A == B )
return Length( P - A );
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if ( dcmp( Dot(v1, v2) ) < 0 )
return Length(v2);
else if ( dcmp( Dot(v1, v3) ) > 0 )
return Length(v3);
else
return fabs( Cross( v1, v2 ) ) / Length(v1);
}
Point GetLineProjection( Point P, Point A, Point B ) // 点在直线上的投影
{
Vector v = B - A;
return A + v*( Dot(v, P - A) / Dot( v, v ) );
}
bool SegmentProperIntersection( Point a1, Point a2, Point b1, Point b2 ) //线段相交,交点不在端点
{
double c1 = Cross( a2 - a1, b1 - a1 ), c2 = Cross( a2 - a1, b2 - a1 ),
c3 = Cross( b2 - b1, a1 - b1 ), c4 = Cross( b2 - b1, a2 - b1 );
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool OnSegment( Point p, Point a1, Point a2 ) //点在线段上,不包含端点
{
return dcmp( Cross(a1 - p, a2 - p) ) == 0 && dcmp( Dot( a1 - p, a2 - p ) ) < 0;
}
double PolygonArea( Point *p, int n ) //多边形有向面积
{
double area = 0;
for ( int i = 1; i < n - 1; ++i )
area += Cross( p[i] - p[0], p[i + 1] - p[0] );
return area / 2.0;
}
vector<Point> sol;
int getLineCircleIntersection(Line L,Circle C) //直线和圆的交点
{
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 = 2*(a*b + c*d),g = b*b + d*d - C.r*C.r;
double delta = f*f - 4*e*g;
if(dcmp(delta) < 0)
return 0;
double t1,t2;
if(dcmp(delta) == 0)
{
t1 = t2 = -f/(2*e);
sol.push_back(L.point(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e);
sol.push_back(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e);
sol.push_back(L.point(t2));
return 2;
}
double angle(Vector v) //计算向量极角
{
return atan2(v.y,v.x);
}
int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point>& sol) //计算两圆相交
{
double d = Length(C1.c - C2.c);
if(dcmp(d) == 0)
{
if(dcmp(C1.r - C2.r) == 0)
return -1;
return 0;
}
if(dcmp(C1.r + C2.r - d) < 0)
return 0;
if(dcmp(fabs(C1.r-C2.r) - d) > 0)
return 0;
double a = angle(C2.c - C1.c);
double da = acos(C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d);
Point p1 = C1.point(a-da),p2 = C1.point(a+da);
sol.push_back(p1);
if(p1 == p2)
return 1;
sol.push_back(p2);
return 2;
}
int getTangent(Point p,Circle C,Vector* v) //过定点做圆的切线
{
Vector u = C.c - p;
double dist = Length(u);
if(dist < C.r)
return 0;
else if(dcmp(dist - C.r) == 0)
{
v[0] = Rotate(u,pi/2);
return 1;
}
else
{
double ang = asin(C.r / dist);
v[0] = Rotate(u,-ang);
v[1] = Rotate(u,ang);
return 2;
}
}
int getTangents(Circle A,Circle B,Point* a,Point* b) //求两圆公切线
{
int cnt = 0;
if(A.r < B.r)
{
swap(A,B);
swap(a,b);
}
int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
int rdiff = A.r - B.r;
int rsum = A.r + B.r;
if(d2 < rdiff*rdiff)
return 0;
double base = atan2(B.c.y-A.c.y,B.c.x-A.c.x);
if(d2 == 0 && A.r == B.r)
return -1;
if(d2 == rdiff*rdiff)
{
a[cnt] = A.point(base);
b[cnt] = B.point(base);
cnt++;
return 1;
}
double ang = acos((A.r - B.r) / sqrt(d2));
a[cnt] = A.point(base+ang);
b[cnt] = B.point(base+ang);
cnt++;
a[cnt] = A.point(base-ang);
b[cnt] = B.point(base-ang);
cnt++;
if(d2 == rsum*rsum)
{
a[cnt] = A.point(base);
b[cnt] = B.point(pi+base);
cnt++;
}
else if(d2 > rsum*rsum)
{
double ang = acos((A.r+B.r) / sqrt(d2));
a[cnt] = A.point(base+ang);
b[cnt] = B.point(pi+base+ang);
cnt++;
a[cnt] = A.point(base-ang);
b[cnt] = B.point(pi+base-ang);
cnt++;
}
return cnt;
}
double torad( double deg ) //角度转弧度
{
return deg / 180.0 * acos( -1.0 );
}
void get_coord(double R,double lat,double Ing,double& x,double& y,double& z) //经纬度(角度)转化为空间坐标
{
lat = torad(lat);
Ing = torad(Ing);
x = R*cos(lat)*cos(Ing);
y = R*cos(lat)*sin(Ing);
z = R*sin(lat);
}
int ConvexHull( Point *p, int n, Point *ch ) //求凸包
{
sort( p, p + n );
n = unique( p, p + n ) - p;
int m = 0;
for ( int i = 0; i < n; ++i )
{
while ( m > 1 && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
ch[m++] = p[i];
}
int k = m;
for ( int i = n - 2; i >= 0; --i )
{
while ( m > k && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
ch[m++] = p[i];
}
if ( n > 1 )
--m;
return
m;
}
int isPointInPolygon( Point p, Point *poly, int n ) //判断一点是否在凸包内
{
int wn = 0;
for ( int i = 0; i < n; ++i )
{
Point& p1 = poly[i], p2 = poly[ (i + 1)%n ];
if ( p == p1 || p == p2 || OnSegment( p, p1, p2 ) ) return -1; //在边界上
int k = dcmp( Cross( p2 - p1, p - p1 ) );
int d1 = dcmp( p1.y - p.y );
int d2 = dcmp( p2.y - p.y );
if ( k > 0 && d1 <= 0 && d2 > 0 ) ++wn;
if ( k < 0 && d2 <= 0 && d1 > 0 ) --wn;
}
if ( wn ) return 1; //内部
return 0; //外部
}
bool checkConvexHullIntersection( Point *a, Point *b, int na, int nb ) //判断凸包是否相交
{
for ( int i = 0; i < na; ++i )
if ( isPointInPolygon( a[i], b, nb ) ) return true;
for ( int i = 0; i < nb; ++i )
if ( isPointInPolygon( b[i], a, na ) ) return true;
for ( int i = 0; i < na; ++i )
for ( int j = 0; j < nb; ++j )
if ( SegmentProperIntersection(a[i], a[ (i + 1) % na ], b[j], b[ (j + 1) % nb ] ) ) return true;
return false;
}
double get_dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ok(Vector v1,Vector v2)
{
if(fabs(v1.x*v2.y-v1.y*v2.x) < eps)
{
if(fabs(min(fabs(v1.x),fabs(v1.y))-0.0) < eps)
{
double x1 = fabs(v1.x) < eps?v1.y:v1.x;
double x2 = fabs(v2.x) < eps?v2.y:v2.x;
if(x1 * x2 < eps)
return 0;
return 1;
}
if(v1.x*v2.x < eps || v1.y*v2.y < eps)
return 0;
return 1;
}
return 0;
}
int main()
{
int t,C = 1;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&c.c.x,&c.c.y,&c.r);
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&v.x,&v.y,&b.x,&b.y);
double dis = DistanceToLine(c.c,a,Point(a.x+v.x,a.y+v.y));
printf("Case #%d: ",C++);
if(dis - c.r > eps)
{
if(ok(v,b-a))
printf("Yes\n");
else
printf("No\n");
continue;
}
sol.clear();
int k = getLineCircleIntersection(Line(a,v),c);
if(k == 1 || get_dis(sol[0],a) < get_dis(sol[1],a))
st = sol[0];
else
st = sol[1];
if(!ok(v,st-a))
{
if(ok(v,b-a))
printf("Yes\n");
else
printf("No\n");
continue;
}
if(ok(v,b-a))
{
if(fabs(dis - c.r) < eps)
printf("Yes\n");
else if(get_dis(st,a) >= get_dis(b,a))
printf("Yes\n");
else
printf("No\n");
continue;
}
vv.x = -(st.y - c.c.y);
vv.y = st.x - c.c.x;
//printf("%lf %lf\n",vv.x,vv.y);
dis = DistanceToLine(b,st,Point(st.x+vv.x,st.y+vv.y));
//printf("%lf\n",dis);
vv.x = st.x - c.c.x;
vv.y = st.y - c.c.y;
double kk = sqrt(vv.x*vv.x+vv.y*vv.y);
vv.x /= kk;
vv.y /= kk;
b.x -= 2 * vv.x * dis;
b.y -= 2 * vv.y * dis;
vv.x = b.x - a.x;
vv.y = b.y - a.y;
if(ok(v,vv))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}