Hdu 4458 Shoot The airplane (judging points within polygons)

Source: Internet
Author: User

Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=4458

Idea: Aircraft as the reference system, the plane is relatively static, the bullet plus the horizontal direction of Speed-V. You only need to enumerate the time to determine if the time point (bullet) is within the polygon (plane). Note that G can be 0, evenly variable and uniform. In addition, the accuracy of the subject is high, the point of judgment in the line with the difference in coordinates, to avoid using dot.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define Debuusing    Namespace Std;const Double eps = 1e-10;const int maxn=25;struct point{double x, y; Point (Double x = 0, double y = 0): x (x), Y (y) {}};typedef point vector;int n,v,b,g; Point P[MAXN]; Vector operator-(const point &a, const point &b) {return Vector (a.x-b.x, a.y-b.y);}    int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;} Double Dot (const vector &a, const vector &b) {return a.x * b.x + a.y * B.Y;} Double Cross (const vector &a, const vector B) {return a.x * b.y-a.y * b.x;}  BOOL Onsegment (const-point &p, const-point &a1, const-point-&AMP;A2) {//return dcmp (cross (a1-p, a2-p)) = = 0    && dcmp (Dot (A1-p, a2-p)) < 0;    if (dcmp (Cross (a1-p,a2-p))) return 0; else if (dcmp (P.x-min (a1.x,a2.x)) >=0&&dcmp (P.x-max (a1.x,a2.x)) <=0&&dcmp (P.y-min (A1.Y,A2.Y) ) &GT;=0&AMP;&AMP;DCMP (P.y-max (A1.Y,A2.Y)) <=0) return 1; else return 0;}    int Ispointinpolygon (point P, point *poly, int n) {int wn = 0;        for (int i = 0; i < n; ++i) {if (Onsegment (p, Poly[i], poly[(i+1)%n])) return 0;        int k=dcmp (Cross (poly[(i+1)%n]-poly[i], p-poly[i]));        int d1=dcmp (POLY[I].Y-P.Y);        int d2=dcmp (poly[(i+1)%n].y-p.y);        if (k>0&&d1<=0&&d2>0) wn++;    if (k<0&&d2<=0&&d1>0) wn--;    } if (wn!=0) return 1; else return 0;}    int main () {#ifdef debug Freopen ("In.in", "R", stdin), #endif//debug while (scanf ("%d%d%d", &v,&b,&g) ==3)        {if (v==0&&b==0&&g==0) break;        scanf ("%d", &n);        Double maxy=0.0;            for (int i=0; i<n; i++) {scanf ("%lf%lf", &p[i].x,&p[i].y);        Maxy=max (MAXY,P[I].Y);        } int flag=0; Double time=dcmp (g)?        (2.0*b/g):(1.0*maxy/b);      cout<<time<<endl;  For (double i=0.0, i<=time; i+=0.001) {point tmp;            Tmp.x=-v*i,tmp.y=b*i-0.5*g*i*i;                if (Ispointinpolygon (tmp,p,n)) {printf ("%.2f\n", I);                flag=1;            Break    }} if (!flag) printf ("miss!\n"); } return 0;}



Hdu 4458 Shoot The airplane (judging points within polygons)

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.