HDU 5572 An easy Physics problem (physics, computational geometry)

Source: Internet
Author: User
Tags cos hypot sin

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5572


Test instructions

A quality penalty, a fixed rigid cylinder
Given cylinder center coordinates, RADIUS
Starting point coordinates, starting direction of motion (vector)
End coordinates
Ask if you can reach the end, if you encounter a cylinder in the ball motion (basic physics knowledge)

In addition, the starting and ending points of the ball are not in the cylinder.


This problem has been stuck in my head for a long time. Always thought is the problem of precision, did not expect the last is a strategic problem. Find some code on the Internet to see, found some code even vector collinear are not judged incredibly can be over ... Why do you have to card me ah.

It took hours, and I didn't know what was harvested ... There is no point in ....

The correct strategy should be first to determine whether the segment AB is a round banana, do not hand over the direction, intersect on the normal, seeking symmetry points, in the direction of judgment, specific look at the code bar.

Now it is clear that judging the vector with the same direction as long as the common line based on the judgment dot product on the line. Also proficient in the following symmetry points, as well as some round operation ...


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
vector> #include <queue> #include <cmath> using namespace std;
Const double EPS = 1e-8;
Const double PI = ACOs (-1.0);
    int SGN (double x) {if (Fabs (x) < EPS) return 0;  
    if (x < 0) return-1; 
else return 1;
    } struct Point {double x, y;  
    Point () {}, point (double _x,double _y) {x = _x;y = _y;  
    } point operator + (const-point-&b) const {return point (x + b.x,y + b.y);  
    } Point operator-(const point &b) const {return point (X-B.X,Y-B.Y);  
    } double operator ^ (const point &b) const {return x*b.y-y*b.x;  
    } Double operator * (const point &b) Const {return x*b.x + y*b.y;
    } Point operator * (const double &k) Const{return point (X*k, y*k);}
    Point operator/(const double &k) Const{return point (x/k, y/k);} void TransXY (double B) {Double tx = X,ty = y;   
        x = Tx*cos (b)-Ty*sin (b);  
    y = Tx*sin (b) + ty*cos (b);
    } double Len () {return hypot (x, y);}
    Double Len2 () {return x*x + y*y;}
    Double distance (point P) {return hypot (x-p.x, y-p.y);};
        Point Trunc (Double R) {Double L = len ();
        if (!SGN (l)) return *this;
        R/= L;
    Return point (X*r, y*r);


}
}; 
    struct line {point s,e;  
    Line () {_s,point _e) {s = _s;e = _e;
    } double Length () {return s.distance (e);}
        Pair<int,point> operator & (const line &b) Const {Point res = s;             
            if (sgn (s-e) ^ (B.S-B.E)) = = 0) {if (sgn (S-B.E) ^ (B.S-B.E) = = 0) return Make_pair (0,res);         
        else return Make_pair (1,res);
        } Double t = ((S-B.S) ^ (B.S-B.E))/((S-E) ^ (B.S-B.E));         
        Res.x + = (e.x-s.x) *t;         
        Res.y + = (e.y-s.y) *t; Return MAKE_pair (2,res);
        } int relation (point p) {int c = sgn (p-s) ^ (e-s));
        if (c < 0) return 1;
        else if (C > 0) return 2;
    else return 3;
    } double Dispointtoline (point P) {return fabs ((p-s) ^ (e-s))/length ();} Double dispointtoseg (point P) {if (SGN (p-s) * (e-s)) < 0 | | sgn ((P-E) * (S-E)) < 0) return
        Min (p.distance (s), P.distance (e));
    Return Dispointtoline (P);
    } point Lineprog {return s+ (((e-s) * ((e-s) * (p-s))/((e-s). Len2 ()));}
        Point Symmetrypoint (Point P) {Point q = Lineprog (p);
    Return point (2*q.x-p.x, 2*q.y-p.y);

}
};
    struct Circle {point P;
    Double R;
        int Relationline (line v) {Double dst = v.dispointtoline (p);
        if (SGN (Dst-r) < 0) return 2;
        else if (SGN (dst-r) = = 0) return 1;
    return 0; } int Pointcrossline (line V, point &p1, point &p2) {if (!) (
   *this). Relationline (v)) return 0;     Point A = V.lineprog (p);
        Double d = v.dispointtoline (p);
        D = sqrt (r*r-d*d);
        if (SGN (d) = = 0) {P1 = A; p2 = A; return 1;}
        P1 = a + (V.E-V.S). Trunc (d);
        P2 = A-(V.E-V.S). Trunc (d);
    return 2;
        } int relationseg (line v) {Double dst = v.dispointtoseg (p);
        if (SGN (Dst-r) < 0) return 2;
        else if (SGN (dst-r) = = 0) return 1;
    else return 0;

}
};
    int main () {int t;
    scanf ("%d", &t);
    Circle C;
    Point A, V, b;
        for (int o = 1; o <= t; o++) {printf ("Case #%d:", O);
        scanf ("%lf%lf%lf", &c.p.x, &c.p.y, &AMP;C.R);
        scanf ("%lf%lf%lf%lf", &a.x, &a.y, &v.x, &AMP;V.Y);
        scanf ("%lf%lf", &b.x, &AMP;B.Y);
        Line AB = line (a, b);
        Point av = a + V;
        Line L = line (A, AV);
        int flag = 0;
       if (l.relation (b) = = 3 && sgn ((b-a) * (av-a)) > 0 && c.relationseg (AB) <= 1) flag = 1; else {point P1, p2;
                if (C.pointcrossline (L, p1, p2) = = 2) {Point m = sgn (A.distance (p1)-a.distance (p2)) < 0? p1:p2;
                Line F (C.P, M);
                Point q = F.symmetrypoint (a);
                Line TMP (M, q);
            if (tmp.relation (b) = = 3 && sgn ((b-m) * (q-m)) >0) flag = 1; }} puts (flag? ")
    Yes ":" No ");
} 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.