Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Path.java
1461 views
1
package org.newdawn.slick.geom;
2
3
import java.util.ArrayList;
4
5
/**
6
* A shape built from lines and curves. Hole support is present but
7
* restricted.
8
*
9
* @author kevin
10
*/
11
public class Path extends Shape {
12
/** The local list of points */
13
private ArrayList localPoints = new ArrayList();
14
/** The current x coordinate */
15
private float cx;
16
/** The current y coordiante */
17
private float cy;
18
/** True if the path has been closed */
19
private boolean closed;
20
/** The list of holes placed */
21
private ArrayList holes = new ArrayList();
22
/** The current hole being built */
23
private ArrayList hole;
24
25
/**
26
* Create a new path
27
*
28
* @param sx The start x coordinate of the path
29
* @param sy The start y coordiante of the path
30
*/
31
public Path(float sx, float sy) {
32
localPoints.add(new float[] {sx,sy});
33
cx = sx;
34
cy = sy;
35
pointsDirty = true;
36
}
37
38
/**
39
* Start building a hole in the previously defined contour
40
*
41
* @param sx The start point of the hole
42
* @param sy The start point of the hole
43
*/
44
public void startHole(float sx, float sy) {
45
hole = new ArrayList();
46
holes.add(hole);
47
}
48
49
/**
50
* Add a line to the contour or hole which ends at the specified
51
* location.
52
*
53
* @param x The x coordinate to draw the line to
54
* @param y The y coordiante to draw the line to
55
*/
56
public void lineTo(float x, float y) {
57
if (hole != null) {
58
hole.add(new float[] {x,y});
59
} else {
60
localPoints.add(new float[] {x,y});
61
}
62
cx = x;
63
cy = y;
64
pointsDirty = true;
65
}
66
67
/**
68
* Close the path to form a polygon
69
*/
70
public void close() {
71
closed = true;
72
}
73
74
/**
75
* Add a curve to the specified location (using the default segments 10)
76
*
77
* @param x The destination x coordinate
78
* @param y The destination y coordiante
79
* @param cx1 The x coordiante of the first control point
80
* @param cy1 The y coordiante of the first control point
81
* @param cx2 The x coordinate of the second control point
82
* @param cy2 The y coordinate of the second control point
83
*/
84
public void curveTo(float x, float y, float cx1, float cy1, float cx2, float cy2) {
85
curveTo(x,y,cx1,cy1,cx2,cy2,10);
86
}
87
88
/**
89
* Add a curve to the specified location (specifing the number of segments)
90
*
91
* @param x The destination x coordinate
92
* @param y The destination y coordiante
93
* @param cx1 The x coordiante of the first control point
94
* @param cy1 The y coordiante of the first control point
95
* @param cx2 The x coordinate of the second control point
96
* @param cy2 The y coordinate of the second control point
97
* @param segments The number of segments to use for the new curve
98
*/
99
public void curveTo(float x, float y, float cx1, float cy1, float cx2, float cy2, int segments) {
100
// special case for zero movement
101
if ((cx == x) && (cy == y)) {
102
return;
103
}
104
105
Curve curve = new Curve(new Vector2f(cx,cy),new Vector2f(cx1,cy1),new Vector2f(cx2,cy2),new Vector2f(x,y));
106
float step = 1.0f / segments;
107
108
for (int i=1;i<segments+1;i++) {
109
float t = i * step;
110
Vector2f p = curve.pointAt(t);
111
if (hole != null) {
112
hole.add(new float[] {p.x,p.y});
113
} else {
114
localPoints.add(new float[] {p.x,p.y});
115
}
116
cx = p.x;
117
cy = p.y;
118
}
119
pointsDirty = true;
120
}
121
122
/**
123
* @see org.newdawn.slick.geom.Shape#createPoints()
124
*/
125
protected void createPoints() {
126
points = new float[localPoints.size() * 2];
127
for (int i=0;i<localPoints.size();i++) {
128
float[] p = (float[]) localPoints.get(i);
129
points[(i*2)] = p[0];
130
points[(i*2)+1] = p[1];
131
}
132
}
133
134
/**
135
* @see org.newdawn.slick.geom.Shape#transform(org.newdawn.slick.geom.Transform)
136
*/
137
public Shape transform(Transform transform) {
138
Path p = new Path(cx,cy);
139
p.localPoints = transform(localPoints, transform);
140
for (int i=0;i<holes.size();i++) {
141
p.holes.add(transform((ArrayList) holes.get(i), transform));
142
}
143
p.closed = this.closed;
144
145
return p;
146
}
147
148
/**
149
* Transform a list of points
150
*
151
* @param pts The pts to transform
152
* @param t The transform to apply
153
* @return The transformed points
154
*/
155
private ArrayList transform(ArrayList pts, Transform t) {
156
float[] in = new float[pts.size()*2];
157
float[] out = new float[pts.size()*2];
158
159
for (int i=0;i<pts.size();i++) {
160
in[i*2] = ((float[]) pts.get(i))[0];
161
in[(i*2)+1] = ((float[]) pts.get(i))[1];
162
}
163
t.transform(in, 0, out, 0, pts.size());
164
165
ArrayList outList = new ArrayList();
166
for (int i=0;i<pts.size();i++) {
167
outList.add(new float[] {out[(i*2)],out[(i*2)+1]});
168
}
169
170
return outList;
171
}
172
173
// /**
174
// * Calculate the triangles that can fill this shape
175
// */
176
// protected void calculateTriangles() {
177
// if (!trianglesDirty) {
178
// return;
179
// }
180
// if (points.length >= 6) {
181
// boolean clockwise = true;
182
// float area = 0;
183
// for (int i=0;i<(points.length/2)-1;i++) {
184
// float x1 = points[(i*2)];
185
// float y1 = points[(i*2)+1];
186
// float x2 = points[(i*2)+2];
187
// float y2 = points[(i*2)+3];
188
//
189
// area += (x1 * y2) - (y1 * x2);
190
// }
191
// area /= 2;
192
// clockwise = area > 0;
193
//
194
// if (clockwise) {
195
// tris = new MannTriangulator();
196
// for (int i=0;i<points.length;i+=2) {
197
// tris.addPolyPoint(points[i], points[i+1]);
198
// }
199
//
200
// for (int h=0;h<holes.size();h++) {
201
// ArrayList hole = (ArrayList) holes.get(h);
202
// tris.startHole();
203
// for (int i=0;i<hole.size();i++) {
204
// float[] pt = (float[]) hole.get(i);
205
// tris.addPolyPoint(pt[0],pt[1]);
206
// }
207
// }
208
// tris.triangulate();
209
// } else {
210
// tris = new MannTriangulator();
211
// for (int i=points.length-2;i>=0;i-=2) {
212
// tris.addPolyPoint(points[i], points[i+1]);
213
// }
214
//
215
// for (int h=0;h<holes.size();h++) {
216
// ArrayList hole = (ArrayList) holes.get(h);
217
// tris.startHole();
218
// for (int i=hole.size()-1;i>=0;i--) {
219
// float[] pt = (float[]) hole.get(i);
220
// tris.addPolyPoint(pt[0],pt[1]);
221
// }
222
// }
223
// tris.triangulate();
224
// }
225
//
226
// } else {
227
// tris.triangulate();
228
// }
229
//
230
// trianglesDirty = false;
231
// }
232
233
/**
234
* True if this is a closed shape
235
*
236
* @return True if this is a closed shape
237
*/
238
public boolean closed() {
239
return closed;
240
}
241
}
242
243