[백준(Baekjoon)][자바(java)] 11758 : CCW / 기하

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

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int x1, y1, x2, y2, x3, y3;
		StringTokenizer st = new StringTokenizer(br.readLine());
		x1 = Integer.parseInt(st.nextToken()); y1 = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		x2 = Integer.parseInt(st.nextToken()); y2 = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		x3 = Integer.parseInt(st.nextToken()); y3 = Integer.parseInt(st.nextToken());
		br.close();
		int value = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
		System.out.println(value > 0 ? 1 : value < 0 ? -1 : 0);
	}
}
public class Main {

	private static int ccw(int x1, int y1, int x2, int y2, int x3, int y3) { // CounterClockWise
		int value = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
		return value < 0 ? -1 : value > 0 ? 1 : 0;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int xy[][] = new int[3][2], i, j;
		for (i = 0; i < 3; ++i) {
			st = new StringTokenizer(br.readLine());
			for (j = 0; j < 2; ++j)
				xy[i][j] = Integer.parseInt(st.nextToken());
		}
		br.close();
		System.out.println(ccw(xy[0][0], xy[0][1], xy[1][0], xy[1][1], xy[2][0], xy[2][1]));
	}
}

 

 

반응형