-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometry.pde
66 lines (56 loc) · 1.46 KB
/
Geometry.pde
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
class Geometry extends Entity {
PVector[] vertices;
color colorFill;
PShape shape;
Geometry(PVector[] vertices, color colorFill, boolean solid) {
this.zIndex = 100;
this.solid = solid;
this.vertices = vertices;
this.colorFill = colorFill;
this.shape = createShape();
this.shape.setFill(this.colorFill);
this.shape.setStroke(false);
this.shape.beginShape();
for (PVector point : vertices) {
this.shape.vertex(point.x, point.y);
}
this.shape.endShape(CLOSE);
this.updateBoundingBox();
}
PVector getVertex(int index) {
return this.vertices[index];
}
PVector[] getVertices() {
return this.vertices;
}
PVector[] getLine(int index) {
PVector start = this.vertices[index];
PVector end = this.vertices[index < this.vertices.length - 1 ? index + 1 : 0];
return new PVector[] { start, end };
}
PVector[][] getLines() {
PVector[][] result = new PVector[this.vertices.length][2];
for (int i = 0; i < this.vertices.length; i++) {
result[i] = this.getLine(i);
}
return result;
}
void updateBoundingBox() {
PVector mins = null, maxs = null;
for (PVector vertex : this.vertices) {
if (mins == null || maxs == null) {
mins = vertex.copy();
maxs = vertex.copy();
continue;
}
mins.x = min(vertex.x, mins.x);
mins.y = min(vertex.y, mins.y);
maxs.x = max(vertex.x, maxs.x);
maxs.y = max(vertex.y, maxs.y);
}
this.boundingBox = new PVector[]{ mins, maxs };
}
void OnDraw() {
shape(this.shape);
}
}