hdu5572(计算几何)

An Easy Physics Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3497    Accepted Submission(s): 690


Problem Description
On an infinite smooth table, there's a big round fixed cylinder and a little ball whose volume can be ignored.

Currently the ball stands still at point  A, then we'll give it an initial speed and a direction. If the ball hits the cylinder, it will bounce back with no energy losses.

We're just curious about whether the ball will pass point  B after some time.
 

Input
First line contains an integer  T, which indicates the number of test cases.

Every test case contains three lines.

The first line contains three integers  Ox Oy and  r, indicating the center of cylinder is  (Ox,Oy) and its radius is  r.

The second line contains four integers  Ax Ay Vx and  Vy, indicating the coordinate of  A is  (Ax,Ay) and the initial direction vector is  (Vx,Vy).

The last line contains two integers  Bx and  By, indicating the coordinate of point  B is  (Bx,By).

 1 ≤  T ≤ 100.

 | Ox|,| Oy|≤ 1000. 

 1 ≤  r ≤ 100.

 | Ax|,| Ay|,| Bx|,| By|≤ 1000. 

 | Vx|,| Vy|≤ 1000.

  Vx0 or  Vy0.

 both A and B are outside of the cylinder and they are not at same position.
 

Output
For every test case, you should output " Case #x: y", where  x indicates the case number and counts from  1 y is " Yes" if the ball will pass point  B after some time, otherwise  y is " No".
 

Sample Input
 
  
20 0 12 2 0 1-1 -10 0 1-1 2 1 -11 2
 

Sample Output
 
  
Case #1: NoCase #2: Yes
 

Source


题意:给出一个点A,点B,一个圆柱,点A有个速度,问点A能否撞到B(碰撞圆柱)。

思路:注意细节。精度问题。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const double eps = 1e-8;
const double pi= 3.14159265358979323846 ;
bool equal(double v1, double v2){
	return fabs(v1 - v2) < eps;
}
void func(double x,double y,double vx,double vy,double bx,double by,int tt)
{
    double db;
    if(!equal(vx,0))db=fabs(vy/vx*bx-by+y-vy/vx*x);
    else db=fabs(bx-x);
    if(equal(db,0))
    {
        double xx=bx-x,yy=by-y;
        if(xx*vx+yy*vy>eps)
        {
            printf("Case #%d: Yes\n",tt);
        }
        else
        {
            printf("Case #%d: No\n",tt);
        }
    }
    else
    {
        printf("Case #%d: No\n",tt);
    }
}
int main()
{
    double x,y,vx,vy;
    double ox,oy,r;
    double bx,by;
    int t;
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++)
    {
        scanf("%lf%lf%lf",&ox,&oy,&r);
        scanf("%lf%lf%lf%lf",&x,&y,&vx,&vy);
        scanf("%lf%lf",&bx,&by);
        if(equal(bx,x)&&equal(by,y))   //两点重合
        {
            printf("Case #%d: Yes\n",tt);
            continue;
        }
        double k,d,v;
        //计算直线离圆心距离
        if(vx==0)
        {
            d=fabs((x-ox));
        }
        else
        {
            k=vy/vx;
            d=fabs((k*ox-oy+y-k*x)/sqrt(k*k+1));
        }
        if(d>r||equal(d,r))  //撞不到
        {
            func(x,y,vx,vy,bx,by,tt);
        }
        else
        {
            double xt,yt;
            if(!equal(vx,0))
            {    
                double a = k*k+1;
                double b= -2*k*k*x-2*ox+2*k*y-2*k*oy;
                double c = ox*ox+k*k*x*x+y*y-2*k*y*x+2*k*oy*x-2*oy*y+oy*oy-r*r;
                double detla = b*b - 4 * a *c;
                double x1 = (-b+sqrt(detla))/2/a;
                double x2 = (-b-sqrt(detla))/2/a;
                if(fabs(x1-x)>fabs(x2-x))
                    xt=x2;
                else
                    xt=x1;
                yt=k*xt-k*x+y;
            }
            else
            {
                double y1,y2;
                double ys=sqrt(r*r-(x-ox)*(x-ox));
                y1=-ys+oy;
                y2=ys+oy; 
                xt=x;
                if(fabs(y1-y)>fabs(y2-y))  //求出交点
                {
                    yt=y2;
                }
                else
                {
                    yt=y1;
                }
            }
            if(equal(xt,bx)&&equal(yt,by))  //两点重合
            {
                printf("Case #%d: Yes\n",tt);
            }
            double xl=xt-x,yl=yt-y;
            if(xl*vx+yl*vy<0)   //向量反向
            {
                func(x,y,vx,vy,bx,by,tt);
                continue;
            }
            bool flag=true;
            if(!equal(vx,0))   //在原先直线上
            {
                double db=fabs(k*bx-by+y-k*x);
                if(equal(db,0))
                {
                    double xx=bx-x,yy=by-y;
                    double xxx=xt-bx,yyy=yt-by;
                    if(xx*vx+yy*vy>0&&xxx*vx+yyy*vy>0)
                    {
                        printf("Case #%d: Yes\n",tt);
                    }
                    else
                    {
                        flag=false;
                    }
                    if(flag)continue;
                }
            }
            else       //在原先直线上
            {
                double db=fabs(bx-x);
                if(equal(db,0))
                {
                    double xx=bx-x,yy=by-y;
                    double xxx=xt-bx,yyy=yt-by;
                    if(xx*vx+yy*vy>eps&&xxx*vx+yyy*vy>eps)
                    {
                        printf("Case #%d: Yes\n",tt);
                    }
                    else
                    {
                        flag=false;
                    }
                    if(flag)continue;
                }
            }
            double vx1,vy1;
            double vx2,vy2;
            
            vx1=xt-ox;
            vy1=yt-oy;
            vx=-vx;vy=-vy;
            vx2=((vx1)*(vx1)*vx+vy*vx1*vy1)/((vy1)*(vy1)+(vx1)*(vx1));
            vx2=vx2-(vx-vx2);
            if(equal(vy1,0))
            {
                vy2=-vy;
            }
            else
            {
                vy2=-vx1/vy1*(vx2-vx)+vy;
            }
            double db;
            func(xt,yt,vx2,vy2,bx,by,tt);
        }
    }
}







猜你喜欢

转载自blog.csdn.net/qq_25576697/article/details/80886119
今日推荐