BOJ 17387 선분 교차 2

Jmnote (토론 | 기여)님의 2024년 1월 13일 (토) 10:55 판 (→‎C++)

1 개요

BOJ 17387 선분 교차 2

2 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;
    }
};
struct Line {
    Point p1;
    Point p2;
};
Line line[2];
 
int ccw(const Point &p1, const Point &p2, const Point &p3){
    long long res = (p1.x*p2.y + p2.x*p3.y + p3.x*p1.y) - (p2.x*p1.y + p3.x*p2.y + p1.x*p3.y);
    if(res > 0) return 1;
    if(res < 0) return -1;
    return 0;
}
 
bool intersect(Line& l1, Line &l2){
    int std1 = ccw(l1.p1, l1.p2, l2.p1) * ccw(l1.p1, l1.p2, l2.p2);
    int std2 = ccw(l2.p1, l2.p2, l1.p1) * ccw(l2.p1, l2.p2, l1.p2);
    if(std1 <= 0 && std2 <= 0) {
        if(std1 == 0 && std2 == 0) {
            if(l1.p2 <= l1.p1) swap(l1.p1, l1.p2);
            if(l2.p2 <= l2.p1) swap(l2.p1, l2.p2);
            return l1.p1 <= l2.p2 && l2.p1 <= l1.p2;
        }
        return true;
    }
    return false;
}
 
void solve(){
    if(intersect(line[0], line[1])) {
        cout << 1;
    } else {
        cout << 0;
    }
}
 
int main(){
    Point p1, p2;
    for(int i = 0 ; i < 2; i++){
        cin >> p1.x >> p1.y >> p2.x >> p2.y;
        line[i].p1 = p1;
        line[i].p2 = p2;
    }
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}