문제


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


풀이


세 점 이상이 일직선 위에 있는 경우가 추가됐다.

선분의 한쪽 끝이 다른 선분 위에 있는 경우 선분 교차 1에서 계산했던 CCW 값 두 개의 곱이 0이 될 것이다.

그러므로 0 미만에서 0 이하로 수정하면 된다.

두 선분이 하나의 직선에서 나온 경우엔 CCW 값 네 개 모두 0이 된다.

이 경우 각 선분의 x값의 범위끼리 겹치는지, y값의 범위끼리 겹치는지 확인해 주면 된다.


코드


import sys

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


def ccw(x1, y1, x2, y2, x3, y3):
    return (x2 - x1) * (y3 - y2) - (x3 - x2) * (y2 - y1)


res1 = ccw(x1, y1, x2, y2, x3, y3)
res2 = ccw(x1, y1, x2, y2, x4, y4)
res3 = ccw(x3, y3, x4, y4, x1, y1)
res4 = ccw(x3, y3, x4, y4, x2, y2)

if res1 == res2 == res3 == res4 == 0:
    if (max(x1, x2) < min(x3, x4)) or (max(x3, x4) < min(x1, x2)) or (max(y1, y2) < min(y3, y4)) or (max(y3, y4) < min(y1, y2)):
        print(0)
    else:
        print(1)

elif (res1 * res2 <= 0) and (res3 * res4 <= 0):
    print(1)
else:
    print(0)

Leave a comment