BOJ 17387 선분 교차 2

1 개요[ | ]

BOJ 17387 선분 교차 2

2 같이 보기[ | ]

3 C++[ | ]

#include <bits/stdc++.h>
using namespace std;
 
struct Point {
    long long x, y;
    bool operator<=(Point const &p1) {
        if(x == p1.x) {
            return y <= p1.y;
        }
        return x <= p1.x;
    }
};
Point p1, p2, p3, p4;

int ccw(const Point &a, const Point &b, const Point &c) {
    long long res = (a.x*b.y + b.x*c.y + c.x*a.y) - (b.x*a.y + c.x*b.y + a.x*c.y);
    if(res > 0) return 1;
    if(res < 0) return -1;
    return 0;
}
 
bool intersect() {
    int std1 = ccw(p1, p2, p3) * ccw(p1, p2, p4);
    int std2 = ccw(p3, p4, p1) * ccw(p3, p4, p2);
    if(std1 <= 0 && std2 <= 0) {
        if(std1 == 0 && std2 == 0) {
            if(p2 <= p1) swap(p1, p2);
            if(p4 <= p3) swap(p3, p4);
            return p1 <= p4 && p3 <= p2;
        }
        return true;
    }
    return false;
}
 
void solve(){
    if(intersect()) {
        cout << 1;
    } else {
        cout << 0;
    }
}
 
int main(){
    cin >> p1.x >> p1.y;
    cin >> p2.x >> p2.y;
    cin >> p3.x >> p3.y;
    cin >> p4.x >> p4.y;
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}