본문 바로가기
Algorithm/Math

[백준] 11758. CCW / Java

by Dev_Green 2023. 6. 20.

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


문제 풀이

이 문제는 두 벡터의 외적을 구하는 공식으로 풀 수 있는 문제이다. 

일명 '신발끈 공식'으로

(빨간색 빗금으로 이어진 것들의 곱의 합) - (파란색 빗금으로 이어진 것들의 곱의 합) 이다.

즉, 아래와 같은 식이다.

(x1*y2 + x2*y3 + x3*y1) - (y1*x2 + y2*x3 + y3*x1)

계산 결과가

양수이면 반시계 방향을 의미하고, 

음수이면 시계 방향이며, 

0이면 같은 방향이다.

소스 코드

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int x1 = sc.nextInt();
        int y1 = sc.nextInt();

        int x2 = sc.nextInt();
        int y2 = sc.nextInt();

        int x3 = sc.nextInt();
        int y3 = sc.nextInt();

        int value = (x1*y2 + x2*y3 + x3*y1) - (y1*x2 + y2*x3 + y3*x1);

        if (value < 0) {
            System.out.println(-1);
        } else if (value > 0) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }

}

'Algorithm > Math' 카테고리의 다른 글

[Math] 부분집합과 멱집합 / Java  (0) 2023.08.14
[Math] 순열(Permutation) / Java  (0) 2023.08.13
순열(Permutation)  (0) 2023.01.09
조합(Combination), 중복조합  (0) 2023.01.09