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
|
/* Copyright (C) 2001 Free Software Foundation
This file is part of libjava.
This software is copyrighted work licensed under the terms of the
Libjava License. Please consult the file "LIBJAVA_LICENSE" for
details. */
package java.awt;
import java.awt.geom.*;
import java.io.Serializable;
import java.util.Arrays;
/**
* @author Tom Tromey <tromey@redhat.com>
* @date May 10, 2001
*/
/** The Polygon class represents a closed region whose boundary is
made of line segments. The Polygon is defined by its vertices. */
public class Polygon implements Shape, Serializable
{
/** The bounds of the polygon. This is null until the bounds have
* been computed for the first time; then it is correctly
* maintained whenever it is modified. */
protected Rectangle bounds;
/** The number of points in the polygon. */
public int npoints;
/** The x coordinates of the points. */
public int[] xpoints;
/** The y coordinates of the points. */
public int[] ypoints;
/** Create a new, empty Polygon. */
public Polygon ()
{
this.xpoints = new int[0];
this.ypoints = new int[0];
this.npoints = 0;
}
/** Create a new Polygon from the given vertices.
* @param xpoints The x coordinates
* @param ypoints The y coordinates
* @param npoints The number of points
*/
public Polygon (int[] xpoints, int[] ypoints, int npoints)
{
// We make explicit copies instead of relying on clone so that we
// ensure the new arrays are the same size.
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);
}
/** Append the specified point to this Polygon.
* @param x The x coordinate
* @param y The y coordinate
*/
public void addPoint (int x, int y)
{
int[] newx = new int[npoints + 1];
System.arraycopy (xpoints, 0, newx, 0, npoints);
int[] newy = new int[npoints + 1];
System.arraycopy (ypoints, 0, newy, 0, npoints);
newx[npoints] = x;
newy[npoints] = y;
++npoints;
xpoints = newx;
ypoints = newy;
// It is simpler to just recompute.
if (bounds != null)
computeBoundingBox ();
}
/** Return true if the indicated point is inside this Polygon.
* This uses an even-odd rule to determine insideness.
* @param x The x coordinate
* @param y The y coordinate
* @returns true if the point is contained by this Polygon.
*/
public boolean contains (double x, double y)
{
// What we do is look at each line segment. If the line segment
// crosses the "scan line" at y at a point x' < x, then we
// increment our counter. At the end, an even number means the
// point is outside the polygon. Instead of a number, though, we
// use a boolean.
boolean inside = false;
for (int i = 0; i < npoints; ++i)
{
// Handle the wrap case.
int x2 = (i == npoints) ? xpoints[0] : xpoints[i + 1];
int y2 = (i == npoints) ? ypoints[0] : ypoints[i + 1];
if (ypoints[i] == y2)
{
// We ignore horizontal lines. This might give weird
// results in some situations -- ?
continue;
}
double t = (y - ypoints[i]) / (double) (y2 - ypoints[i]);
double x3 = xpoints[i] + t * (x2 - xpoints[i]);
if (x3 < x)
inside = ! inside;
}
return inside;
}
/** Return true if the indicated rectangle is entirely inside this
* Polygon.
* This uses an even-odd rule to determine insideness.
* @param x The x coordinate
* @param y The y coordinate
* @param w The width
* @param h The height
* @returns true if the rectangle is contained by this Polygon.
*/
public boolean contains (double x, double y, double w, double h)
{
return intersectOrContains (x, y, w, h, false);
}
/** Return true if the indicated point is inside this Polygon.
* This uses an even-odd rule to determine insideness.
* @param x The x coordinate
* @param y The y coordinate
* @returns true if the point is contained by this Polygon.
*/
public boolean contains (int x, int y)
{
return contains ((double) x, (double) y);
}
/** Return true if the indicated point is inside this Polygon.
* This uses an even-odd rule to determine insideness.
* @param p The point
* @returns true if the point is contained by this Polygon.
*/
public boolean contains (Point p)
{
return contains (p.x, p.y);
}
/** Return true if the indicated point is inside this Polygon.
* This uses an even-odd rule to determine insideness.
* @param p The point
* @returns true if the point is contained by this Polygon.
*/
public boolean contains (Point2D p)
{
return contains (p.getX (), p.getY ());
}
/** Return true if the indicated rectangle is entirely inside this
* Polygon. This uses an even-odd rule to determine insideness.
* @param r The rectangle
* @returns true if the rectangle is contained by this Polygon.
*/
public boolean contains (Rectangle2D r)
{
return contains (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
}
/** Returns the bounds of this Polygon.
* @deprecated Use getBounds() instead.
*/
public Rectangle getBoundingBox ()
{
if (bounds == null)
computeBoundingBox ();
return bounds;
}
/** Returns the bounds of this Polygon. */
public Rectangle getBounds ()
{
if (bounds == null)
computeBoundingBox ();
return bounds;
}
/** Returns the bounds of this Polygon. */
public Rectangle2D getBounds2D ()
{
if (bounds == null)
computeBoundingBox ();
return bounds; // Why not?
}
/** Return an iterator for the boundary of this Polygon.
* @param at A transform to apply to the coordinates.
* @returns A path iterator for the Polygon's boundary.
*/
public PathIterator getPathIterator (AffineTransform at)
{
return new Iterator (at);
}
/** Return an iterator for the boundary of this Polygon.
* @param at A transform to apply to the coordinates.
* @param flatness The flatness of the result; it is ignored by
* this class.
* @returns A path iterator for the Polygon's boundary.
*/
public PathIterator getPathIterator (AffineTransform at, double flatness)
{
// We ignore the flatness.
return new Iterator (at);
}
/** @deprecated use contains(int,int). */
public boolean inside (int x, int y)
{
return contains (x, y);
}
/** Return true if this Polygon's interior intersects the given
* rectangle's interior.
* @param x The x coordinate
* @param y The y coordinate
* @param w The width
* @param h The height
*/
public boolean intersects (double x, double y, double w, double h)
{
return intersectOrContains (x, y, w, h, true);
}
/** Return true if this Polygon's interior intersects the given
* rectangle's interior.
* @param r The rectangle
*/
public boolean intersects (Rectangle2D r)
{
return intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
}
// This tests for intersection with or containment of a rectangle,
// depending on the INTERSECT argument.
private boolean intersectOrContains (double x, double y, double w, double h,
boolean intersect)
{
// First compute the rectangle of possible intersection points.
Rectangle r = getBounds ();
int minx = Math.max (r.x, (int) x);
int maxx = Math.min (r.x + r.width, (int) (x + w));
int miny = Math.max (r.y, (int) y);
int maxy = Math.min (r.y + r.height, (int) (y + h));
if (miny > maxy)
return false;
double[] crosses = new double[npoints + 1];
for (; miny < maxy; ++miny)
{
// First compute every place where the polygon might intersect
// the scan line at Y.
int ins = 0;
for (int i = 0; i < npoints; ++i)
{
// Handle the wrap case.
int x2 = (i == npoints) ? xpoints[0] : xpoints[i + 1];
int y2 = (i == npoints) ? ypoints[0] : ypoints[i + 1];
if (ypoints[i] == y2)
{
// We ignore horizontal lines. This might give weird
// results in some situations -- ?
continue;
}
double t = (((double) miny - ypoints[i])
/ (double) (y2 - ypoints[i]));
double x3 = xpoints[i] + t * (x2 - xpoints[i]);
crosses[ins++] = x3;
}
// Now we can sort into increasing order and look to see if
// any point in the rectangle is in the polygon. We examine
// every other pair due to our even-odd rule.
Arrays.sort (crosses, 0, ins);
int i = intersect ? 0 : 1;
for (; i < ins - 1; i += 2)
{
// Pathological case.
if (crosses[i] == crosses[i + 1])
continue;
// Found a point on the inside.
if ((crosses[i] >= x && crosses[i] < x + w)
|| (crosses[i + 1] >= x && crosses[i + 1] < x + w))
{
// If we're checking containment then we just lost.
// But if we're checking intersection then we just
// won.
return intersect;
}
}
}
return false;
}
/** Translates all the vertices of the polygon via a given vector.
* @param deltaX The X offset
* @param deltaY The Y offset
*/
public void translate (int deltaX, int deltaY)
{
for (int i = 0; i < npoints; ++i)
{
xpoints[i] += deltaX;
ypoints[i] += deltaY;
}
if (bounds != null)
{
bounds.x += deltaX;
bounds.y += deltaY;
}
}
// This computes the bounding box if required.
private void computeBoundingBox ()
{
if (npoints == 0)
{
// This is wrong if the user adds a new point, but we
// account for that in addPoint().
bounds = new Rectangle (0, 0, 0, 0);
}
else
{
int maxx = xpoints[0];
int minx = xpoints[0];
int maxy = ypoints[0];
int miny = ypoints[0];
for (int i = 1; i < npoints; ++i)
{
maxx = Math.max (maxx, xpoints[i]);
minx = Math.min (minx, xpoints[i]);
maxy = Math.max (maxy, ypoints[i]);
miny = Math.min (miny, ypoints[i]);
}
bounds = new Rectangle (minx, miny, maxx - minx, maxy - miny);
}
}
private class Iterator implements PathIterator
{
public AffineTransform xform;
public int where;
public Iterator (AffineTransform xform)
{
this.xform = xform;
where = 0;
}
public int currentSegment (double[] coords)
{
int r;
if (where < npoints)
{
coords[0] = xpoints[where];
coords[1] = ypoints[where];
r = (where == 0) ? SEG_MOVETO : SEG_LINETO;
xform.transform (coords, 0, coords, 0, 1);
++where;
}
else
r = SEG_CLOSE;
return r;
}
public int currentSegment (float[] coords)
{
int r;
if (where < npoints)
{
coords[0] = xpoints[where];
coords[1] = ypoints[where];
r = (where == 0) ? SEG_MOVETO : SEG_LINETO;
xform.transform (coords, 0, coords, 0, 1);
}
else
r = SEG_CLOSE;
return r;
}
public int getWindingRule ()
{
return WIND_EVEN_ODD;
}
public boolean isDone ()
{
return where == npoints + 1;
}
public void next ()
{
++where;
}
}
}
|