Repository navigation
Expand file tree
/
Copy pathPolygon.java
More file actions
78 lines (66 loc) · 2.3 KB
/
Copy pathPolygon.java
File metadata and controls
78 lines (66 loc) · 2.3 KB
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
package bwta;
import bwapi.Position;
import bwem.Area;
import java.util.ArrayList;
import java.util.List;
public class Polygon {
private final Area area;
private final List<Position> boundaryVertices;
private final double areaTotal;
private final double perimeterTotal;
private final Position center;
Polygon(final Area area) {
this.area = area;
this.boundaryVertices = area.getBoundaryVertices();
// calculate surface
// https://en.wikipedia.org/wiki/Shoelace_formula#Statement
double surface = 0;
for (int i=0; i < boundaryVertices.size(); i++) {
final Position p0 = boundaryVertices.get(i);
final Position p1 = boundaryVertices.get((i+1) % boundaryVertices.size());
surface += p0.x * p1.y - p1.x * p0.y;
}
areaTotal = Math.abs(surface) / 2;
// calculate perimeter
double perimeter = 0;
for (int i=0; i < boundaryVertices.size(); i++) {
perimeter += boundaryVertices.get(i).getDistance(boundaryVertices.get((i + 1) % boundaryVertices.size()));
}
perimeterTotal = perimeter;
// calculate center of gravity
// https://stackoverflow.com/questions/5271583/center-of-gravity-of-a-polygon
if (areaTotal == 0) {
center = null;
}
else {
int Cx = 0;
int Cy = 0;
for (int i=0; i < boundaryVertices.size(); i++) {
final Position p0 = boundaryVertices.get(i);
final Position p1 = boundaryVertices.get((i+1) % boundaryVertices.size());
final int div = p0.x * p1.y - p1.x * p0.y;
Cx += (p0.x + p1.x) * div;
Cy += (p0.y + p1.y) * div;
}
Cx /= 6 * areaTotal;
Cy /= 6 * areaTotal;
center = new Position(Cx, Cy);
}
}
public double getArea() {
return areaTotal;
}
public double getPerimeter() {
return perimeterTotal;
}
public Position getCenter() {
return center;
}
public boolean isInside(final Position position) {
// approximately
return BWTA.getRegion(position).area.equals(area);
}
public List<Position> getPoints() {
return new ArrayList<>(boundaryVertices);
}
}