POJ 3449 Geometric Shapes

Source: Internet
Author: User

To determine whether a polygon intersects, simply split the polygon into segments and see if the segments intersect.

One need to pay attention to the square vertex of the method, it is best not to use trigonometric functions to seek. Look at the methods in my Code.

And then it's better.


#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <cstdlib> #include <cmath> #include <map> #include <sstream> #include <queue> #include <vector> #define MAXN 100005 #define MAXM 211111 #define EPS 1e-8 #define INF 50000001 using
namespace Std;
    inline int dblcmp (double d) {if (Fabs (d) < EPS) return 0; Return d > EPS?
1:-1;
    } struct Point {double x, y; 
    Point () {} point (double _x, double _y): X (_x), Y (_y) {} void input () {scanf ("%lf%lf", &x, &y);
    } bool operator = = (Point a) const {return dblcmp (a.x-x) = = 0 && dblcmp (a.y-y) = = 0;
    } point Sub (point P) {return point (x-p.x, y-p.y);
    } double dot (point P) {return x * p.x + y * P.Y;
    } double det (point p) {return x * p.y-y * p.x;
    } double distance (point p) {    Return Hypot (x-p.x, Y-P.Y);
}}p[33];
    struct Line {point A, B;
    Line () {} line (point _a, point _b) {a = _a; b = _b;}
        void input () {a.input ();
    B.input ();
        } int segcrossseg (line v) {int d1 = dblcmp (B.sub (a). Det (V.a.sub (a)));
        int d2 = DBLCMP (B.sub (a). Det (V.b.sub (a)));
        int d3 = DBLCMP (V.b.sub (V.A). Det (A.sub (V.A)));
        int d4 = dblcmp (V.b.sub (V.A). Det (B.sub (V.A)));
        if (d1 ^ d2) = =-2 && (d3 ^ d4) = =-2) return 2;
                return (D1 = = 0 && dblcmp (v.a.sub (a). dot (v.a.sub (b))) <= 0| |
                D2 = = 0 && dblcmp (v.b.sub (a). dot (v.b.sub (b))) <= 0| |
                D3 = = 0 && dblcmp (a.sub (V.A). Dot (A.sub (V.B))) <= 0| |
    D4 = = 0 && dblcmp (b.sub (V.A). Dot (B.sub (V.B))) <= 0);
        } int linecrossseg (line v)//v is seg {int d1 = dblcmp (B.sub (a). Det (V.a.sub (a)));
        int d2 = DBLCMP (B.sub (a). Det (V.b.sub (a))); IF ((d1 ^ d2) = =-2) return 2;
    return (D1 = = 0 | | d2 = = 0);
        } point CrossPoint (line v) {Double A1 = V.b.sub (V.A). Det (A.sub (V.A));
        Double A2 = V.b.sub (V.A). Det (B.sub (V.A));
    Return Point ((a.x * a2-b.x * A1)/(A2-A1), (A.Y * A2-B.Y * A1)/(A2-A1));
}

};
    struct Node {string name;
vector<line>seg;
};
Char sa[55], sb[55];
vector<node>g;
vector<string>ans[55];
    BOOL CMP (node x, node Y) {return x.name < y.name;} bool ok (int k1, int k2) {int sz1 = G[k1].seg.size ();
    int sz2 = G[k2].seg.size (); for (int i = 0, i < sz1; i++) for (int j = 0; J < sz2; J + +) if (G[k1].seg[i].segcrossseg (g[k2].seg[
    J])) return true;
return false;
    } void Solve () {for (int i = 0; i <; i++) ans[i].clear ();
    Sort (G.begin (), G.end (), CMP);
    int sz = G.size (); for (int i = 0, i < sz; i++) {for (int j = 0; J < Sz; J + +) {if (i = = j) ContinUe
        if (ok (i, J)) Ans[i].push_back (g[j].name); }} for (int i = 0; i < sz; i++) {if (ans[i].size () = = 0) printf ('%s has no intersections\n ', G[I].N
        Ame.c_str ()); else {if (ans[i].size () = = 1) {printf ("%s intersects with%s\n", G[i].nam
            E.c_str (), Ans[i][0].c_str ()); } else if (ans[i].size () = = 2) {printf ("%s intersects with%s and%s\n", G[i].name
            . C_STR (), Ans[i][0].c_str (), Ans[i][1].c_str ());
                } else {printf ("%s intersects with", G[I].NAME.C_STR ());
                for (int j = 0; J < ans[i].size ()-1; j + +) printf ("%s,", Ans[i][j].c_str ());
            printf ("and%s\n", Ans[i][ans[i].size ()-1].c_str ());
    }}} g.clear ();
printf ("\ n");
    } int main () {double xa, ya, xb, YB; while (scanf ("%s", sa)! = EOF) {if (strcmp (SA, "-") = =0) {solve ();
        Continue
        } if (strcmp (SA, ".") = = 0) break;
        node tmp;
        Tmp.name = sa;
        scanf ("%s", SB);
            if (sb[0] = = ' s ') {point A, C;
            scanf ("(%LF,%LF)", &a.x, &AMP;A.Y);
            scanf ("(%LF,%LF)", &c.x, &AMP;C.Y);
            Point B, D;
            Double x, y, mx, my;
            MX = (a.x + c.x)/2.0, my = (a.y + c.y)/2.0;    x = A.X-MX;
            y = a.y-my;   b.x =-y + mx;
            B.Y = x + my;    x = C.X-MX;
            y = c.y-my; D.x =-y + mx;
            D.Y = x + my;
            Tmp.seg.push_back (Line (A, b));
            Tmp.seg.push_back (line (b, c));
            Tmp.seg.push_back (Line (A, d));
        Tmp.seg.push_back (Line (c, D));
            } else if (sb[0] = = ' L ') {point A, B;
            scanf ("(%LF,%LF)", &a.x, &AMP;A.Y);
            scanf ("(%LF,%LF)", &b.x, &AMP;B.Y); Tmp.seg.push_Back (line (a, b));
            } else if (sb[0] = = ' t ') {Point A, B, C;
            scanf ("(%LF,%LF)", &a.x, &AMP;A.Y);
            scanf ("(%LF,%LF)", &b.x, &AMP;B.Y);
            scanf ("(%LF,%LF)", &c.x, &AMP;C.Y);
            Tmp.seg.push_back (Line (A, b));
            Tmp.seg.push_back (Line (A, c));
        Tmp.seg.push_back (line (b, c));
            } else if (sb[0] = = ' R ') {point A, B, C, D;
            scanf ("(%LF,%LF)", &a.x, &AMP;A.Y);
            scanf ("(%LF,%LF)", &b.x, &AMP;B.Y);
            scanf ("(%LF,%LF)", &c.x, &AMP;C.Y);
            Double dx = b.x-a.x;
            D.x = C.X-DX;
            Double dy = b.y-a.y;
            D.y = C.y-dy;
            Tmp.seg.push_back (Line (A, b));
            Tmp.seg.push_back (line (b, c));
            Tmp.seg.push_back (Line (A, d));
        Tmp.seg.push_back (Line (c, D));
            } else {int t; scanf ("%d", &amP;T);
            for (int i = 0; i < T; i++) scanf ("(%LF,%LF)", &p[i].x, &AMP;P[I].Y);
        for (int i = 0; i < T; i++) Tmp.seg.push_back (line (P[i], p[(i + 1)% T]));
    } g.push_back (TMP);
} 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.