HDU 4793 Collision 【计算几何】

题目链接:传送门

题目大意:给你一组数据 Rm, R, r, x, y, vx and vy 

               含义分别为:小圆的半径Rm,大圆的半径R(注:小圆和大圆的圆心都是原点)以及射击点的半径r,射击点的坐标(x,y)还有射击点的移动方向,射击点在遇到小圆的时候会反弹,遇到大圆的时候不做反应,问射击点在大圆中持续的时间。

               题目意思很容易理解吧.......

               我写的有点逗........没有用到数学知识,完全是用模板写的

               首先判断这个射击点的轨迹与(大圆的半径+r)为半径的圆是否相交,若不相交则直接返回0,这样的轨迹不可能与圆相交

               其次判断这个射击点的轨迹与(小圆的半径+r)为半径的圆是否相交,若不相交则不会产生反弹,则直接求出直线与大圆的两个交点计算即可

               若相交则产生反弹,求出(大圆半径+r)与(小圆半径+r)为半径的圆的交点,计算即可,注意结果要乘于2

贴代码:

          

#include <math.h>
#include <stdio.h>
#define eps 1e-8
struct point{double x,y;};
double max(double a,double b)
{
    if(a>b) return a;
    else return b;
}


double xmult(point p1,point p2,point p0){
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

double distance(point p1,point p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

double disptoline(point p,point l1,point l2){
	return fabs(xmult(p,l1,l2))/distance(l1,l2);
}

point intersection(point u1,point u2,point v1,point v2){
	point ret=u1;
	double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
			/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x+=(u2.x-u1.x)*t;
	ret.y+=(u2.y-u1.y)*t;
	return ret;
}

//判直线和圆相交,包括相切
int intersect_line_circle(point c,double r,point l1,point l2){
    /*printf("%lf\n",r+eps);
    printf("%lf\n",disptoline(c,l1,l2)); //dayu dengyu*/
    if(disptoline(c,l1,l2)-r>-eps) return 0;
    else return 1;
}

//判线段和圆相交,包括端点和相切
int intersect_seg_circle(point c,double r,point l1,point l2){
	double t1=distance(c,l1)-r,t2=distance(c,l2)-r;
	point t=c;
	if (t1<eps||t2<eps)
		return t1>-eps||t2>-eps;
	t.x+=l1.y-l2.y;
	t.y+=l2.x-l1.x;
	return xmult(l1,c,t)*xmult(l2,c,t)<eps&&disptoline(c,l1,l2)-r<eps;
}

//判圆和圆相交,包括相切
int intersect_circle_circle(point c1,double r1,point c2,double r2){
	return distance(c1,c2)<r1+r2+eps&&distance(c1,c2)>fabs(r1-r2)-eps;
}

//计算圆上到点p最近点,如p与圆心重合,返回p本身
point dot_to_circle(point c,double r,point p){
	point u,v;
	if (distance(p,c)<eps)
		return p;
	u.x=c.x+r*fabs(c.x-p.x)/distance(c,p);
	u.y=c.y+r*fabs(c.y-p.y)/distance(c,p)*((c.x-p.x)*(c.y-p.y)<0?-1:1);
	v.x=c.x-r*fabs(c.x-p.x)/distance(c,p);
	v.y=c.y-r*fabs(c.y-p.y)/distance(c,p)*((c.x-p.x)*(c.y-p.y)<0?-1:1);
	return distance(u,p)<distance(v,p)?u:v;
}

//计算直线与圆的交点,保证直线与圆有交点
//计算线段与圆的交点可用这个函数后判点是否在线段上
void intersection_line_circle(point c,double r,point l1,point l2,point& p1,point& p2){
	point p=c;
	double t;
	p.x+=l1.y-l2.y;
	p.y+=l2.x-l1.x;
	p=intersection(p,c,l1,l2);
	t=sqrt(r*r-distance(p,c)*distance(p,c))/distance(l1,l2);
	p1.x=p.x+(l2.x-l1.x)*t;
	p1.y=p.y+(l2.y-l1.y)*t;
	p2.x=p.x-(l2.x-l1.x)*t;
	p2.y=p.y-(l2.y-l1.y)*t;
}

//计算圆与圆的交点,保证圆与圆有交点,圆心不重合
void intersection_circle_circle(point c1,double r1,point c2,double r2,point& p1,point& p2){
	point u,v;
	double t;
	t=(1+(r1*r1-r2*r2)/distance(c1,c2)/distance(c1,c2))/2;
	u.x=c1.x+(c2.x-c1.x)*t;
	u.y=c1.y+(c2.y-c1.y)*t;
	v.x=u.x+c1.y-c2.y;
	v.y=u.y-c1.x+c2.x;
	intersection_line_circle(c1,r1,u,v,p1,p2);
}

int main ()
{
   double Rm,R,r,x,y,vx,vy;
   while(~scanf("%lf %lf %lf %lf %lf %lf %lf",&Rm,&R,&r,&x,&y,&vx,&vy))
   {
    point O; O.x=0; O.y=0;
    point P; P.x=x; P.y=y;
    point Pd; Pd.x=x+vx/10; Pd.y=y+vy/10;
    point p1,p2,p3,p4;
    if(distance(O,P)<distance(O,Pd))  {printf("0\n");continue;}

    if(intersect_line_circle(O,R+r,P,Pd)==0) {printf("0\n");continue;} //与大圆相离
    else if(intersect_line_circle(O,Rm+r,P,Pd)==0) //与小圆相离
    {
        intersection_line_circle(O,R+r,P,Pd,p1,p2);
         if(p1.x!=p2.x)
         printf("%.3lf\n",fabs((p1.x-p2.x)/vx));
         else
         printf("%.3lf\n",fabs((p1.y-p2.y)/vy));
    }
    else
    {
        intersection_line_circle(O,R +r,P,Pd,p1,p2);
        intersection_line_circle(O,Rm+r,P,Pd,p3,p4);
        if(p1.x!=p2.x)
        printf("%.3lf\n",2*fabs((max(p1.x,p2.x)-max(p3.x,p4.x))/vx));
        else
        printf("%.3lf\n",2*fabs((max(p1.y,p2.y)-max(p3.y,p4.y))/vy));
    }
   }
}
/*
5 20 1 0 100 0 -1
5 20 1 30 15 -1 0
5 20 1 0 21  1  0
*/



还有一点要注意的是判断射击点的方向,若是反向则直接输出0如何判断呢....... pd是移动一点点之后的,注意我的pd在vx和vy的基础上除了10

if(distance(O,P)<distance(O,Pd))  {printf("0\n");continue;}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值