BOJ 17386 선분 교차 1

1 개요[ | ]

BOJ 17386 선분 교차 1

2 같이 보기[ | ]

3 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

#define Point pair<long,long>
Point a, b, c, d;

int ccw(Point p1, Point p2, Point p3) {
    long temp = p1.first*p2.second + p2.first*p3.second + p3.first*p1.second - p1.second*p2.first - p2.second*p3.first - p3.second * p1.first;
    if (temp > 0) return 1;
    if (temp < 0) return -1;
    return 0;
}

void solve() {
    int abc = ccw(a, b, c);
    int abd = ccw(a, b, d);
    int cda = ccw(c, d, a);
    int cdb = ccw(c, d, b);
    if (abc * abd == 0 && cda * cdb == 0) {
        if (a > b) swap(a, b);
        if (c > d) swap(c, d);
        if(a <= d && c <= b) {
            cout << 1;
        } else {
            cout << 0;
        }
    }
    if(abc * abd <= 0 && cda * cdb <= 0) {
        cout << 1;
    } else {
        cout << 0;
    }
}

int main() {
    cin >> a.first >> a.second;
    cin >> b.first >> b.second;
    cin >> c.first >> c.second;
    cin >> d.first >> d.second;
    solve();
}
#include <bits/stdc++.h>
using namespace std;

struct Point {
    long x;
    long y;
};
Point a, b, c, d;

int ccw(Point p1, Point p2, Point p3) {
    long temp = p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y*p2.x - p2.y*p3.x - p3.y*p1.x;
    if (temp > 0) return 1;
    if (temp < 0) return -1;
    return 0;
}

void solve() {
    int abc = ccw(a, b, c);
    int abd = ccw(a, b, d);
    int cda = ccw(c, d, a);
    int cdb = ccw(c, d, b);
    if (abc * abd == 0 && cda * cdb == 0) {
        if (a.x > b.x) swap(a, b);
        if (c.x > d.x) swap(c, d);
        if(a.x <= d.x && c.x <= b.x) {
            cout << 1;
        } else {
            cout << 0;
        }
    }
    if(abc * abd <= 0 && cda * cdb <= 0) {
        cout << 1;
    } else {
        cout << 0;
    }
}

int main() {
    cin >> a.x >> a.y;
    cin >> b.x >> b.y;
    cin >> c.x >> c.y;
    cin >> d.x >> d.y;
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}