Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Polygon.java
1461 views
1
package org.newdawn.slick.geom;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
6
/**
7
* A polygon implementation meeting the <code>Shape</code> contract.
8
*
9
* @author Mark
10
*/
11
public class Polygon extends Shape {
12
/** Allow duplicated points */
13
private boolean allowDups = false;
14
/** True if the polygon is closed */
15
private boolean closed = true;
16
17
/**
18
* Construct a new polygon with 3 or more points.
19
* This constructor will take the first set of points and copy them after
20
* the last set of points to create a closed shape.
21
*
22
* @param points An array of points in x, y order.
23
*/
24
public Polygon(float points[]) {
25
int length = points.length;
26
27
this.points = new float[length];
28
maxX = -Float.MIN_VALUE;
29
maxY = -Float.MIN_VALUE;
30
minX = Float.MAX_VALUE;
31
minY = Float.MAX_VALUE;
32
x = Float.MAX_VALUE;
33
y = Float.MAX_VALUE;
34
35
for(int i=0;i<length;i++) {
36
this.points[i] = points[i];
37
if(i % 2 == 0) {
38
if(points[i] > maxX) {
39
maxX = points[i];
40
}
41
if(points[i] < minX) {
42
minX = points[i];
43
}
44
if(points[i] < x) {
45
x = points[i];
46
}
47
}
48
else {
49
if(points[i] > maxY) {
50
maxY = points[i];
51
}
52
if(points[i] < minY) {
53
minY = points[i];
54
}
55
if(points[i] < y) {
56
y = points[i];
57
}
58
}
59
}
60
61
findCenter();
62
calculateRadius();
63
pointsDirty = true;
64
}
65
/**
66
* Create an empty polygon
67
*
68
*/
69
public Polygon(){
70
points = new float[0];
71
maxX = -Float.MIN_VALUE;
72
maxY = -Float.MIN_VALUE;
73
minX = Float.MAX_VALUE;
74
minY = Float.MAX_VALUE;
75
}
76
77
/**
78
* Indicate if duplicate points are allow
79
*
80
* @param allowDups True if duplicate points are allowed
81
*/
82
public void setAllowDuplicatePoints(boolean allowDups) {
83
this.allowDups = allowDups;
84
}
85
86
/**
87
* Add a point to the polygon
88
*
89
* @param x The x coordinate of the point
90
* @param y The y coordinate of the point
91
*/
92
public void addPoint(float x, float y) {
93
if (hasVertex(x,y) && (!allowDups)) {
94
return;
95
}
96
97
ArrayList tempPoints = new ArrayList();
98
for(int i=0;i<points.length;i++) {
99
tempPoints.add(new Float(points[i]));
100
}
101
tempPoints.add(new Float(x));
102
tempPoints.add(new Float(y));
103
int length = tempPoints.size();
104
points = new float[length];
105
for(int i=0;i<length;i++) {
106
points[i] = ((Float)tempPoints.get(i)).floatValue();
107
}
108
if(x > maxX) {
109
maxX = x;
110
}
111
if(y > maxY) {
112
maxY = y;
113
}
114
if(x < minX) {
115
minX = x;
116
}
117
if(y < minY) {
118
minY = y;
119
}
120
findCenter();
121
calculateRadius();
122
123
pointsDirty = true;
124
}
125
126
127
/**
128
* Apply a transformation and return a new shape. This will not alter the current shape but will
129
* return the transformed shape.
130
*
131
* @param transform The transform to be applied
132
* @return The transformed shape.
133
*/
134
public Shape transform(Transform transform) {
135
checkPoints();
136
137
Polygon resultPolygon = new Polygon();
138
139
float result[] = new float[points.length];
140
transform.transform(points, 0, result, 0, points.length / 2);
141
resultPolygon.points = result;
142
resultPolygon.findCenter();
143
resultPolygon.closed = closed;
144
145
return resultPolygon;
146
}
147
148
/**
149
* @see org.newdawn.slick.geom.Shape#setX(float)
150
*/
151
public void setX(float x) {
152
super.setX(x);
153
154
pointsDirty = false;
155
}
156
157
/**
158
* @see org.newdawn.slick.geom.Shape#setY(float)
159
*/
160
public void setY(float y) {
161
super.setY(y);
162
163
pointsDirty = false;
164
}
165
166
/**
167
* @see org.newdawn.slick.geom.Shape#createPoints()
168
*/
169
protected void createPoints() {
170
// This is empty since a polygon must have it's points all the time.
171
}
172
173
/**
174
* @see org.newdawn.slick.geom.Shape#closed()
175
*/
176
public boolean closed() {
177
return closed;
178
}
179
180
/**
181
* Indicate if the polygon should be closed
182
*
183
* @param closed True if the polygon should be closed
184
*/
185
public void setClosed(boolean closed) {
186
this.closed = closed;
187
}
188
189
/**
190
* Provide a copy of this polygon
191
*
192
* @return A copy of this polygon
193
*/
194
public Polygon copy() {
195
float[] copyPoints = new float[points.length];
196
System.arraycopy(points, 0, copyPoints, 0, copyPoints.length);
197
198
return new Polygon(copyPoints);
199
}
200
}
201
202