forked from wupeixuan/JDKSourceCode1.8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath2D.java
More file actions
2706 lines (2562 loc) · 102 KB
/
Path2D.java
File metadata and controls
2706 lines (2562 loc) · 102 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
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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt.geom;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.Arrays;
import sun.awt.geom.Curve;
/**
* The {@code Path2D} class provides a simple, yet flexible
* shape which represents an arbitrary geometric path.
* It can fully represent any path which can be iterated by the
* {@link PathIterator} interface including all of its segment
* types and winding rules and it implements all of the
* basic hit testing methods of the {@link Shape} interface.
* <p>
* Use {@link Path2D.Float} when dealing with data that can be represented
* and used with floating point precision. Use {@link Path2D.Double}
* for data that requires the accuracy or range of double precision.
* <p>
* {@code Path2D} provides exactly those facilities required for
* basic construction and management of a geometric path and
* implementation of the above interfaces with little added
* interpretation.
* If it is useful to manipulate the interiors of closed
* geometric shapes beyond simple hit testing then the
* {@link Area} class provides additional capabilities
* specifically targeted at closed figures.
* While both classes nominally implement the {@code Shape}
* interface, they differ in purpose and together they provide
* two useful views of a geometric shape where {@code Path2D}
* deals primarily with a trajectory formed by path segments
* and {@code Area} deals more with interpretation and manipulation
* of enclosed regions of 2D geometric space.
* <p>
* The {@link PathIterator} interface has more detailed descriptions
* of the types of segments that make up a path and the winding rules
* that control how to determine which regions are inside or outside
* the path.
*
* @author Jim Graham
* @since 1.6
*/
public abstract class Path2D implements Shape, Cloneable {
/**
* An even-odd winding rule for determining the interior of
* a path.
*
* @see PathIterator#WIND_EVEN_ODD
* @since 1.6
*/
public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD;
/**
* A non-zero winding rule for determining the interior of a
* path.
*
* @see PathIterator#WIND_NON_ZERO
* @since 1.6
*/
public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO;
// For code simplicity, copy these constants to our namespace
// and cast them to byte constants for easy storage.
private static final byte SEG_MOVETO = (byte) PathIterator.SEG_MOVETO;
private static final byte SEG_LINETO = (byte) PathIterator.SEG_LINETO;
private static final byte SEG_QUADTO = (byte) PathIterator.SEG_QUADTO;
private static final byte SEG_CUBICTO = (byte) PathIterator.SEG_CUBICTO;
private static final byte SEG_CLOSE = (byte) PathIterator.SEG_CLOSE;
transient byte[] pointTypes;
transient int numTypes;
transient int numCoords;
transient int windingRule;
static final int INIT_SIZE = 20;
static final int EXPAND_MAX = 500;
static final int EXPAND_MAX_COORDS = EXPAND_MAX * 2;
static final int EXPAND_MIN = 10; // ensure > 6 (cubics)
/**
* Constructs a new empty {@code Path2D} object.
* It is assumed that the package sibling subclass that is
* defaulting to this constructor will fill in all values.
*
* @since 1.6
*/
/* private protected */
Path2D() {
}
/**
* Constructs a new {@code Path2D} object from the given
* specified initial values.
* This method is only intended for internal use and should
* not be made public if the other constructors for this class
* are ever exposed.
*
* @param rule the winding rule
* @param initialTypes the size to make the initial array to
* store the path segment types
* @since 1.6
*/
/* private protected */
Path2D(int rule, int initialTypes) {
setWindingRule(rule);
this.pointTypes = new byte[initialTypes];
}
abstract float[] cloneCoordsFloat(AffineTransform at);
abstract double[] cloneCoordsDouble(AffineTransform at);
abstract void append(float x, float y);
abstract void append(double x, double y);
abstract Point2D getPoint(int coordindex);
abstract void needRoom(boolean needMove, int newCoords);
abstract int pointCrossings(double px, double py);
abstract int rectCrossings(double rxmin, double rymin,
double rxmax, double rymax);
static byte[] expandPointTypes(byte[] oldPointTypes, int needed) {
final int oldSize = oldPointTypes.length;
final int newSizeMin = oldSize + needed;
if (newSizeMin < oldSize) {
// hard overflow failure - we can't even accommodate
// new items without overflowing
throw new ArrayIndexOutOfBoundsException(
"pointTypes exceeds maximum capacity !");
}
// growth algorithm computation
int grow = oldSize;
if (grow > EXPAND_MAX) {
grow = Math.max(EXPAND_MAX, oldSize >> 3); // 1/8th min
} else if (grow < EXPAND_MIN) {
grow = EXPAND_MIN;
}
assert grow > 0;
int newSize = oldSize + grow;
if (newSize < newSizeMin) {
// overflow in growth algorithm computation
newSize = Integer.MAX_VALUE;
}
while (true) {
try {
// try allocating the larger array
return Arrays.copyOf(oldPointTypes, newSize);
} catch (OutOfMemoryError oome) {
if (newSize == newSizeMin) {
throw oome;
}
}
newSize = newSizeMin + (newSize - newSizeMin) / 2;
}
}
/**
* The {@code Float} class defines a geometric path with
* coordinates stored in single precision floating point.
*
* @since 1.6
*/
public static class Float extends Path2D implements Serializable {
transient float floatCoords[];
/**
* Constructs a new empty single precision {@code Path2D} object
* with a default winding rule of {@link #WIND_NON_ZERO}.
*
* @since 1.6
*/
public Float() {
this(WIND_NON_ZERO, INIT_SIZE);
}
/**
* Constructs a new empty single precision {@code Path2D} object
* with the specified winding rule to control operations that
* require the interior of the path to be defined.
*
* @param rule the winding rule
* @see #WIND_EVEN_ODD
* @see #WIND_NON_ZERO
* @since 1.6
*/
public Float(int rule) {
this(rule, INIT_SIZE);
}
/**
* Constructs a new empty single precision {@code Path2D} object
* with the specified winding rule and the specified initial
* capacity to store path segments.
* This number is an initial guess as to how many path segments
* will be added to the path, but the storage is expanded as
* needed to store whatever path segments are added.
*
* @param rule the winding rule
* @param initialCapacity the estimate for the number of path segments
* in the path
* @see #WIND_EVEN_ODD
* @see #WIND_NON_ZERO
* @since 1.6
*/
public Float(int rule, int initialCapacity) {
super(rule, initialCapacity);
floatCoords = new float[initialCapacity * 2];
}
/**
* Constructs a new single precision {@code Path2D} object
* from an arbitrary {@link Shape} object.
* All of the initial geometry and the winding rule for this path are
* taken from the specified {@code Shape} object.
*
* @param s the specified {@code Shape} object
* @since 1.6
*/
public Float(Shape s) {
this(s, null);
}
/**
* Constructs a new single precision {@code Path2D} object
* from an arbitrary {@link Shape} object, transformed by an
* {@link AffineTransform} object.
* All of the initial geometry and the winding rule for this path are
* taken from the specified {@code Shape} object and transformed
* by the specified {@code AffineTransform} object.
*
* @param s the specified {@code Shape} object
* @param at the specified {@code AffineTransform} object
* @since 1.6
*/
public Float(Shape s, AffineTransform at) {
if (s instanceof Path2D) {
Path2D p2d = (Path2D) s;
setWindingRule(p2d.windingRule);
this.numTypes = p2d.numTypes;
// trim arrays:
this.pointTypes = Arrays.copyOf(p2d.pointTypes, p2d.numTypes);
this.numCoords = p2d.numCoords;
this.floatCoords = p2d.cloneCoordsFloat(at);
} else {
PathIterator pi = s.getPathIterator(at);
setWindingRule(pi.getWindingRule());
this.pointTypes = new byte[INIT_SIZE];
this.floatCoords = new float[INIT_SIZE * 2];
append(pi, false);
}
}
@Override
float[] cloneCoordsFloat(AffineTransform at) {
// trim arrays:
float ret[];
if (at == null) {
ret = Arrays.copyOf(floatCoords, numCoords);
} else {
ret = new float[numCoords];
at.transform(floatCoords, 0, ret, 0, numCoords / 2);
}
return ret;
}
@Override
double[] cloneCoordsDouble(AffineTransform at) {
// trim arrays:
double ret[] = new double[numCoords];
if (at == null) {
for (int i = 0; i < numCoords; i++) {
ret[i] = floatCoords[i];
}
} else {
at.transform(floatCoords, 0, ret, 0, numCoords / 2);
}
return ret;
}
void append(float x, float y) {
floatCoords[numCoords++] = x;
floatCoords[numCoords++] = y;
}
void append(double x, double y) {
floatCoords[numCoords++] = (float) x;
floatCoords[numCoords++] = (float) y;
}
Point2D getPoint(int coordindex) {
return new Point2D.Float(floatCoords[coordindex],
floatCoords[coordindex+1]);
}
@Override
void needRoom(boolean needMove, int newCoords) {
if ((numTypes == 0) && needMove) {
throw new IllegalPathStateException("missing initial moveto "+
"in path definition");
}
if (numTypes >= pointTypes.length) {
pointTypes = expandPointTypes(pointTypes, 1);
}
if (numCoords > (floatCoords.length - newCoords)) {
floatCoords = expandCoords(floatCoords, newCoords);
}
}
static float[] expandCoords(float[] oldCoords, int needed) {
final int oldSize = oldCoords.length;
final int newSizeMin = oldSize + needed;
if (newSizeMin < oldSize) {
// hard overflow failure - we can't even accommodate
// new items without overflowing
throw new ArrayIndexOutOfBoundsException(
"coords exceeds maximum capacity !");
}
// growth algorithm computation
int grow = oldSize;
if (grow > EXPAND_MAX_COORDS) {
grow = Math.max(EXPAND_MAX_COORDS, oldSize >> 3); // 1/8th min
} else if (grow < EXPAND_MIN) {
grow = EXPAND_MIN;
}
assert grow > needed;
int newSize = oldSize + grow;
if (newSize < newSizeMin) {
// overflow in growth algorithm computation
newSize = Integer.MAX_VALUE;
}
while (true) {
try {
// try allocating the larger array
return Arrays.copyOf(oldCoords, newSize);
} catch (OutOfMemoryError oome) {
if (newSize == newSizeMin) {
throw oome;
}
}
newSize = newSizeMin + (newSize - newSizeMin) / 2;
}
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final synchronized void moveTo(double x, double y) {
if (numTypes > 0 && pointTypes[numTypes - 1] == SEG_MOVETO) {
floatCoords[numCoords-2] = (float) x;
floatCoords[numCoords-1] = (float) y;
} else {
needRoom(false, 2);
pointTypes[numTypes++] = SEG_MOVETO;
floatCoords[numCoords++] = (float) x;
floatCoords[numCoords++] = (float) y;
}
}
/**
* Adds a point to the path by moving to the specified
* coordinates specified in float precision.
* <p>
* This method provides a single precision variant of
* the double precision {@code moveTo()} method on the
* base {@code Path2D} class.
*
* @param x the specified X coordinate
* @param y the specified Y coordinate
* @see Path2D#moveTo
* @since 1.6
*/
public final synchronized void moveTo(float x, float y) {
if (numTypes > 0 && pointTypes[numTypes - 1] == SEG_MOVETO) {
floatCoords[numCoords-2] = x;
floatCoords[numCoords-1] = y;
} else {
needRoom(false, 2);
pointTypes[numTypes++] = SEG_MOVETO;
floatCoords[numCoords++] = x;
floatCoords[numCoords++] = y;
}
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final synchronized void lineTo(double x, double y) {
needRoom(true, 2);
pointTypes[numTypes++] = SEG_LINETO;
floatCoords[numCoords++] = (float) x;
floatCoords[numCoords++] = (float) y;
}
/**
* Adds a point to the path by drawing a straight line from the
* current coordinates to the new specified coordinates
* specified in float precision.
* <p>
* This method provides a single precision variant of
* the double precision {@code lineTo()} method on the
* base {@code Path2D} class.
*
* @param x the specified X coordinate
* @param y the specified Y coordinate
* @see Path2D#lineTo
* @since 1.6
*/
public final synchronized void lineTo(float x, float y) {
needRoom(true, 2);
pointTypes[numTypes++] = SEG_LINETO;
floatCoords[numCoords++] = x;
floatCoords[numCoords++] = y;
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final synchronized void quadTo(double x1, double y1,
double x2, double y2)
{
needRoom(true, 4);
pointTypes[numTypes++] = SEG_QUADTO;
floatCoords[numCoords++] = (float) x1;
floatCoords[numCoords++] = (float) y1;
floatCoords[numCoords++] = (float) x2;
floatCoords[numCoords++] = (float) y2;
}
/**
* Adds a curved segment, defined by two new points, to the path by
* drawing a Quadratic curve that intersects both the current
* coordinates and the specified coordinates {@code (x2,y2)},
* using the specified point {@code (x1,y1)} as a quadratic
* parametric control point.
* All coordinates are specified in float precision.
* <p>
* This method provides a single precision variant of
* the double precision {@code quadTo()} method on the
* base {@code Path2D} class.
*
* @param x1 the X coordinate of the quadratic control point
* @param y1 the Y coordinate of the quadratic control point
* @param x2 the X coordinate of the final end point
* @param y2 the Y coordinate of the final end point
* @see Path2D#quadTo
* @since 1.6
*/
public final synchronized void quadTo(float x1, float y1,
float x2, float y2)
{
needRoom(true, 4);
pointTypes[numTypes++] = SEG_QUADTO;
floatCoords[numCoords++] = x1;
floatCoords[numCoords++] = y1;
floatCoords[numCoords++] = x2;
floatCoords[numCoords++] = y2;
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final synchronized void curveTo(double x1, double y1,
double x2, double y2,
double x3, double y3)
{
needRoom(true, 6);
pointTypes[numTypes++] = SEG_CUBICTO;
floatCoords[numCoords++] = (float) x1;
floatCoords[numCoords++] = (float) y1;
floatCoords[numCoords++] = (float) x2;
floatCoords[numCoords++] = (float) y2;
floatCoords[numCoords++] = (float) x3;
floatCoords[numCoords++] = (float) y3;
}
/**
* Adds a curved segment, defined by three new points, to the path by
* drawing a Bézier curve that intersects both the current
* coordinates and the specified coordinates {@code (x3,y3)},
* using the specified points {@code (x1,y1)} and {@code (x2,y2)} as
* Bézier control points.
* All coordinates are specified in float precision.
* <p>
* This method provides a single precision variant of
* the double precision {@code curveTo()} method on the
* base {@code Path2D} class.
*
* @param x1 the X coordinate of the first Bézier control point
* @param y1 the Y coordinate of the first Bézier control point
* @param x2 the X coordinate of the second Bézier control point
* @param y2 the Y coordinate of the second Bézier control point
* @param x3 the X coordinate of the final end point
* @param y3 the Y coordinate of the final end point
* @see Path2D#curveTo
* @since 1.6
*/
public final synchronized void curveTo(float x1, float y1,
float x2, float y2,
float x3, float y3)
{
needRoom(true, 6);
pointTypes[numTypes++] = SEG_CUBICTO;
floatCoords[numCoords++] = x1;
floatCoords[numCoords++] = y1;
floatCoords[numCoords++] = x2;
floatCoords[numCoords++] = y2;
floatCoords[numCoords++] = x3;
floatCoords[numCoords++] = y3;
}
int pointCrossings(double px, double py) {
if (numTypes == 0) {
return 0;
}
double movx, movy, curx, cury, endx, endy;
float coords[] = floatCoords;
curx = movx = coords[0];
cury = movy = coords[1];
int crossings = 0;
int ci = 2;
for (int i = 1; i < numTypes; i++) {
switch (pointTypes[i]) {
case PathIterator.SEG_MOVETO:
if (cury != movy) {
crossings +=
Curve.pointCrossingsForLine(px, py,
curx, cury,
movx, movy);
}
movx = curx = coords[ci++];
movy = cury = coords[ci++];
break;
case PathIterator.SEG_LINETO:
crossings +=
Curve.pointCrossingsForLine(px, py,
curx, cury,
endx = coords[ci++],
endy = coords[ci++]);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_QUADTO:
crossings +=
Curve.pointCrossingsForQuad(px, py,
curx, cury,
coords[ci++],
coords[ci++],
endx = coords[ci++],
endy = coords[ci++],
0);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_CUBICTO:
crossings +=
Curve.pointCrossingsForCubic(px, py,
curx, cury,
coords[ci++],
coords[ci++],
coords[ci++],
coords[ci++],
endx = coords[ci++],
endy = coords[ci++],
0);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_CLOSE:
if (cury != movy) {
crossings +=
Curve.pointCrossingsForLine(px, py,
curx, cury,
movx, movy);
}
curx = movx;
cury = movy;
break;
}
}
if (cury != movy) {
crossings +=
Curve.pointCrossingsForLine(px, py,
curx, cury,
movx, movy);
}
return crossings;
}
int rectCrossings(double rxmin, double rymin,
double rxmax, double rymax)
{
if (numTypes == 0) {
return 0;
}
float coords[] = floatCoords;
double curx, cury, movx, movy, endx, endy;
curx = movx = coords[0];
cury = movy = coords[1];
int crossings = 0;
int ci = 2;
for (int i = 1;
crossings != Curve.RECT_INTERSECTS && i < numTypes;
i++)
{
switch (pointTypes[i]) {
case PathIterator.SEG_MOVETO:
if (curx != movx || cury != movy) {
crossings =
Curve.rectCrossingsForLine(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
movx, movy);
}
// Count should always be a multiple of 2 here.
// assert((crossings & 1) != 0);
movx = curx = coords[ci++];
movy = cury = coords[ci++];
break;
case PathIterator.SEG_LINETO:
crossings =
Curve.rectCrossingsForLine(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
endx = coords[ci++],
endy = coords[ci++]);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_QUADTO:
crossings =
Curve.rectCrossingsForQuad(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
coords[ci++],
coords[ci++],
endx = coords[ci++],
endy = coords[ci++],
0);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_CUBICTO:
crossings =
Curve.rectCrossingsForCubic(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
coords[ci++],
coords[ci++],
coords[ci++],
coords[ci++],
endx = coords[ci++],
endy = coords[ci++],
0);
curx = endx;
cury = endy;
break;
case PathIterator.SEG_CLOSE:
if (curx != movx || cury != movy) {
crossings =
Curve.rectCrossingsForLine(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
movx, movy);
}
curx = movx;
cury = movy;
// Count should always be a multiple of 2 here.
// assert((crossings & 1) != 0);
break;
}
}
if (crossings != Curve.RECT_INTERSECTS &&
(curx != movx || cury != movy))
{
crossings =
Curve.rectCrossingsForLine(crossings,
rxmin, rymin,
rxmax, rymax,
curx, cury,
movx, movy);
}
// Count should always be a multiple of 2 here.
// assert((crossings & 1) != 0);
return crossings;
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final void append(PathIterator pi, boolean connect) {
float coords[] = new float[6];
while (!pi.isDone()) {
switch (pi.currentSegment(coords)) {
case SEG_MOVETO:
if (!connect || numTypes < 1 || numCoords < 1) {
moveTo(coords[0], coords[1]);
break;
}
if (pointTypes[numTypes - 1] != SEG_CLOSE &&
floatCoords[numCoords-2] == coords[0] &&
floatCoords[numCoords-1] == coords[1])
{
// Collapse out initial moveto/lineto
break;
}
lineTo(coords[0], coords[1]);
break;
case SEG_LINETO:
lineTo(coords[0], coords[1]);
break;
case SEG_QUADTO:
quadTo(coords[0], coords[1],
coords[2], coords[3]);
break;
case SEG_CUBICTO:
curveTo(coords[0], coords[1],
coords[2], coords[3],
coords[4], coords[5]);
break;
case SEG_CLOSE:
closePath();
break;
}
pi.next();
connect = false;
}
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final void transform(AffineTransform at) {
at.transform(floatCoords, 0, floatCoords, 0, numCoords / 2);
}
/**
* {@inheritDoc}
* @since 1.6
*/
public final synchronized Rectangle2D getBounds2D() {
float x1, y1, x2, y2;
int i = numCoords;
if (i > 0) {
y1 = y2 = floatCoords[--i];
x1 = x2 = floatCoords[--i];
while (i > 0) {
float y = floatCoords[--i];
float x = floatCoords[--i];
if (x < x1) x1 = x;
if (y < y1) y1 = y;
if (x > x2) x2 = x;
if (y > y2) y2 = y;
}
} else {
x1 = y1 = x2 = y2 = 0.0f;
}
return new Rectangle2D.Float(x1, y1, x2 - x1, y2 - y1);
}
/**
* {@inheritDoc}
* <p>
* The iterator for this class is not multi-threaded safe,
* which means that the {@code Path2D} class does not
* guarantee that modifications to the geometry of this
* {@code Path2D} object do not affect any iterations of
* that geometry that are already in process.
*
* @since 1.6
*/
public final PathIterator getPathIterator(AffineTransform at) {
if (at == null) {
return new CopyIterator(this);
} else {
return new TxIterator(this, at);
}
}
/**
* Creates a new object of the same class as this object.
*
* @return a clone of this instance.
* @exception OutOfMemoryError if there is not enough memory.
* @see java.lang.Cloneable
* @since 1.6
*/
public final Object clone() {
// Note: It would be nice to have this return Path2D
// but one of our subclasses (GeneralPath) needs to
// offer "public Object clone()" for backwards
// compatibility so we cannot restrict it further.
// REMIND: Can we do both somehow?
if (this instanceof GeneralPath) {
return new GeneralPath(this);
} else {
return new Path2D.Float(this);
}
}
/*
* JDK 1.6 serialVersionUID
*/
private static final long serialVersionUID = 6990832515060788886L;
/**
* Writes the default serializable fields to the
* {@code ObjectOutputStream} followed by an explicit
* serialization of the path segments stored in this
* path.
*
* @serialData
* <a name="Path2DSerialData"><!-- --></a>
* <ol>
* <li>The default serializable fields.
* There are no default serializable fields as of 1.6.
* <li>followed by
* a byte indicating the storage type of the original object
* as a hint (SERIAL_STORAGE_FLT_ARRAY)
* <li>followed by
* an integer indicating the number of path segments to follow (NP)
* or -1 to indicate an unknown number of path segments follows
* <li>followed by
* an integer indicating the total number of coordinates to follow (NC)
* or -1 to indicate an unknown number of coordinates follows
* (NC should always be even since coordinates always appear in pairs
* representing an x,y pair)
* <li>followed by
* a byte indicating the winding rule
* ({@link #WIND_EVEN_ODD WIND_EVEN_ODD} or
* {@link #WIND_NON_ZERO WIND_NON_ZERO})
* <li>followed by
* {@code NP} (or unlimited if {@code NP < 0}) sets of values consisting of
* a single byte indicating a path segment type
* followed by one or more pairs of float or double
* values representing the coordinates of the path segment
* <li>followed by
* a byte indicating the end of the path (SERIAL_PATH_END).
* </ol>
* <p>
* The following byte value constants are used in the serialized form
* of {@code Path2D} objects:
* <table>
* <tr>
* <th>Constant Name</th>
* <th>Byte Value</th>
* <th>Followed by</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>{@code SERIAL_STORAGE_FLT_ARRAY}</td>
* <td>0x30</td>
* <td></td>
* <td>A hint that the original {@code Path2D} object stored
* the coordinates in a Java array of floats.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_STORAGE_DBL_ARRAY}</td>
* <td>0x31</td>
* <td></td>
* <td>A hint that the original {@code Path2D} object stored
* the coordinates in a Java array of doubles.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_FLT_MOVETO}</td>
* <td>0x40</td>
* <td>2 floats</td>
* <td>A {@link #moveTo moveTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_FLT_LINETO}</td>
* <td>0x41</td>
* <td>2 floats</td>
* <td>A {@link #lineTo lineTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_FLT_QUADTO}</td>
* <td>0x42</td>
* <td>4 floats</td>
* <td>A {@link #quadTo quadTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_FLT_CUBICTO}</td>
* <td>0x43</td>
* <td>6 floats</td>
* <td>A {@link #curveTo curveTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_DBL_MOVETO}</td>
* <td>0x50</td>
* <td>2 doubles</td>
* <td>A {@link #moveTo moveTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_DBL_LINETO}</td>
* <td>0x51</td>
* <td>2 doubles</td>
* <td>A {@link #lineTo lineTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_DBL_QUADTO}</td>
* <td>0x52</td>
* <td>4 doubles</td>
* <td>A {@link #curveTo curveTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_DBL_CUBICTO}</td>
* <td>0x53</td>
* <td>6 doubles</td>
* <td>A {@link #curveTo curveTo} path segment follows.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_SEG_CLOSE}</td>
* <td>0x60</td>
* <td></td>
* <td>A {@link #closePath closePath} path segment.</td>
* </tr>
* <tr>
* <td>{@code SERIAL_PATH_END}</td>
* <td>0x61</td>
* <td></td>
* <td>There are no more path segments following.</td>
* </table>
*
* @since 1.6
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
{
super.writeObject(s, false);
}
/**
* Reads the default serializable fields from the
* {@code ObjectInputStream} followed by an explicit
* serialization of the path segments stored in this
* path.
* <p>
* There are no default serializable fields as of 1.6.
* <p>
* The serial data for this object is described in the
* writeObject method.
*
* @since 1.6
*/
private void readObject(java.io.ObjectInputStream s)
throws java.lang.ClassNotFoundException, java.io.IOException
{
super.readObject(s, false);
}
static class CopyIterator extends Path2D.Iterator {
float floatCoords[];
CopyIterator(Path2D.Float p2df) {
super(p2df);
this.floatCoords = p2df.floatCoords;
}
public int currentSegment(float[] coords) {
int type = path.pointTypes[typeIdx];
int numCoords = curvecoords[type];
if (numCoords > 0) {