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 package math.geom2d;
27
28 import java.awt.Graphics2D;
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Iterator;
33
34 import math.geom2d.circulinear.CirculinearDomain2D;
35 import math.geom2d.circulinear.CirculinearShape2D;
36 import math.geom2d.circulinear.GenericCirculinearDomain2D;
37 import math.geom2d.conic.Circle2D;
38 import math.geom2d.point.PointArray2D;
39 import math.geom2d.point.PointShape2D;
40 import math.geom2d.transform.CircleInversion2D;
41
42
43
44
45
46
47
48
49
50
51
52 public class Point2D extends java.awt.geom.Point2D.Double
53 implements PointShape2D, Cloneable, CirculinearShape2D {
54
55
56
57
58 private static final long serialVersionUID = 1L;
59
60
61
62
63
64
65
66 @Deprecated
67 public final static Point2D INFINITY_POINT = new Point2D(
68 java.lang.Double.POSITIVE_INFINITY,
69 java.lang.Double.POSITIVE_INFINITY);
70
71
72
73
74
75
76
77
78
79
80 public Point2D() {
81 super(0, 0);
82 }
83
84
85
86
87
88 public Point2D(double x, double y) {
89 super(x, y);
90 }
91
92
93
94
95
96 public Point2D(java.awt.geom.Point2D point) {
97 super(point.getX(), point.getY());
98 }
99
100
101
102
103
104
105 @Deprecated
106 public Point2D(java.awt.geom.Point2D point1, java.awt.geom.Point2D point2) {
107 super(point1.getX()+point2.getX(), point1.getY()+point2.getY());
108 }
109
110
111
112
113
114
115 @Deprecated
116 public Point2D(java.awt.geom.Point2D point1, double x, double y) {
117 super(point1.getX()+x, point1.getY()+y);
118 }
119
120
121
122
123
124
125
126
127
128 public final static Point2D create(double x, double y) {
129 return new Point2D(x, y);
130 }
131
132
133
134
135
136
137 public final static Point2D create(java.awt.geom.Point2D point) {
138 return new Point2D(point.getX(), point.getY());
139 }
140
141
142
143
144
145 public final static Point2D createPolar(double rho, double theta) {
146 return new Point2D(rho*Math.cos(theta), rho*Math.sin(theta));
147 }
148
149
150
151
152
153 public final static Point2D createPolar(Point2D point, double rho,
154 double theta) {
155 return new Point2D(
156 point.getX()+rho*Math.cos(theta),
157 point.getY()+rho*Math.sin(theta));
158 }
159
160
161
162
163
164 public final static Point2D createPolar(double x0, double y0, double rho,
165 double theta) {
166 return new Point2D(x0+rho*Math.cos(theta), y0+rho*Math.sin(theta));
167 }
168
169
170
171
172
173
174
175 public final static double getDistance(double x1, double y1, double x2,
176 double y2) {
177 return Math.hypot(x2-x1, y2-y1);
178 }
179
180
181
182
183
184
185
186
187 public final static double getDistance(java.awt.geom.Point2D p1,
188 java.awt.geom.Point2D p2) {
189 return Math.hypot(p1.getX()-p2.getX(), p1.getY()-p2.getY());
190 }
191
192
193
194
195
196
197 public final static boolean isColinear(java.awt.geom.Point2D p1,
198 java.awt.geom.Point2D p2, java.awt.geom.Point2D p3) {
199 double dx1, dx2, dy1, dy2;
200 dx1 = p2.getX()-p1.getX();
201 dy1 = p2.getY()-p1.getY();
202 dx2 = p3.getX()-p1.getX();
203 dy2 = p3.getY()-p1.getY();
204
205
206 return Math.abs(dx1*dy2-dy1*dx2)<Shape2D.ACCURACY;
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220 public final static int ccw(Point2D p0, Point2D p1, Point2D p2) {
221 double x0 = p0.getX();
222 double y0 = p0.getY();
223 double dx1 = p1.getX()-x0;
224 double dy1 = p1.getY()-y0;
225 double dx2 = p2.getX()-x0;
226 double dy2 = p2.getY()-y0;
227
228 if (dx1*dy2>dy1*dx2)
229 return +1;
230 if (dx1*dy2<dy1*dx2)
231 return -1;
232 if ((dx1*dx2<0)||(dy1*dy2<0))
233 return -1;
234 if ((dx1*dx1+dy1*dy1)<(dx2*dx2+dy2*dy2))
235 return +1;
236 return 0;
237 }
238
239 public final static Point2D midPoint(java.awt.geom.Point2D p1,
240 java.awt.geom.Point2D p2) {
241 return new Point2D((p1.getX()+p2.getX())/2, (p1.getY()+p2.getY())/2);
242 }
243
244
245
246
247
248
249
250 public final static Point2D centroid(java.awt.geom.Point2D[] points) {
251 int n = points.length;
252 double sx = 0, sy = 0;
253 for (int i = 0; i<n; i++) {
254 sx += points[i].getX();
255 sy += points[i].getY();
256 }
257 return new Point2D(sx/n, sy/n);
258 }
259
260
261
262
263
264
265
266
267
268 public final static Point2D centroid(
269 java.awt.geom.Point2D[] points,
270 double[] weights) {
271
272 int n = points.length;
273
274
275 if(n!=weights.length) {
276 throw new RuntimeException("Arrays must have the same size");
277 }
278
279
280 double sx = 0, sy = 0, sw=0;
281 double w;
282 for (int i = 0; i<n; i++) {
283 w = weights[i];
284 sx += points[i].getX()*w;
285 sy += points[i].getY()*w;
286 sw += w;
287 }
288
289
290 return new Point2D(sx/sw, sy/sw);
291 }
292
293
294
295
296
297
298
299 public final static Point2D centroid(Collection<? extends Point2D> points) {
300 int n = points.size();
301 double sx = 0, sy = 0;
302 for (Point2D point : points) {
303 sx += point.getX();
304 sy += point.getY();
305 }
306 return new Point2D(sx/n, sy/n);
307 }
308
309
310
311
312
313
314
315
316
317 public final static Point2D centroid(java.awt.geom.Point2D pt1,
318 java.awt.geom.Point2D pt2, java.awt.geom.Point2D pt3) {
319 return new Point2D(
320 (pt1.getX()+pt2.getX()+pt3.getX())/3,
321 (pt1.getY()+pt2.getY()+pt3.getY())/3);
322 }
323
324
325
326
327
328 public Point2D plus(java.awt.geom.Point2D p) {
329 return new Point2D(p.getX()+x, p.getY()+y);
330 }
331
332 public Point2D minus(java.awt.geom.Point2D p) {
333 return new Point2D(x-p.getX(), y-p.getY());
334 }
335
336
337
338
339
340
341
342 public Point2D translate(double tx, double ty) {
343 return new Point2D(this.x+tx, this.y+ty);
344 }
345
346
347
348
349
350
351
352 public Point2D scale(double kx, double ky) {
353 return new Point2D(this.x*kx, this.y*ky);
354 }
355
356
357
358
359
360
361 public Point2D scale(double k) {
362 return new Point2D(this.x*k, this.y*k);
363 }
364
365
366
367
368
369
370 public Point2D rotate(double theta){
371 double cot = Math.cos(theta);
372 double sit = Math.sin(theta);
373 return new Point2D(x*cot-y*sit, x*sit+y*cot);
374 }
375
376
377
378
379
380
381
382 public Point2D rotate(Point2D center, double theta){
383 double cx = center.getX();
384 double cy = center.getY();
385 double cot = Math.cos(theta);
386 double sit = Math.sin(theta);
387 return new Point2D(
388 x*cot-y*sit+(1-cot)*cx+sit*cy,
389 x*sit+y*cot+(1-cot)*cy-sit*cx);
390 }
391
392
393
394
395
396
397
398
399
400
401 public java.awt.Point getAsInt() {
402 return new java.awt.Point((int) x, (int) y);
403 }
404
405
406
407
408 public java.awt.geom.Point2D.Double getAsDouble() {
409 return new java.awt.geom.Point2D.Double(x, y);
410 }
411
412
413
414
415
416 public java.awt.geom.Point2D.Float getAsFloat() {
417 return new java.awt.geom.Point2D.Float((float) x, (float) y);
418 }
419
420
421
422
423
424
425 @Deprecated
426 public void setPolarLocation(double rho, double theta) {
427 x = rho*Math.cos(theta);
428 y = rho*Math.sin(theta);
429 }
430
431
432
433
434
435
436 @Deprecated
437 public void setPolarLocation(java.awt.geom.Point2D point, double rho,
438 double theta) {
439 x = point.getX()+rho*Math.cos(theta);
440 y = point.getY()+rho*Math.sin(theta);
441 }
442
443
444
445
446
447
448
449
450 public CirculinearDomain2D getBuffer(double dist) {
451 return new GenericCirculinearDomain2D(
452 new Circle2D(this, Math.abs(dist), dist>0));
453 }
454
455
456
457
458 public Point2D transform(CircleInversion2D inv) {
459
460 Point2D center = inv.getCenter();
461 double r = inv.getRadius();
462
463
464 double d = r*r/Point2D.getDistance(this, center);
465 double theta = Angle2D.getHorizontalAngle(center, this);
466
467
468 return Point2D.createPolar(center, d, theta);
469 }
470
471
472
473
474
475
476
477
478 public double getDistance(java.awt.geom.Point2D point) {
479 return getDistance(point.getX(), point.getY());
480 }
481
482
483
484
485
486
487 public double getDistance(double x, double y) {
488 return Math.hypot(getX()-x, getY()-y);
489 }
490
491
492
493
494
495
496
497 public boolean isBounded() {
498 if (java.lang.Double.isInfinite(this.x))
499 return false;
500 if (java.lang.Double.isInfinite(this.y))
501 return false;
502 if (java.lang.Double.isNaN(this.x))
503 return false;
504 if (java.lang.Double.isNaN(this.y))
505 return false;
506 return true;
507 }
508
509 public boolean isEmpty() {
510 return false;
511 }
512
513
514
515
516 public boolean contains(double x, double y) {
517 return this.equals(new Point2D(x, y));
518 }
519
520
521
522
523 public boolean contains(java.awt.geom.Point2D p) {
524 return this.equals(p);
525 }
526
527
528
529
530
531 public math.geom2d.point.PointSet2D clip(Box2D box) {
532
533 math.geom2d.point.PointSet2D set = new PointArray2D(0);
534
535
536 if (x<box.getMinX())
537 return set;
538 if (y<box.getMinY())
539 return set;
540 if (x>box.getMaxX())
541 return set;
542 if (y>box.getMaxY())
543 return set;
544
545
546 set.addPoint(this);
547 return set;
548 }
549
550
551
552
553
554 public Box2D getBoundingBox() {
555 return new Box2D(getX(), getX(), getY(), getY());
556 }
557
558
559
560
561 public Point2D transform(AffineTransform2D trans) {
562 double[] tab = trans.getCoefficients();
563 return new Point2D(
564 x*tab[0]+y*tab[1]+tab[2],
565 x*tab[3]+y*tab[4]+tab[5]);
566 }
567
568
569
570
571
572
573
574
575
576
577 public void draw(Graphics2D g2) {
578 this.draw(g2, 1);
579 }
580
581
582
583
584
585
586
587 public void draw(Graphics2D g2, double r) {
588 g2.fill(new java.awt.geom.Ellipse2D.Double(x-r, y-r, 2*r, 2*r));
589 }
590
591
592
593
594
595
596
597 public int getPointNumber() {
598 return 1;
599 }
600
601
602
603
604 public Collection<Point2D> getPoints() {
605 ArrayList<Point2D> array = new ArrayList<Point2D>(1);
606 array.add(this);
607 return array;
608 }
609
610
611
612
613 public Iterator<Point2D> iterator() {
614 return this.getPoints().iterator();
615 }
616
617
618
619
620
621 @Override
622 public String toString() {
623 return new String("Point2D(" + x + ", "+y+")");
624 }
625
626
627
628
629
630 @Override
631 public boolean equals(Object obj) {
632 if (!(obj instanceof java.awt.geom.Point2D))
633 return false;
634 java.awt.geom.Point2D p = (java.awt.geom.Point2D) obj;
635 return this.distance(p.getX(), p.getY())<Shape2D.ACCURACY;
636 }
637
638
639
640
641 @Override
642 public Point2D clone() {
643 return new Point2D(x, y);
644 }
645 }