Step-by-Step

[Java] 백준11758 - CCW 본문

언어/JAVA

[Java] 백준11758 - CCW

희주(KHJ) 2022. 11. 12. 21:46

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는 수학 기벡 시간에 배운 벡터 외적과 물리시간에 배운 오른손의 법칙이랑 관련이 있다

고등학교때 배웠던 공식이지만 벌써 N년이 지난 이야기........

 

CCW 알고리즘 (CounterClockWise)

A값과 B 값을 뺀 C 값의 부호에 따라 일직선, 시계, 반시계 방향을 알 수 있다.

코드에 적용하기만 하면 끝!

 

 

[코드]

package BOJ;

import java.util.ArrayList;
import java.util.Scanner;

public class boj11758 {
	static class Point{
		int x, y;
		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList<Point> points = new ArrayList<>();
		for(int i=0; i<3; i++) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			points.add(new Point(x,y));
		}
		
		Point p1 = points.get(0);
		Point p2 = points.get(1);
		Point p3 = points.get(2);
		
		int n1 = p1.x * p2.y + p2.x * p3.y + p3.x * p1.y;
		int n2 = p2.x * p1.y + p3.x * p2.y + p1.x * p3.y;
		
		int sum = n1- n2;
		
		// 반시계
		if(sum > 0) {
			System.out.println(1);
		}else if(sum < 0) {
			System.out.println(-1);
		}else {
			System.out.println(0);
		}
		
	}
	
}

 

 

 

[참조]

https://cantcoding.tistory.com/12

Comments