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 package math.geom2d.point;
28
29 import java.awt.Graphics2D;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Iterator;
34
35 import math.geom2d.AffineTransform2D;
36 import math.geom2d.Box2D;
37 import math.geom2d.Point2D;
38 import math.geom2d.Shape2D;
39 import math.geom2d.circulinear.CirculinearCurve2DUtils;
40 import math.geom2d.circulinear.CirculinearDomain2D;
41 import math.geom2d.circulinear.CirculinearShape2D;
42 import math.geom2d.transform.CircleInversion2D;
43
44
45
46
47
48
49 public class PointArray2D
50 implements PointSet2D, CirculinearShape2D, Cloneable {
51
52
53
54
55
56
57
58 protected ArrayList<Point2D> points = null;
59
60
61
62
63
64
65
66 public PointArray2D() {
67 this(0);
68 }
69
70
71
72
73
74
75
76 public PointArray2D(int n) {
77 points = new ArrayList<Point2D>();
78 }
79
80
81
82
83
84 public PointArray2D(java.awt.geom.Point2D[] points) {
85 this(points.length);
86 for (java.awt.geom.Point2D element : points)
87 if (Point2D.class.isInstance(element))
88 this.points.add((Point2D) element);
89 else
90 this.points.add(new Point2D(element));
91 }
92
93
94
95
96
97
98
99
100 public PointArray2D(Collection<? extends java.awt.geom.Point2D> points) {
101 this(points.size());
102
103 for (java.awt.geom.Point2D point : points) {
104 if (point instanceof Point2D)
105 this.points.add((Point2D) point);
106 else
107 this.points.add(new Point2D(point));
108 }
109 }
110
111
112
113
114 public final static PointSet2D create(Collection<? extends java.awt.geom.Point2D> points) {
115 return new PointArray2D(points);
116 }
117
118 public final static PointSet2D create(Point2D[] points) {
119 return new PointArray2D(points);
120 }
121
122 public final static PointSet2D create(int size) {
123 return new PointArray2D(size);
124 }
125
126
127
128
129
130
131
132
133
134
135 public void addPoint(java.awt.geom.Point2D point) {
136 if (point instanceof Point2D)
137 this.points.add((Point2D) point);
138 else
139 this.points.add(new Point2D(point));
140 }
141
142
143
144
145
146
147 public void addPoints(java.awt.geom.Point2D[] points) {
148 for (java.awt.geom.Point2D element : points)
149 this.addPoint(element);
150 }
151
152 public void addPoints(Collection<? extends Point2D> points) {
153 this.points.addAll(points);
154 }
155
156
157
158
159
160
161 public Collection<Point2D> getPoints() {
162 return Collections.unmodifiableList(points);
163 }
164
165
166
167
168 public void clearPoints() {
169 this.points.clear();
170 }
171
172
173
174
175
176
177 public int getPointNumber() {
178 return points.size();
179 }
180
181
182
183
184
185
186
187
188 public CirculinearDomain2D getBuffer(double dist) {
189 return CirculinearCurve2DUtils.computeBuffer(this, dist);
190 }
191
192 public PointArray2D transform(CircleInversion2D inv) {
193
194 PointArray2D array = new PointArray2D(points.size());
195
196 for(Point2D point : points)
197 array.addPoint(point.transform(inv));
198
199 return array;
200 }
201
202
203
204
205 public double getDistance(java.awt.geom.Point2D p) {
206 return getDistance(p.getX(), p.getY());
207 }
208
209
210
211
212
213
214 public double getDistance(double x, double y) {
215 if (points.isEmpty())
216 return Double.NaN;
217 double dist = Double.MAX_VALUE;
218 for (Point2D point : points)
219 dist = Math.min(dist, point.getDistance(x, y));
220 return dist;
221 }
222
223
224
225
226 public boolean isBounded() {
227 return true;
228 }
229
230 public boolean isEmpty() {
231 return points.size()==0;
232 }
233
234
235
236
237
238
239 public PointArray2D clip(Box2D box) {
240
241 PointArray2D res = new PointArray2D(points.size());
242
243
244 for (Point2D point : points) {
245 if(box.contains(point)) {
246 res.addPoint(point);
247 }
248 }
249
250
251 res.points.trimToSize();
252
253
254 return res;
255 }
256
257 public Box2D getBoundingBox() {
258
259 double xmin = Double.MAX_VALUE;
260 double ymin = Double.MAX_VALUE;
261 double xmax = Double.MIN_VALUE;
262 double ymax = Double.MIN_VALUE;
263
264
265 for (Point2D point : points) {
266 xmin = Math.min(xmin, point.getX());
267 ymin = Math.min(ymin, point.getY());
268 xmax = Math.max(xmax, point.getX());
269 ymax = Math.max(ymax, point.getY());
270 }
271
272
273 return new Box2D(xmin, xmax, ymin, ymax);
274 }
275
276
277
278
279
280
281 public PointArray2D transform(AffineTransform2D trans) {
282 PointArray2D res = new PointArray2D(points.size());
283
284 for (Point2D point : points)
285 res.addPoint(point.transform(trans));
286
287 return res;
288 }
289
290
291
292
293
294
295 public boolean contains(double x, double y) {
296 for (Point2D point : points)
297 if (point.getDistance(x, y)<Shape2D.ACCURACY)
298 return true;
299 return false;
300 }
301
302
303
304
305
306
307 public boolean contains(java.awt.geom.Point2D point) {
308 return contains(point.getX(), point.getY());
309 }
310
311
312
313
314
315
316
317 public void draw(Graphics2D g2) {
318 this.draw(g2, 1);
319 }
320
321
322
323
324
325
326
327 public void draw(Graphics2D g2, double r) {
328 for (Point2D point : points)
329 g2.fill(new java.awt.geom.Ellipse2D.Double(point.x-r, point.y-r,
330 2*r, 2*r));
331 }
332
333
334
335
336
337
338 public Iterator<Point2D> iterator() {
339 return points.iterator();
340 }
341
342 @Override
343 public boolean equals(Object obj) {
344 if(!(obj instanceof PointSet2D))
345 return false;
346
347 PointSet2D set = (PointSet2D) obj;
348 for(Point2D point : this){
349 if(!set.contains(point))
350 return false;
351 }
352
353 for(Point2D point : set){
354 if(!this.contains(point))
355 return false;
356 }
357
358 return true;
359 }
360
361 @Override
362 public PointArray2D clone() {
363 PointArray2D set = new PointArray2D(this.getPointNumber());
364 for(Point2D point : this)
365 set.addPoint(point.clone());
366 return set;
367 }
368 }