Codeforces 994 C - Two Squares

C - Two Squares

思路:

点积叉积应用

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

struct P {
    double x, y;
    P(){}
    P(double x, double y):x(x), y(y) {}
    P operator - (P p) {
        return P(x-p.x, y-p.y);
    }
    double cross(P p) {
        return x*p.y - y*p.x;
    }
    double dot(P p) {
        return x*p.x + y*p.y;
    }
};
typedef P Vector;
bool on_seg(P p, Vector a, Vector b) {
    if((a-p).cross(b-p) == 0 && (a-p).dot(b-p) <= 0) return true;
    else return false;
}
double area2(Vector a, Vector b, Vector c) {
    return (b-a).cross(c-a);
}
bool intersect(Vector a, Vector b, Vector c, Vector d) {
    if(area2(a, c, d) == 0 && area2(b, c, d) == 0 && !on_seg(a, c, d) && !on_seg(b, c, d)
        || area2(a, c, d) * area2(b, c, d) > 0
        || area2(c, a, b) * area2(d, a, b) > 0
    ) return false;
    else return true;
}
pii a[4], b[4];
int main() {
    for (int i = 0; i < 4; i++) scanf("%d %d", &a[i].fi, &a[i].se);
    for (int i = 0; i < 4; i++) scanf("%d %d", &b[i].fi, &b[i].se);
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            Vector aa(a[i].fi, a[i].se);
            Vector bb(a[(i+1)%4].fi, a[(i+1)%4].se);
            Vector c(b[j].fi, b[j].se);
            Vector d(b[(j+1)%4].fi, b[(j+1)%4].se);
            if(intersect(aa, bb, c, d)) return 0*puts("YES");
        }
    }
    for (int i = 0; i < 4; i++) {
        int x = 0, y = 0, xx = 0, yy = 0;
        for (int j = 0; j < 4; j++) {
            Vector A(a[i].fi, a[i].se), B(b[j].fi, b[j].se), C(b[(j+1)%4].fi, b[(j+1)%4].se);
            if(area2(A, B, C) > 0) x++;
            else if(area2(A, B, C) < 0)y++;
            else x++, y++;
            Vector AA(b[i].fi, b[i].se), BB(a[j].fi, a[j].se), CC(a[(j+1)%4].fi, a[(j+1)%4].se);
            if(area2(AA, BB, CC) > 0) xx++;
            else if(area2(AA, BB, CC) < 0) yy++;
            else xx++, yy++;
        }
        if(x == 4 || y == 4 || xx == 4 || yy == 4) return 0*puts("YES");
    }
    puts("NO");
    return 0;
}

 

posted @ 2018-07-07 16:39  Wisdom+.+  阅读(348)  评论(0编辑  收藏  举报