-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolygonArea.java
79 lines (57 loc) · 2.14 KB
/
PolygonArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
public class PolygonArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
arraySolution(scanner);
scanner.close();
}
public static void variableSolution(Scanner scanner) {
int numPoints = scanner.nextInt();
while (numPoints != 0) {
double doubleArea = 0;
int xFirstPoint = scanner.nextInt();
int yFirstPoint = scanner.nextInt();
int xPrev = xFirstPoint;
int yPrev = yFirstPoint;
int xCurr = 0;
int yCurr = 0;
for (int i = 0; i < numPoints - 1; i++) {
xCurr = scanner.nextInt();
yCurr = scanner.nextInt();
doubleArea += xPrev * yCurr - yPrev * xCurr;
xPrev = xCurr;
yPrev = yCurr;
}
doubleArea += xCurr * yFirstPoint - yCurr * xFirstPoint;
if (doubleArea >= 0) {
System.out.println(String.format("CCW %.1f", doubleArea * 0.5));
}
else {
System.out.println(String.format("CW %.1f", -doubleArea * 0.5));
}
numPoints = scanner.nextInt();
}
}
public static void arraySolution(Scanner scanner) {
int numPoints = scanner.nextInt();
while (numPoints != 0) {
int[] x = new int[numPoints];
int[] y = new int[numPoints];
for (int i = 0; i < numPoints; i++) {
x[i] = scanner.nextInt();
y[i] = scanner.nextInt();
}
double area = calculateArea(x, y);
System.out.println((area >= 0) ? String.format("CCW %.1f", area) : String.format("CW %.1f", -area));
numPoints = scanner.nextInt();
}
}
private static double calculateArea(int[] x, int[] y) {
int numPoints = x.length;
double doubleArea = x[numPoints-1] * y[0] - y[numPoints-1] * x[0];
for (int i = 1; i < numPoints; i++) {
doubleArea += x[i-1] * y[i] - y[i-1] * x[i];
}
return doubleArea / 2;
}
}