-
Notifications
You must be signed in to change notification settings - Fork 1
/
Polygon2D.java
862 lines (778 loc) · 30.8 KB
/
Polygon2D.java
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
/**
* This class is a Polygon with float coordinates.
*
* @version $Id: Polygon2D.java 594018 2007-11-12 04:17:41Z cam $
*/
public class Polygon2D implements Shape, Cloneable, Serializable {
/**
* The total number of points. The value of <code>npoints</code>
* represents the number of valid points in this <code>Polygon</code>.
*
*/
public int npoints;
/**
* The array of <i>x</i> coordinates. The value of {@link #npoints npoints} is equal to the
* number of points in this <code>Polygon2D</code>.
*
*/
public float[] xpoints;
/**
* The array of <i>x</i> coordinates. The value of {@link #npoints npoints} is equal to the
* number of points in this <code>Polygon2D</code>.
*
*/
public float[] ypoints;
/**
* Bounds of the Polygon2D.
* @see #getBounds()
*/
protected Rectangle2D bounds;
private GeneralPath path;
private GeneralPath closedPath;
/**
* Creates an empty Polygon2D.
*/
public Polygon2D() {
xpoints = new float[4];
ypoints = new float[4];
}
/**
* Constructs and initializes a <code>Polygon2D</code> from the specified
* Rectangle2D.
* @param rec the Rectangle2D
* @exception NullPointerException rec is <code>null</code>.
*/
public Polygon2D(Rectangle2D rec) {
if (rec == null) {
throw new IndexOutOfBoundsException("null Rectangle");
}
npoints = 4;
xpoints = new float[4];
ypoints = new float[4];
xpoints[0] = (float)rec.getMinX();
ypoints[0] = (float)rec.getMinY();
xpoints[1] = (float)rec.getMaxX();
ypoints[1] = (float)rec.getMinY();
xpoints[2] = (float)rec.getMaxX();
ypoints[2] = (float)rec.getMaxY();
xpoints[3] = (float)rec.getMinX();
ypoints[3] = (float)rec.getMaxY();
calculatePath();
}
/**
* Constructs and initializes a <code>Polygon2D</code> from the specified
* Polygon.
* @param pol the Polygon
* @exception NullPointerException pol is <code>null</code>.
*/
public Polygon2D(Polygon pol) {
if (pol == null) {
throw new IndexOutOfBoundsException("null Polygon");
}
this.npoints = pol.npoints;
this.xpoints = new float[pol.npoints];
this.ypoints = new float[pol.npoints];
for (int i = 0; i < pol.npoints; i++) {
xpoints[i] = pol.xpoints[i];
ypoints[i] = pol.ypoints[i];
}
calculatePath();
}
/**
* Constructs and initializes a <code>Polygon2D</code> from the specified
* parameters.
* @param xpoints an array of <i>x</i> coordinates
* @param ypoints an array of <i>y</i> coordinates
* @param npoints the total number of points in the <code>Polygon2D</code>
* @exception NegativeArraySizeException if the value of
* <code>npoints</code> is negative.
* @exception IndexOutOfBoundsException if <code>npoints</code> is
* greater than the length of <code>xpoints</code>
* or the length of <code>ypoints</code>.
* @exception NullPointerException if <code>xpoints</code> or
* <code>ypoints</code> is <code>null</code>.
*/
public Polygon2D(float[] xpoints, float[] ypoints, int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
}
this.npoints = npoints;
this.xpoints = new float[npoints];
this.ypoints = new float[npoints];
System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
calculatePath();
}
/**
* Constructs and initializes a <code>Polygon2D</code> from the specified
* parameters.
* @param xpoints an array of <i>x</i> coordinates
* @param ypoints an array of <i>y</i> coordinates
* @param npoints the total number of points in the <code>Polygon2D</code>
* @exception NegativeArraySizeException if the value of
* <code>npoints</code> is negative.
* @exception IndexOutOfBoundsException if <code>npoints</code> is
* greater than the length of <code>xpoints</code>
* or the length of <code>ypoints</code>.
* @exception NullPointerException if <code>xpoints</code> or
* <code>ypoints</code> is <code>null</code>.
*/
public Polygon2D(int[] xpoints, int[] ypoints, int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
}
this.npoints = npoints;
this.xpoints = new float[npoints];
this.ypoints = new float[npoints];
for (int i = 0; i < npoints; i++) {
this.xpoints[i] = xpoints[i];
this.ypoints[i] = ypoints[i];
}
calculatePath();
}
/**
* Resets this <code>Polygon</code> object to an empty polygon.
*/
public void reset() {
npoints = 0;
bounds = null;
path = new GeneralPath();
closedPath = null;
}
public Object clone() {
Polygon2D pol = new Polygon2D();
for (int i = 0; i < npoints; i++) {
pol.addPoint(xpoints[i], ypoints[i]);
}
return pol;
}
private void calculatePath() {
path = new GeneralPath();
path.moveTo(xpoints[0], ypoints[0]);
for (int i = 1; i < npoints; i++) {
path.lineTo(xpoints[i], ypoints[i]);
}
bounds = path.getBounds2D();
closedPath = null;
}
private void updatePath(float x, float y) {
closedPath = null;
if (path == null) {
path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(x, y);
bounds = new Rectangle2D.Float(x, y, 0, 0);
} else {
path.lineTo(x, y);
float _xmax = (float)bounds.getMaxX();
float _ymax = (float)bounds.getMaxY();
float _xmin = (float)bounds.getMinX();
float _ymin = (float)bounds.getMinY();
if (x < _xmin) _xmin = x;
else if (x > _xmax) _xmax = x;
if (y < _ymin) _ymin = y;
else if (y > _ymax) _ymax = y;
bounds = new Rectangle2D.Float(_xmin, _ymin, _xmax - _xmin, _ymax - _ymin);
}
}
/* get the associated {@link Polyline2D}.
*/
public Polyline2D getPolyline2D() {
Polyline2D pol = new Polyline2D( xpoints, ypoints, npoints );
pol.addPoint( xpoints[0], ypoints[0]);
return pol;
}
public Polygon getPolygon() {
int[] _xpoints = new int[npoints];
int[] _ypoints = new int[npoints];
for (int i = 0; i < npoints; i++) {
_xpoints[i] = (int)xpoints[i]; // todo maybe rounding is better ?
_ypoints[i] = (int)ypoints[i];
}
return new Polygon(_xpoints, _ypoints, npoints);
}
public void addPoint(Point2D p) {
addPoint((float)p.getX(), (float)p.getY());
}
/**
* Appends the specified coordinates to this <code>Polygon2D</code>.
* @param x the specified x coordinate
* @param y the specified y coordinate
*/
public void addPoint(float x, float y) {
if (npoints == xpoints.length) {
float[] tmp;
tmp = new float[npoints * 2];
System.arraycopy(xpoints, 0, tmp, 0, npoints);
xpoints = tmp;
tmp = new float[npoints * 2];
System.arraycopy(ypoints, 0, tmp, 0, npoints);
ypoints = tmp;
}
xpoints[npoints] = x;
ypoints[npoints] = y;
npoints++;
updatePath(x, y);
}
/**
* Determines whether the specified {@link Point} is inside this
* <code>Polygon</code>.
* @param p the specified <code>Point</code> to be tested
* @return <code>true</code> if the <code>Polygon</code> contains the
* <code>Point</code>; <code>false</code> otherwise.
* @see #contains(double, double)
*/
public boolean contains(Point p) {
return contains(p.x, p.y);
}
/**
* Determines whether the specified coordinates are inside this
* <code>Polygon</code>.
* <p>
* @param x the specified x coordinate to be tested
* @param y the specified y coordinate to be tested
* @return <code>true</code> if this <code>Polygon</code> contains
* the specified coordinates, (<i>x</i>, <i>y</i>);
* <code>false</code> otherwise.
*/
public boolean contains(int x, int y) {
return contains((double) x, (double) y);
}
/**
* Returns the high precision bounding box of the {@link Shape}.
* @return a {@link Rectangle2D} that precisely
* bounds the <code>Shape</code>.
*/
public Rectangle2D getBounds2D() {
return bounds;
}
public Rectangle getBounds() {
if (bounds == null) return null;
else return bounds.getBounds();
}
/**
* Determines if the specified coordinates are inside this
* <code>Polygon</code>. For the definition of
* <i>insideness</i>, see the class comments of {@link Shape}.
* @param x the specified x coordinate
* @param y the specified y coordinate
* @return <code>true</code> if the <code>Polygon</code> contains the
* specified coordinates; <code>false</code> otherwise.
*/
public boolean contains(double x, double y) {
if (npoints <= 2 || !bounds.contains(x, y)) {
return false;
}
updateComputingPath();
return closedPath.contains(x, y);
}
private void updateComputingPath() {
if (npoints >= 1) {
if (closedPath == null) {
closedPath = (GeneralPath)path.clone();
closedPath.closePath();
}
}
}
/**
* Tests if a specified {@link Point2D} is inside the boundary of this
* <code>Polygon</code>.
* @param p a specified <code>Point2D</code>
* @return <code>true</code> if this <code>Polygon</code> contains the
* specified <code>Point2D</code>; <code>false</code>
* otherwise.
* @see #contains(double, double)
*/
public boolean contains(Point2D p) {
return contains(p.getX(), p.getY());
}
/**
* Tests if the interior of this <code>Polygon</code> intersects the
* interior of a specified set of rectangular coordinates.
* @param x the x coordinate of the specified rectangular
* shape's top-left corner
* @param y the y coordinate of the specified rectangular
* shape's top-left corner
* @param w the width of the specified rectangular shape
* @param h the height of the specified rectangular shape
* @return <code>true</code> if the interior of this
* <code>Polygon</code> and the interior of the
* specified set of rectangular
* coordinates intersect each other;
* <code>false</code> otherwise.
*/
public boolean intersects(double x, double y, double w, double h) {
if (npoints <= 0 || !bounds.intersects(x, y, w, h)) {
return false;
}
updateComputingPath();
return closedPath.intersects(x, y, w, h);
}
/**
* Tests if the interior of this <code>Polygon</code> intersects the
* interior of a specified <code>Rectangle2D</code>.
* @param r a specified <code>Rectangle2D</code>
* @return <code>true</code> if this <code>Polygon</code> and the
* interior of the specified <code>Rectangle2D</code>
* intersect each other; <code>false</code>
* otherwise.
*/
public boolean intersects(Rectangle2D r) {
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* Tests if the interior of this <code>Polygon</code> entirely
* contains the specified set of rectangular coordinates.
* @param x the x coordinate of the top-left corner of the
* specified set of rectangular coordinates
* @param y the y coordinate of the top-left corner of the
* specified set of rectangular coordinates
* @param w the width of the set of rectangular coordinates
* @param h the height of the set of rectangular coordinates
* @return <code>true</code> if this <code>Polygon</code> entirely
* contains the specified set of rectangular
* coordinates; <code>false</code> otherwise.
*/
public boolean contains(double x, double y, double w, double h) {
if (npoints <= 0 || !bounds.intersects(x, y, w, h)) {
return false;
}
updateComputingPath();
return closedPath.contains(x, y, w, h);
}
/**
* Tests if the interior of this <code>Polygon</code> entirely
* contains the specified <code>Rectangle2D</code>.
* @param r the specified <code>Rectangle2D</code>
* @return <code>true</code> if this <code>Polygon</code> entirely
* contains the specified <code>Rectangle2D</code>;
* <code>false</code> otherwise.
* @see #contains(double, double, double, double)
*/
public boolean contains(Rectangle2D r) {
return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* Returns an iterator object that iterates along the boundary of this
* <code>Polygon</code> and provides access to the geometry
* of the outline of this <code>Polygon</code>. An optional
* {@link AffineTransform} can be specified so that the coordinates
* returned in the iteration are transformed accordingly.
* @param at an optional <code>AffineTransform</code> to be applied to the
* coordinates as they are returned in the iteration, or
* <code>null</code> if untransformed coordinates are desired
* @return a {@link PathIterator} object that provides access to the
* geometry of this <code>Polygon</code>.
*/
public PathIterator getPathIterator(AffineTransform at) {
updateComputingPath();
if (closedPath == null) return null;
else return closedPath.getPathIterator(at);
}
/**
* Returns an iterator object that iterates along the boundary of
* the <code>Polygon2D</code> and provides access to the geometry of the
* outline of the <code>Shape</code>. Only SEG_MOVETO, SEG_LINETO, and
* SEG_CLOSE point types are returned by the iterator.
* Since polygons are already flat, the <code>flatness</code> parameter
* is ignored.
* @param at an optional <code>AffineTransform</code> to be applied to the
* coordinates as they are returned in the iteration, or
* <code>null</code> if untransformed coordinates are desired
* @param flatness the maximum amount that the control points
* for a given curve can vary from colinear before a subdivided
* curve is replaced by a straight line connecting the
* endpoints. Since polygons are already flat the
* <code>flatness</code> parameter is ignored.
* @return a <code>PathIterator</code> object that provides access to the
* <code>Shape</code> object's geometry.
*/
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return getPathIterator(at);
}
}
/*
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.
*/
/**
* This class has the same behavior than {@link Polygon2D}, except that
* the figure is not closed.
*
* @version $Id: Polyline2D.java 594018 2007-11-12 04:17:41Z cam $
*/
class Polyline2D implements Shape, Cloneable, Serializable {
private static final float ASSUME_ZERO = 0.001f;
/**
* The total number of points. The value of <code>npoints</code>
* represents the number of points in this <code>Polyline2D</code>.
*
*/
public int npoints;
/**
* The array of <i>x</i> coordinates. The value of {@link #npoints npoints} is equal to the
* number of points in this <code>Polyline2D</code>.
*
*/
public float[] xpoints;
/**
* The array of <i>x</i> coordinates. The value of {@link #npoints npoints} is equal to the
* number of points in this <code>Polyline2D</code>.
*
*/
public float[] ypoints;
/**
* Bounds of the Polyline2D.
* @see #getBounds()
*/
protected Rectangle2D bounds;
private GeneralPath path;
private GeneralPath closedPath;
/**
* Creates an empty Polyline2D.
*/
public Polyline2D() {
xpoints = new float[4];
ypoints = new float[4];
}
/**
* Constructs and initializes a <code>Polyline2D</code> from the specified
* parameters.
* @param xpoints an array of <i>x</i> coordinates
* @param ypoints an array of <i>y</i> coordinates
* @param npoints the total number of points in the
* <code>Polyline2D</code>
* @exception NegativeArraySizeException if the value of
* <code>npoints</code> is negative.
* @exception IndexOutOfBoundsException if <code>npoints</code> is
* greater than the length of <code>xpoints</code>
* or the length of <code>ypoints</code>.
* @exception NullPointerException if <code>xpoints</code> or
* <code>ypoints</code> is <code>null</code>.
*/
public Polyline2D(float[] xpoints, float[] ypoints, int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
}
this.npoints = npoints;
this.xpoints = new float[npoints+1]; // make space for one more to close the polyline
this.ypoints = new float[npoints+1]; // make space for one more to close the polyline
System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
calculatePath();
}
/**
* Constructs and initializes a <code>Polyline2D</code> from the specified
* parameters.
* @param xpoints an array of <i>x</i> coordinates
* @param ypoints an array of <i>y</i> coordinates
* @param npoints the total number of points in the <code>Polyline2D</code>
* @exception NegativeArraySizeException if the value of
* <code>npoints</code> is negative.
* @exception IndexOutOfBoundsException if <code>npoints</code> is
* greater than the length of <code>xpoints</code>
* or the length of <code>ypoints</code>.
* @exception NullPointerException if <code>xpoints</code> or
* <code>ypoints</code> is <code>null</code>.
*/
public Polyline2D(int[] xpoints, int[] ypoints, int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
}
this.npoints = npoints;
this.xpoints = new float[npoints];
this.ypoints = new float[npoints];
for (int i = 0; i < npoints; i++) {
this.xpoints[i] = xpoints[i];
this.ypoints[i] = ypoints[i];
}
calculatePath();
}
public Polyline2D(Line2D line) {
npoints = 2;
xpoints = new float[2];
ypoints = new float[2];
xpoints[0] = (float)line.getX1();
xpoints[1] = (float)line.getX2();
ypoints[0] = (float)line.getY1();
ypoints[1] = (float)line.getY2();
calculatePath();
}
/**
* Resets this <code>Polyline2D</code> object to an empty polygon.
* The coordinate arrays and the data in them are left untouched
* but the number of points is reset to zero to mark the old
* vertex data as invalid and to start accumulating new vertex
* data at the beginning.
* All internally-cached data relating to the old vertices
* are discarded.
* Note that since the coordinate arrays from before the reset
* are reused, creating a new empty <code>Polyline2D</code> might
* be more memory efficient than resetting the current one if
* the number of vertices in the new polyline data is significantly
* smaller than the number of vertices in the data from before the
* reset.
*/
public void reset() {
npoints = 0;
bounds = null;
path = new GeneralPath();
closedPath = null;
}
public Object clone() {
Polyline2D pol = new Polyline2D();
for (int i = 0; i < npoints; i++) {
pol.addPoint(xpoints[i], ypoints[i]);
}
return pol;
}
private void calculatePath() {
path = new GeneralPath();
path.moveTo(xpoints[0], ypoints[0]);
for (int i = 1; i < npoints; i++) {
path.lineTo(xpoints[i], ypoints[i]);
}
bounds = path.getBounds2D();
closedPath = null;
}
private void updatePath(float x, float y) {
closedPath = null;
if (path == null) {
path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(x, y);
bounds = new Rectangle2D.Float(x, y, 0, 0);
} else {
path.lineTo(x, y);
float _xmax = (float)bounds.getMaxX();
float _ymax = (float)bounds.getMaxY();
float _xmin = (float)bounds.getMinX();
float _ymin = (float)bounds.getMinY();
if (x < _xmin) _xmin = x;
else if (x > _xmax) _xmax = x;
if (y < _ymin) _ymin = y;
else if (y > _ymax) _ymax = y;
bounds = new Rectangle2D.Float(_xmin, _ymin, _xmax - _xmin, _ymax - _ymin);
}
}
public void addPoint(Point2D p) {
addPoint((float)p.getX(), (float)p.getY());
}
/**
* Appends the specified coordinates to this <code>Polyline2D</code>.
* <p>
* If an operation that calculates the bounding box of this
* <code>Polyline2D</code> has already been performed, such as
* <code>getBounds</code> or <code>contains</code>, then this
* method updates the bounding box.
* @param x the specified x coordinate
* @param y the specified y coordinate
* @see java.awt.Polygon#getBounds
* @see java.awt.Polygon#contains(double,double)
*/
public void addPoint(float x, float y) {
if (npoints == xpoints.length) {
float[] tmp;
tmp = new float[npoints * 2];
System.arraycopy(xpoints, 0, tmp, 0, npoints);
xpoints = tmp;
tmp = new float[npoints * 2];
System.arraycopy(ypoints, 0, tmp, 0, npoints);
ypoints = tmp;
}
xpoints[npoints] = x;
ypoints[npoints] = y;
npoints++;
updatePath(x, y);
}
/**
* Gets the bounding box of this <code>Polyline2D</code>.
* The bounding box is the smallest {@link Rectangle} whose
* sides are parallel to the x and y axes of the
* coordinate space, and can completely contain the <code>Polyline2D</code>.
* @return a <code>Rectangle</code> that defines the bounds of this
* <code>Polyline2D</code>.
*/
public Rectangle getBounds() {
if (bounds == null) return null;
else return bounds.getBounds();
}
private void updateComputingPath() {
if (npoints >= 1) {
if (closedPath == null) {
closedPath = (GeneralPath)path.clone();
closedPath.closePath();
}
}
}
/**
* Determines whether the specified {@link Point} is inside this
* <code>Polyline2D</code>.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(Point p) {
return false;
}
/**
* Determines if the specified coordinates are inside this
* <code>Polyline2D</code>.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(double x, double y) {
return false;
}
/**
* Determines whether the specified coordinates are inside this
* <code>Polyline2D</code>.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(int x, int y) {
return false;
}
/**
* Returns the high precision bounding box of the {@link Shape}.
* @return a {@link Rectangle2D} that precisely
* bounds the <code>Shape</code>.
*/
public Rectangle2D getBounds2D() {
return bounds;
}
/**
* Tests if a specified {@link Point2D} is inside the boundary of this
* <code>Polyline2D</code>.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(Point2D p) {
return false;
}
/**
* Tests if the interior of this <code>Polygon</code> intersects the
* interior of a specified set of rectangular coordinates.
* @param x the x coordinate of the specified rectangular
* shape's top-left corner
* @param y the y coordinate of the specified rectangular
* shape's top-left corner
* @param w the width of the specified rectangular shape
* @param h the height of the specified rectangular shape
* @return <code>true</code> if the interior of this
* <code>Polygon</code> and the interior of the
* specified set of rectangular
* coordinates intersect each other;
* <code>false</code> otherwise.
*/
public boolean intersects(double x, double y, double w, double h) {
if (npoints <= 0 || !bounds.intersects(x, y, w, h)) {
return false;
}
updateComputingPath();
return closedPath.intersects(x, y, w, h);
}
/**
* Tests if the interior of this <code>Polygon</code> intersects the
* interior of a specified <code>Rectangle2D</code>.
* @param r a specified <code>Rectangle2D</code>
* @return <code>true</code> if this <code>Polygon</code> and the
* interior of the specified <code>Rectangle2D</code>
* intersect each other; <code>false</code>
* otherwise.
*/
public boolean intersects(Rectangle2D r) {
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* Tests if the interior of this <code>Polyline2D</code> entirely
* contains the specified set of rectangular coordinates.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(double x, double y, double w, double h) {
return false;
}
/**
* Tests if the interior of this <code>Polyline2D</code> entirely
* contains the specified <code>Rectangle2D</code>.
* This method is required to implement the Shape interface,
* but in the case of Line2D objects it always returns false since a line contains no area.
*/
public boolean contains(Rectangle2D r) {
return false;
}
/**
* Returns an iterator object that iterates along the boundary of this
* <code>Polygon</code> and provides access to the geometry
* of the outline of this <code>Polygon</code>. An optional
* {@link AffineTransform} can be specified so that the coordinates
* returned in the iteration are transformed accordingly.
* @param at an optional <code>AffineTransform</code> to be applied to the
* coordinates as they are returned in the iteration, or
* <code>null</code> if untransformed coordinates are desired
* @return a {@link PathIterator} object that provides access to the
* geometry of this <code>Polygon</code>.
*/
public PathIterator getPathIterator(AffineTransform at) {
if (path == null) return null;
else return path.getPathIterator(at);
}
/* get the associated {@link Polygon2D}.
* This method take care that may be the last point can
* be equal to the first. In that case it must not be included in the Polygon,
* as polygons declare their first point only once.
*/
public Polygon2D getPolygon2D() {
Polygon2D pol = new Polygon2D();
for (int i = 0; i < npoints - 1; i++) {
pol.addPoint(xpoints[i], ypoints[i]);
}
Point2D.Double p0 =
new Point2D.Double(xpoints[0], ypoints[0]);
Point2D.Double p1 =
new Point2D.Double(xpoints[npoints-1], ypoints[npoints-1]);
if (p0.distance(p1) > ASSUME_ZERO)
pol.addPoint(xpoints[npoints-1], ypoints[npoints-1]);
return pol;
}
/**
* Returns an iterator object that iterates along the boundary of
* the <code>Shape</code> and provides access to the geometry of the
* outline of the <code>Shape</code>. Only SEG_MOVETO and SEG_LINETO, point types
* are returned by the iterator.
* Since polylines are already flat, the <code>flatness</code> parameter
* is ignored.
* @param at an optional <code>AffineTransform</code> to be applied to the
* coordinates as they are returned in the iteration, or
* <code>null</code> if untransformed coordinates are desired
* @param flatness the maximum amount that the control points
* for a given curve can vary from colinear before a subdivided
* curve is replaced by a straight line connecting the
* endpoints. Since polygons are already flat the
* <code>flatness</code> parameter is ignored.
* @return a <code>PathIterator</code> object that provides access to the
* <code>Shape</code> object's geometry.
*/
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return path.getPathIterator(at);
}
}