-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygon.java
More file actions
515 lines (467 loc) · 16.4 KB
/
Polygon.java
File metadata and controls
515 lines (467 loc) · 16.4 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Denis M. Kishenko
* @version $Revision$
*/
package java.awt;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.NoSuchElementException;
import org.apache.harmony.awt.gl.*;
import org.apache.harmony.awt.internal.nls.Messages;
/**
* The Polygon class defines an closed area specified by n vertices and n edges.
* The coordinates of the vertices are specified by x, y arrays. The edges are
* the line segments from the point (x[i], y[i]) to the point (x[i+1], y[i+1]),
* for -1 < i < (n-1) plus the line segment from the point (x[n-1], y[n-1]) to
* the point (x[0], y[0]) point. The Polygon is empty if the number of vertices
* is zero.
*
* @since Android 1.0
*/
public class Polygon implements Shape, Serializable {
/**
* The Constant serialVersionUID.
*/
private static final long serialVersionUID = -6460061437900069969L;
/**
* The points buffer capacity.
*/
private static final int BUFFER_CAPACITY = 4;
/**
* The number of Polygon vertices.
*/
public int npoints;
/**
* The array of X coordinates of the vertices.
*/
public int[] xpoints;
/**
* The array of Y coordinates of the vertices.
*/
public int[] ypoints;
/**
* The smallest Rectangle that completely contains this Polygon.
*/
protected Rectangle bounds;
/*
* Polygon path iterator
*/
/**
* The internal Class Iterator.
*/
class Iterator implements PathIterator {
/**
* The source Polygon object.
*/
public Polygon p;
/**
* The path iterator transformation.
*/
public AffineTransform t;
/**
* The current segment index.
*/
public int index;
/**
* Constructs a new Polygon.Iterator for the given polygon and
* transformation
*
* @param at
* the AffineTransform object to apply rectangle path.
* @param p
* the p.
*/
public Iterator(AffineTransform at, Polygon p) {
this.p = p;
this.t = at;
if (p.npoints == 0) {
index = 1;
}
}
public int getWindingRule() {
return WIND_EVEN_ODD;
}
public boolean isDone() {
return index > p.npoints;
}
public void next() {
index++;
}
public int currentSegment(double[] coords) {
if (isDone()) {
// awt.110=Iterator out of bounds
throw new NoSuchElementException(Messages.getString("awt.110")); //$NON-NLS-1$
}
if (index == p.npoints) {
return SEG_CLOSE;
}
coords[0] = p.xpoints[index];
coords[1] = p.ypoints[index];
if (t != null) {
t.transform(coords, 0, coords, 0, 1);
}
return index == 0 ? SEG_MOVETO : SEG_LINETO;
}
public int currentSegment(float[] coords) {
if (isDone()) {
// awt.110=Iterator out of bounds
throw new NoSuchElementException(Messages.getString("awt.110")); //$NON-NLS-1$
}
if (index == p.npoints) {
return SEG_CLOSE;
}
coords[0] = p.xpoints[index];
coords[1] = p.ypoints[index];
if (t != null) {
t.transform(coords, 0, coords, 0, 1);
}
return index == 0 ? SEG_MOVETO : SEG_LINETO;
}
}
/**
* Instantiates a new empty polygon.
*/
public Polygon() {
xpoints = new int[BUFFER_CAPACITY];
ypoints = new int[BUFFER_CAPACITY];
}
/**
* Instantiates a new polygon with the specified number of vertices, and the
* given arrays of x, y vertex coordinates. The length of each coordinate
* array may not be less than the specified number of vertices but may be
* greater. Only the first n elements are used from each coordinate array.
*
* @param xpoints
* the array of X vertex coordinates.
* @param ypoints
* the array of Y vertex coordinates.
* @param npoints
* the number vertices of the polygon.
* @throws IndexOutOfBoundsException
* if the length of xpoints or ypoints is less than n.
* @throws NegativeArraySizeException
* if n is negative.
*/
public Polygon(int[] xpoints, int[] ypoints, int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
// awt.111=Parameter npoints is greater than array length
throw new IndexOutOfBoundsException(Messages.getString("awt.111")); //$NON-NLS-1$
}
if (npoints < 0) {
// awt.112=Negative number of points
throw new NegativeArraySizeException(Messages.getString("awt.112")); //$NON-NLS-1$
}
this.npoints = npoints;
this.xpoints = new int[npoints];
this.ypoints = new int[npoints];
System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
}
/**
* Resets the current Polygon to an empty Polygon. More precisely, the
* number of Polygon vertices is set to zero, but x, y coordinates arrays
* are not affected.
*/
public void reset() {
npoints = 0;
bounds = null;
}
/**
* Invalidates the data that depends on the vertex coordinates. This method
* should be called after direct manipulations of the x, y vertex
* coordinates arrays to avoid unpredictable results of methods which rely
* on the bounding box.
*/
public void invalidate() {
bounds = null;
}
/**
* Adds the point to the Polygon and updates the bounding box accordingly.
*
* @param px
* the X coordinate of the added vertex.
* @param py
* the Y coordinate of the added vertex.
*/
public void addPoint(int px, int py) {
if (npoints == xpoints.length) {
int[] tmp;
tmp = new int[xpoints.length + BUFFER_CAPACITY];
System.arraycopy(xpoints, 0, tmp, 0, xpoints.length);
xpoints = tmp;
tmp = new int[ypoints.length + BUFFER_CAPACITY];
System.arraycopy(ypoints, 0, tmp, 0, ypoints.length);
ypoints = tmp;
}
xpoints[npoints] = px;
ypoints[npoints] = py;
npoints++;
if (bounds != null) {
bounds.setFrameFromDiagonal(Math.min(bounds.getMinX(), px), Math.min(bounds.getMinY(),
py), Math.max(bounds.getMaxX(), px), Math.max(bounds.getMaxY(), py));
}
}
/**
* Gets the bounding rectangle of the Polygon. The bounding rectangle is the
* smallest rectangle which contains the Polygon.
*
* @return the bounding rectangle of the Polygon.
* @see java.awt.Shape#getBounds()
*/
public Rectangle getBounds() {
if (bounds != null) {
return bounds;
}
if (npoints == 0) {
return new Rectangle();
}
int bx1 = xpoints[0];
int by1 = ypoints[0];
int bx2 = bx1;
int by2 = by1;
for (int i = 1; i < npoints; i++) {
int x = xpoints[i];
int y = ypoints[i];
if (x < bx1) {
bx1 = x;
} else if (x > bx2) {
bx2 = x;
}
if (y < by1) {
by1 = y;
} else if (y > by2) {
by2 = y;
}
}
return bounds = new Rectangle(bx1, by1, bx2 - bx1, by2 - by1);
}
/**
* Gets the bounding rectangle of the Polygon. The bounding rectangle is the
* smallest rectangle which contains the Polygon.
*
* @return the bounding rectangle of the Polygon.
* @deprecated Use getBounds() method.
*/
@Deprecated
public Rectangle getBoundingBox() {
return getBounds();
}
/**
* Gets the Rectangle2D which represents Polygon bounds. The bounding
* rectangle is the smallest rectangle which contains the Polygon.
*
* @return the bounding rectangle of the Polygon.
* @see java.awt.Shape#getBounds2D()
*/
public Rectangle2D getBounds2D() {
return getBounds().getBounds2D();
}
/**
* Translates all vertices of Polygon the specified distances along X, Y
* axis.
*
* @param mx
* the distance to translate horizontally.
* @param my
* the distance to translate vertically.
*/
public void translate(int mx, int my) {
for (int i = 0; i < npoints; i++) {
xpoints[i] += mx;
ypoints[i] += my;
}
if (bounds != null) {
bounds.translate(mx, my);
}
}
/**
* Checks whether or not the point given by the coordinates x, y lies inside
* the Polygon.
*
* @param x
* the X coordinate of the point to check.
* @param y
* the Y coordinate of the point to check.
* @return true, if the specified point lies inside the Polygon, false
* otherwise.
* @deprecated Use contains(int, int) method.
*/
@Deprecated
public boolean inside(int x, int y) {
return contains((double)x, (double)y);
}
/**
* Checks whether or not the point given by the coordinates x, y lies inside
* the Polygon.
*
* @param x
* the X coordinate of the point to check.
* @param y
* the Y coordinate of the point to check.
* @return true, if the specified point lies inside the Polygon, false
* otherwise.
*/
public boolean contains(int x, int y) {
return contains((double)x, (double)y);
}
/**
* Checks whether or not the point with specified double coordinates lies
* inside the Polygon.
*
* @param x
* the X coordinate of the point to check.
* @param y
* the Y coordinate of the point to check.
* @return true, if the point given by the double coordinates lies inside
* the Polygon, false otherwise.
* @see java.awt.Shape#contains(double, double)
*/
public boolean contains(double x, double y) {
return Crossing.isInsideEvenOdd(Crossing.crossShape(this, x, y));
}
/**
* Checks whether or not the rectangle determined by the parameters [x, y,
* width, height] lies inside the Polygon.
*
* @param x
* the X coordinate of the rectangles's left upper corner as a
* double.
* @param y
* the Y coordinate of the rectangles's left upper corner as a
* double.
* @param width
* the width of rectangle as a double.
* @param height
* the height of rectangle as a double.
* @return true, if the specified rectangle lies inside the Polygon, false
* otherwise.
* @see java.awt.Shape#contains(double, double, double, double)
*/
public boolean contains(double x, double y, double width, double height) {
int cross = Crossing.intersectShape(this, x, y, width, height);
return cross != Crossing.CROSSING && Crossing.isInsideEvenOdd(cross);
}
/**
* Checks whether or not the rectangle determined by the parameters [x, y,
* width, height] intersects the interior of the Polygon.
*
* @param x
* the X coordinate of the rectangles's left upper corner as a
* double.
* @param y
* the Y coordinate of the rectangles's left upper corner as a
* double.
* @param width
* the width of rectangle as a double.
* @param height
* the height of rectangle as a double.
* @return true, if the specified rectangle intersects the interior of the
* Polygon, false otherwise.
* @see java.awt.Shape#intersects(double, double, double, double)
*/
public boolean intersects(double x, double y, double width, double height) {
int cross = Crossing.intersectShape(this, x, y, width, height);
return cross == Crossing.CROSSING || Crossing.isInsideEvenOdd(cross);
}
/**
* Checks whether or not the specified rectangle lies inside the Polygon.
*
* @param rect
* the Rectangle2D object.
* @return true, if the specified rectangle lies inside the Polygon, false
* otherwise.
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
*/
public boolean contains(Rectangle2D rect) {
return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
/**
* Checks whether or not the specified Point lies inside the Polygon.
*
* @param point
* the Point object.
* @return true, if the specified Point lies inside the Polygon, false
* otherwise.
*/
public boolean contains(Point point) {
return contains(point.getX(), point.getY());
}
/**
* Checks whether or not the specified Point2D lies inside the Polygon.
*
* @param point
* the Point2D object.
* @return true, if the specified Point2D lies inside the Polygon, false
* otherwise.
* @see java.awt.Shape#contains(java.awt.geom.Point2D)
*/
public boolean contains(Point2D point) {
return contains(point.getX(), point.getY());
}
/**
* Checks whether or not the interior of rectangle specified by the
* Rectangle2D object intersects the interior of the Polygon.
*
* @param rect
* the Rectangle2D object.
* @return true, if the Rectangle2D intersects the interior of the Polygon,
* false otherwise.
* @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
*/
public boolean intersects(Rectangle2D rect) {
return intersects(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
/**
* Gets the PathIterator object which gives the coordinates of the polygon,
* transformed according to the specified AffineTransform.
*
* @param t
* the specified AffineTransform object or null.
* @return PathIterator object for the Polygon.
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
*/
public PathIterator getPathIterator(AffineTransform t) {
return new Iterator(t, this);
}
/**
* Gets the PathIterator object which gives the coordinates of the polygon,
* transformed according to the specified AffineTransform. The flatness
* parameter is ignored.
*
* @param t
* the specified AffineTransform object or null.
* @param flatness
* the maximum number of the control points for a given curve
* which varies from colinear before a subdivided curve is
* replaced by a straight line connecting the endpoints. This
* parameter is ignored for the Polygon class.
* @return PathIterator object for the Polygon.
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
* double)
*/
public PathIterator getPathIterator(AffineTransform t, double flatness) {
return new Iterator(t, this);
}
}