HDU 5572 An easy Physics problem (calculates geometric point class line class vector class segment intersects the circle)

Source: Internet
Author: User
Tags cos sin
Topic links

2015 Shanghai Regional match 5th Question HDU5572 topic

On the smooth plane, there is a fixed circle, there are two points outside the circle, a, B, q A with the speed vector v motion can touch the B. (A elastic collision occurs when a circle is encountered) Analysis

Write this question when did not think clearly began to write, when the game must first make clear the idea, the situation is more of the question to draw tree chart or flowchart.
Read a big guy's blog to draw a clear flowchart, for reference.

Original blog Link code

#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include <cstdlib
> #include <queue> #include <map> #include <algorithm> #include <set> #include <stack>
using namespace Std;
typedef long long int LL;
typedef long double LD;
const long double eps=1e-8;
    int dcmp (long double x) {if (Fabs (x) <eps) return 0;
    if (x>0) return 1;
return-1;

} long double mysqrt (long double x) {return sqrt (max (long Double) 0,x)}
    struct point///DOT class {Long double x, y;
    Point () {} ' point (long double a,long double b): X (a), Y (b) {} void input () {cin>>x>>y; } friend bool operator = = (Const point &a,const point &b) {return dcmp (a.x-b.x) ==0 && DCM
    P (a.y-b.y) ==0;
}
};
typedef point vector;///Vector class vectors 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);} Vectoroperator * (vector a,long double t) {return vector (a.x*t,a.y*t);} Vector operator/(vector a,long double t) {return vector (a.x/t,a.y/t);} long double dot (vector a,vector B) {return a.x*b.
X+A.Y*B.Y;} Long double Cross (vector a,vector B) {return a.x*b.y-a.y*b.x;} long double length (vector A) {return mysqrt (dot (a,a));}/*lo ng Double Angle (Vector a,vector B) {return ACOs (dot (b)/length (A)/length (B)); For two vector angle vector Rotate (vector a,long double rad)////vector A rotates the rad arc counterclockwise (A.x*cos (RAD)-a.y*sin (RAD), A.x*sin, around the starting point
(RAD) +a.y*cos (RAD));
    }*/struct line///line class {point P;
    Vector v; Line (Point P,vector v):p (P), V (v) {}, GetPoint (long double t)///Get a little bit on the wire {return point (p.x+t*v.x,p.y+t
    *V.Y);
}
};
Point Project (Point A,line L) {return l.p+l.v* (dot (L.V,A-L.P)/dot (L.V,L.V));}
    Point Mirrorpoint (Point A,line L) {Vector d=project (a,l);
Return d+ (D-A); } void Circle_cross_line (Point a,point b,point o,long double r,point ret[],int &num)///For line segments andCircular intersection Template {long double x0=o.x,y0=o.y;
    a long double x1=a.x,y1=a.y;
    a long double x2=b.x,y2=b.y;
    a long double dx=x2-x1,dy=y2-y1;
    a long double a=dx*dx+dy*dy;
    Long double b=2*dx* (x1-x0) +2*dy* (y1-y0);
    Long double c= (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)-r*r;
    a long double delta=b*b-4*a*c;
    num=0;
        if (dcmp (delta) >=0) {long double t1= (-b-mysqrt (Delta))/(2*A);
        Long Double t2= (-b+mysqrt (Delta))/(2*A);
        if (dcmp (t1) >=0) ret[++num]=point (X1+t1*dx,y1+t1*dy);
    if (dcmp (T2) >=0) ret[++num]=point (X1+t2*dx,y1+t2*dy); }} int Get_dis (point A,point B) {return ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y)), bool Onray (point a,line L)//
    Whether breakpoint A is on the Ray L (p,v) {Vector w=a-l.p;
Return (DCMP (W,L.V) ==0&&dcmp (dot (w,l.v)) >0); } bool Onseg (point a,point b,point C)///Determine if the dot A is on the segment BC {return (DCMP (Cross (b-a,c-a)) ==0 && dcmp (dot (b-a,c-a)) &L
T;0);
  } int main () {int t,temp1,temp2,tot;  Point O,a,aa,a1,b,c,ret[5],p1,p2,p3;
    Vector V;
    Long double R;
    scanf ("%d", &t);
        for (int. tt=1;tt<=t;tt++) {printf ("Case #%d:", TT);
        O.input ();cin>>r; A.input ();
        V.input ();
        Line La=line (A,V);
        Aa=la.getpoint (1.0);

        B.input ();
        Circle_cross_line (A,aa,o,r,ret,tot);

        if (tot==2&&ret[1]==ret[2])////the same intersection with this template will be stored two times tot--;
            if (tot<=1)///If the circle does not change the motion trajectory of a (ray with circle absent or tangent) {if (Onray (B,la)) printf ("yes\n");
            else printf ("no\n");
        Continue
        } Temp1=get_dis (a,ret[1]);
        Temp2=get_dis (a,ret[2]);
        if (TEMP1&LT;TEMP2)////The closest intersection to a is the collision point C c=ret[1];

        else c=ret[2];
        if (Onseg (B,A,C))///If B is on the line segment AC printf ("yes\n");
            else {line oc=line (o,c-o); A1=mirrorpoint (A,OC);/////Do a symmetric point about OC ac if (Onray (b,lINE (C,A1-C))///Determine whether B is on the Ray CA1 printf ("yes\n");
        else printf ("no\n");
}} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.