Uvalive 4639 && spoj spoints && POJ 3805 && AOJ 1298 separate Points find out whether two convex packets intersect difficulty: 3

Source: Internet
Author: User
Tags cmath

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=2640

http://www.spoj.com/problems/SPOINTS/en/

http://poj.org/problem?id=3805

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1298

To have a straight line divided into two convex bags, two convex packets do not intersect, not tangent is necessary

In the absence of templates, my code, over the Poj,uva, and Spoj, but not aoj, and the correct code on the PAT found that the situation is not quite in line with the facts

The method is:

1. Determine if each point is inside or on the side of the convex hull

2. Determine if the two convex packages intersect each other

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std;    const double Eps=1e-8;const int maxn=205;int dcmp (double D) {if (Fabs (d) <eps) return 0; return d>0?1:-1;}    struct pnt{double x, y;        PNT (): X (0), y (0) {} pnt (double tx,double ty): X (TX), y (ty) {} PNT operator-(PNT p2) {PNT newp (X-P2.X,Y-P2.Y);    return NEWP;        } PNT operator + (PNT p2) {PNT newp (X+P2.X,Y+P2.Y);    return NEWP;        } PNT operator * (double D) {PNT newp (x*d,y*d);    return NEWP;        } PNT operator/(double D) {PNT newp (x/d,y/d);    return NEWP;    } double Dis (pnt p2) {return sqrt ((x-p2.x) * (x-p2.x) + (Y-P2.Y) * (Y-P2.Y));        } bool operator = = (PNT p2) {if (dcmp (x-p2.x) ==0&&dcmp (Y-P2.Y) ==0) return true;    return false; }};d ouble Cross (PNT p1,pnt p2) {return p1.x*p2.y-p1.y*p2.x;}    BOOL Cmpx (PNT p1,pnt p2) {if (p1.x!=p2.x) return p1.x<p2.x; Return P1.Y&LT;P2. Y;} PNT Base;bool cmp (PNT p1,pnt p2) {return cross (p1-base,p2-base) <0;} int Ispointinconvexpolygon (PNT p1,pnt * p,int N) {for (int i=0;i<n;i++) {PNT a=pnt (p[(i+1)%n].x-p[i].x,p[(i+1)        %N].Y-P[I].Y);        PNT B=pnt (P1.X-P[I].X,P1.Y-P[I].Y);        int fl=dcmp (A, b);        if (fl<0) return 0;            if (fl==0) {int Maxx=max (p[(i+1)%n].x,p[i].x);            int Minx=min (p[(i+1)%n].x,p[i].x);            int Maxy=max (p[(i+1)%n].y,p[i].y);            int Miny=min (p[(i+1)%n].y,p[i].y); if (MINX&LT;=P1.X&AMP;&AMP;MAXX&GT;=P1.X&AMP;&AMP;MINY&LT;=P1.Y&AMP;&AMP;MAXY&GT;=P1.Y) return-1;//on the Edge E        LSE return 0; }} return 1;}        int Graham (PNT * P,PNT * h,int n) {int m=0;                for (int i=0;i<n;i++) {//compute on convex hull while (M>1&&cross ((H[m-1]-h[m-2]), (P[i]-h[m-2])) <=0) {m--;}        H[m++]=p[i];        } int tm=m; for (int i=n-2;i>=0;i--) {//compute the convex hull while (m>tm& &cross ((H[m-1]-h[m-2]), (P[i]-h[m-2]) <=0) {m--;}        H[m++]=p[i];        } if (n>1) m--; return m;} bool Between (PNT P1,PNT P2,pnt p) {return (P.x<=max (p1.x,p2.x) &&p.x>=min (p1.x,p2.x)) &&amp  ; (P.y<=max (P1.Y,P2.Y) &&p.y>=min (P1.Y,P2.Y));}        BOOL Isinsert (PNT p11,pnt p12,pnt p21,pnt p22) {PNT v=p22-p21;        PNT w=p12-p11;        PNT u=p21-p11;                if (Cross (v,w) ==0) {if (Cross (v,u)!=0) return false;                if (between (P11,P12,P21)) return true;                if (between (P11,P12,P22)) return true;        return false;        } double T=cross (w,u)/cross (V,W);        Double T2=cross (v,u)/cross (V,W); if (t2>1| |        T2&LT;0) return false; if (t>1| |        T&LT;0) return false; return true;} PNT bp[maxn],bh[maxn],wp[maxn],wh[maxn];int Bn,wn,btop,wtop;int Main () {while (scanf ("%d%d", &bn,&wn) ==2&amp ;& (bn| | WN) {for (int i=0;i<bn;i++) {                        scanf ("%lf%lf", &bp[i].x,&bp[i].y);                } for (int i=0;i<wn;i++) {scanf ("%lf%lf", &wp[i].x,&wp[i].y);                } bool Fl=true;                        if (bn>1) {sort (bp,bp+bn,cmpx);                        Base =bp[0];                        Sort (bp+1,bp+bn,cmp);                        Btop=graham (BP,BH,BN);                                        if (fl) for (int i=0;i<wn;i++) {if (Ispointinconvexpolygon (wp[i],bh,btop)) {                                        Fl=false;                                Break                        }}} if (wn>1) {sort (wp,wp+wn,cmpx);                        Base =wp[0];                        Sort (wp+1,wp+wn,cmp);                        Wtop=graham (WP,WH,WN); if (fl) {for (int i=0;i<bn;i++) {if (Ispointinconvexpolygon (bp[i],wh,wtop)) {                                                Fl=false;                                        Break }}} if (Fl&&bn>1&&am                                        p;wn>1) {for (int. i=0;i<btop;i++) {for (int j=0;j<wtop;j++) {                                                if (Isinsert (bh[i],bh[(i+1)%btop],wh[j],wh[(j+1)%wtop])) {                                                Fl=false;                                        Break }}} if (WN==1&AMP;&AMP;BN==1&AMP;&A                Mp;bp[0]==wp[0]) {fl=false;}                if (fl) puts ("YES");        Else puts ("NO"); } return 0;}

The code here can be too: http://pelkira.hatenablog.jp/

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <map > #include <vector> #include <queue> #include <functional> #include <cstring> #include < Cstdlib> #include <complex>using namespace Std;typedef long long ll;typedef complex < double > point; typedef vector < point > polygon;namespace STD {bool operator < (point A,point b) {return real (a)! = Real (b)?    Real (a) < Real (b): Imag (a) < Imag (b);    }}const double EPS = 1e-8;const double INF = 1e12;struct line:polygon{line () {}; Line (point P,point q) {push_back (P);p ush_back (q);}}; Double Cross (point P,point q) {return imag (Conj (p) *q);} Double dot (point P,point q) {return real (CONJ (p) *q);}    int CCW (point A,point b,point c) {b-=a,c-=a; if (Cross (b,c) >0) return 1; Reverse-りif (Cross (b,c) <0) return-1; Time back to りif (dot (b,c) <0) return 2; Straight on にc-a-B if (norm (b) <norm (c)) return-2; Straight on にa-b-c RETUrn 0;         Straight on にa-c-B}bool intersectss (line s, line t) {return CCW (S[0],s[1],t[0]) *CCW (s[0],s[1],t[1]) <= 0 && CCW (T[0],t[1],s[0]) *CCW (t[0],t[1],s[1]) <= 0; BOOL Intersectsp (line S, point P) {return abs (S[0]-P) +abs (s[1]-p)-abs (S[1]-s[0]) < EPS;//Triangle Inequality}poly    Gon Convexhull (Polygon PS) {//convex packet int n = ps.size (), k = 0;    if (n = = 1) return PS;    Sort (Ps.begin (), Ps.end ());    Polygon CH (2*n);  for (int i = 0; i < n; ch[k++] = ps[i++])//Lower-hull while (K >= 2 && CCW (Ch[k-2], ch[k-1], ps[i])    <= 0)--k; for (int i = n-2, t = k+1; I >= 0; ch[k++] = ps[i--])//Upper-hull while (k >= T && CCW (CH[K-2], ch[    K-1], ps[i]) <= 0)--k;    Ch.resize (k-1); return ch;}    Point-vertexenum{out, ON, In};int Isinnerpointvertex (Polygon ps,point a) {bool FLG = false;        for (int i = 0;i < Ps.size (), i++) {point P = ps[i]-a,q = ps[(i+1)%ps.size ()]-A; if (Imag (p) >imag (q)) Swap (P,Q);        if (Imag (p) <=0&&0<imag (q)) {if (Cross (P,Q) < 0) FLG =!FLG;    } if (cross (p,q) = = 0 && dot (p,q) <= 0) return on; } return FLG? In:out;}    int main () {int n,m;        while (cin>>n>>m,n) {Polygon g,h;            for (int i = 0;i < n;i++) {double x, y;            cin>>x>>y;        G.push_back (Point (x, y));            } for (int i = 0;i < m;i++) {double x, y;            cin>>x>>y;        H.push_back (Point (x, y));        } g = Convexhull (g);        h = convexhull (h);        int g = G.size (), H = h.size ();        cout<<g<< "" <

  

Uvalive 4639 && spoj spoints && POJ 3805 && AOJ 1298 separate Points find out whether two convex packets intersect difficulty: 3

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.