Coding Test/BaekJoon_Python

백준 11758 <CCW> Python

JunOnJuly 2023. 12. 17. 20:27
728x90

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

 

11758번: CCW

첫째 줄에 P1의 (x1, y1), 둘째 줄에 P2의 (x2, y2), 셋째 줄에 P3의 (x3, y3)가 주어진다. (-10,000 ≤ x1, y1, x2, y2, x3, y3 ≤ 10,000) 모든 좌표는 정수이다. P1, P2, P3의 좌표는 서로 다르다.

www.acmicpc.net


CCW 알고리즘을 사용하면 쉽게 풀 수 있습니다.


import math


def solution(data_list):
    # x, y 좌표 분리
    x_list = []
    y_list = []
    for x, y in data_list:
        x_list.append(x)
        y_list.append(y)
    x_list.append(data_list[0][0])
    y_list.append(data_list[0][1])
    # CCW 알고리즘
    sum_ccw = 0
    for i in range(3):
        sum_ccw += x_list[i]*y_list[i+1]
        sum_ccw -= y_list[i]*x_list[i+1]
    # 합이 0 이상이면 반시계
    # 합이 0 이하면 시계
    # 합이 0 이면 일직선
    if sum_ccw > 0:
        print(1)
    elif sum_ccw < 0:
        print(-1)
    else:
        print(0)


data_list = [list(map(int, input().split())) for _ in range(3)]
solution(data_list)

 

728x90