PS/Algorithms

[Java] CCW (Counter Clock Wise) - 세 점의 방향성 파악

CalicoCat22 2022. 2. 19. 14:56

https://travelbeeee.tistory.com/45

 

[알고리즘] 세 점의 방향성 파악하기 'CCW 알고리즘'

안녕하세요. 여행벌입니다. 오늘은 세 점이 주어졌을 때, 방향성을 파악할 수 있는 CCW 알고리즘에 대해 다뤄보겠습니다. 다음과 같이 세 점 A, B, C 가 있을 때, 세 점의 방향성을 시계방향 / 반시

travelbeeee.tistory.com

반시계 방향 : 1

시계 방향 : -1

일직선 : 0

    static int ccw(Node n1, Node n2, Node n3){
        long s = (n1.x * n2.y + n2.x * n3.y + n3.x * n1.y) - (n1.y * n2.x + n2.y * n3.x + n3.y * n1.x);
        if (s > 0) return 1;
        else if (s < 0) return -1;
        else return 0;
    }
    
    static class Node {
        long x, y;
        
        public Node(long x, long y){
            this.x = x;
            this.y = y;
        }
    }