BOJ - 17387 - 선분 교차 2

Updated:

import sys


def isCCW(p1, p2, p3):
    ret = p1[0] * p2[1] + p2[0] * p3[1] + p3[0] * p1[1]
    ret = ret - (p1[1] * p2[0]) - (p2[1] * p3[0]) - (p3[1] * p1[0])

    if ret > 0:
        return 1
    elif ret < 0:
        return -1
    else:
        return 0


def solution():
    x1, y1, x2, y2 = map(int, sys.stdin.readline().split())
    x3, y3, x4, y4 = map(int, sys.stdin.readline().split())

    r1 = isCCW([x1, y1], [x2, y2], [x3, y3])
    r2 = isCCW([x1, y1], [x2, y2], [x4, y4])
    r3 = isCCW([x3, y3], [x4, y4], [x1, y1])
    r4 = isCCW([x3, y3], [x4, y4], [x2, y2])

    if r1 * r2 == 0 and r3 * r4 == 0:
        if [x1, y1] > [x2, y2]: x1, y1, x2, y2 = x2, y2, x1, y1
        if [x3, y3] > [x4, y4]: x3, y3, x4, y4 = x4, y4, x3, y3

        if [x1, y1] <= [x4, y4] and [x3, y3] <= [x2, y2]:
            return True
        else:
            return False

    if r1 * r2 <= 0 and r3 * r4 <= 0:
        return True
    else:
        return False


print(1 if solution() else 0)

https://www.acmicpc.net/problem/17387

Categories:

Updated:

Leave a comment