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.line;
27
28
29
30 import java.awt.Graphics2D;
31 import java.awt.geom.GeneralPath;
32 import java.util.ArrayList;
33 import java.util.Collection;
34
35 import math.geom2d.AffineTransform2D;
36 import math.geom2d.Angle2D;
37 import math.geom2d.Box2D;
38 import math.geom2d.Point2D;
39 import math.geom2d.Shape2D;
40 import math.geom2d.UnboundedShapeException;
41 import math.geom2d.Vector2D;
42 import math.geom2d.circulinear.CircleLine2D;
43 import math.geom2d.conic.Circle2D;
44 import math.geom2d.domain.ContinuousBoundary2D;
45 import math.geom2d.domain.Domain2D;
46 import math.geom2d.domain.GenericDomain2D;
47 import math.geom2d.domain.SmoothBoundary2D;
48 import math.geom2d.polygon.Polyline2D;
49 import math.geom2d.transform.CircleInversion2D;
50
51
52
53
54
55
56 public class StraightLine2D extends AbstractLine2D implements
57 SmoothBoundary2D, Cloneable, CircleLine2D {
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public final static StraightLine2D create(java.awt.geom.Point2D point,
72 double angle) {
73 return new StraightLine2D(point.getX(), point.getY(), Math.cos(angle),
74 Math.sin(angle));
75 }
76
77
78
79
80 public final static StraightLine2D create(java.awt.geom.Point2D p1,
81 java.awt.geom.Point2D p2) {
82 return new StraightLine2D(p1, p2);
83 }
84
85
86
87
88
89 public final static StraightLine2D create(java.awt.geom.Point2D origin,
90 Vector2D direction) {
91 return new StraightLine2D(origin, direction);
92 }
93
94
95
96
97 @Deprecated
98 public final static StraightLine2D createStraightLine2D(
99 java.awt.geom.Point2D p1, java.awt.geom.Point2D p2) {
100 return new StraightLine2D(p1, p2);
101 }
102
103
104
105
106
107
108
109
110
111 public final static StraightLine2D createMedian(java.awt.geom.Point2D p1,
112 java.awt.geom.Point2D p2) {
113 Point2D mid = Point2D.midPoint(p1, p2);
114 StraightLine2D line = StraightLine2D.create(p1, p2);
115 return StraightLine2D.createPerpendicular(line, mid);
116 }
117
118
119
120
121
122
123
124
125
126 @Deprecated
127 public final static StraightLine2D createMedian2D(java.awt.geom.Point2D p1,
128 java.awt.geom.Point2D p2) {
129 Point2D mid = Point2D.midPoint(p1, p2);
130 StraightLine2D line = StraightLine2D.create(p1, p2);
131 return StraightLine2D.createPerpendicular(line, mid);
132 }
133
134
135
136
137
138
139
140 @Deprecated
141 public final static StraightLine2D createStraightLine2D(
142 java.awt.geom.Point2D point, double dx, double dy) {
143 return new StraightLine2D(point, dx, dy);
144 }
145
146
147
148
149
150
151
152 public final static StraightLine2D createParallel(LinearShape2D line,
153 java.awt.geom.Point2D point) {
154 return new StraightLine2D(line, point);
155 }
156
157
158
159
160
161
162
163 @Deprecated
164 public final static StraightLine2D createParallelLine2D(LinearShape2D line,
165 java.awt.geom.Point2D point) {
166 return new StraightLine2D(line, point);
167 }
168
169
170
171
172
173
174
175 public final static StraightLine2D createParallel(LinearShape2D linear,
176 double d) {
177 StraightLine2D line = linear.getSupportingLine();
178 double dd = Math.hypot(line.dx, line.dy);
179 return new StraightLine2D(line.x0+line.dy*d/dd, line.y0-line.dx*d/dd,
180 line.dx, line.dy);
181 }
182
183
184
185
186
187
188
189 @Deprecated
190 public final static StraightLine2D createParallelLine2D(
191 LinearShape2D linear, double d) {
192 StraightLine2D line = linear.getSupportingLine();
193 double dd = Math.hypot(line.dx, line.dy);
194 return new StraightLine2D(line.x0+line.dy*d/dd, line.y0-line.dx*d/dd,
195 line.dx, line.dy);
196 }
197
198
199
200
201
202
203
204 public final static StraightLine2D createPerpendicular(
205 LinearShape2D linear, Point2D point) {
206 StraightLine2D line = linear.getSupportingLine();
207 return new StraightLine2D(point, -line.dy, line.dx);
208 }
209
210
211
212
213
214
215
216 @Deprecated
217 public final static StraightLine2D createOrthogonalLine2D(
218 LinearShape2D linear, Point2D point) {
219 StraightLine2D line = linear.getSupportingLine();
220 return new StraightLine2D(point, -line.dy, line.dx);
221 }
222
223
224
225
226
227 public final static StraightLine2D createCartesian(double a, double b,
228 double c) {
229 return new StraightLine2D(a, b, c);
230 }
231
232
233
234
235
236
237
238 @Deprecated
239 public final static StraightLine2D createCartesianLine2D(double a,
240 double b, double c) {
241 return new StraightLine2D(a, b, c);
242 }
243
244
245
246
247
248
249 public final static Point2D getIntersection(java.awt.geom.Point2D p1,
250 java.awt.geom.Point2D p2, java.awt.geom.Point2D p3,
251 java.awt.geom.Point2D p4) {
252 StraightLine2D line1 = new StraightLine2D(p1, p2);
253 StraightLine2D line2 = new StraightLine2D(p3, p4);
254 return line1.getIntersection(line2);
255 }
256
257
258
259
260
261 public StraightLine2D() {
262 this(0, 0, 1, 0);
263 }
264
265
266 public StraightLine2D(java.awt.geom.Point2D point1,
267 java.awt.geom.Point2D point2) {
268 this(point1, new Vector2D(point1, point2));
269 }
270
271
272
273
274
275 public StraightLine2D(java.awt.geom.Point2D point, Vector2D direction) {
276 this(point.getX(), point.getY(), direction.getX(), direction.getY());
277 }
278
279
280
281
282
283 public StraightLine2D(java.awt.geom.Point2D point, double dx, double dy) {
284 this(point.getX(), point.getY(), dx, dy);
285 }
286
287
288
289
290
291 public StraightLine2D(java.awt.geom.Point2D point, double angle) {
292 this(point.getX(), point.getY(), Math.cos(angle), Math.sin(angle));
293 }
294
295
296
297
298
299 public StraightLine2D(LinearShape2D line) {
300 super(line);
301 }
302
303
304
305
306
307 public StraightLine2D(double xp, double yp, double dx, double dy) {
308 super(xp, yp, dx, dy);
309 }
310
311
312
313
314
315 public StraightLine2D(LinearShape2D line, java.awt.geom.Point2D point) {
316 this(point, line.getVector());
317 }
318
319
320
321
322
323
324 public StraightLine2D(double a, double b, double c) {
325 this(0, 0, 1, 0);
326 double d = a*a+b*b;
327 x0 = -a*c/d;
328 y0 = -b*c/d;
329 double theta = Math.atan2(-a, b);
330 dx = Math.cos(theta);
331 dy = Math.sin(theta);
332 }
333
334
335
336
337
338
339
340 @Deprecated
341 public void setLine(double x0, double y0, double dx, double dy) {
342 this.x0 = x0;
343 this.y0 = y0;
344 this.dx = dx;
345 this.dy = dy;
346 }
347
348
349
350
351 @Deprecated
352 public void setPoints(double x1, double y1, double x2, double y2) {
353 this.x0 = x1;
354 this.y0 = y1;
355 this.dx = x2-x1;
356 this.dy = y2-y1;
357 }
358
359
360
361
362 @Deprecated
363 public void setLine(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2) {
364 this.x0 = p1.getX();
365 this.y0 = p1.getY();
366 this.dx = p2.getX()-x0;
367 this.dy = p2.getY()-y0;
368 }
369
370
371
372
373 @Deprecated
374 public void setLine(LinearShape2D linear) {
375 StraightLine2D line = linear.getSupportingLine();
376 this.x0 = line.x0;
377 this.y0 = line.y0;
378 this.dx = line.dx;
379 this.dy = line.dy;
380 }
381
382
383
384
385 @Deprecated
386 public void setCartesianEquation(double a, double b, double c) {
387 dx = -b;
388 dy = a;
389 x0 = -a*c/(a*a+b*b);
390 y0 = -b*c/(a*a+b*b);
391 }
392
393
394
395
396
397 public StraightLine2D getParallel(java.awt.geom.Point2D point) {
398 return new StraightLine2D(point, dx, dy);
399 }
400
401
402
403
404
405
406
407
408
409 public StraightLine2D getParallel(double d) {
410 double dd = Math.sqrt(dx*dx+dy*dy);
411 return new StraightLine2D(x0+dy*d/dd, y0-dx*d/dd, dx, dy);
412 }
413
414
415
416
417
418 @Override
419 public StraightLine2D getPerpendicular(Point2D point) {
420 return new StraightLine2D(point, -dy, dx);
421 }
422
423
424
425
426 @Override
427 public CircleLine2D transform(CircleInversion2D inv) {
428
429 Point2D center = inv.getCenter();
430 double r = inv.getRadius();
431
432 Point2D po = this.getProjectedPoint(center);
433 double d = this.getDistance(po);
434
435
436
437 if (Math.abs(d)<Shape2D.ACCURACY){
438 return new StraightLine2D(this);
439 }
440
441
442 double angle = Angle2D.getHorizontalAngle(center, po);
443
444
445 double r2 = r*r/d/2;
446 Point2D c2 = Point2D.createPolar(center, r2, angle);
447
448
449 boolean direct = !this.isInside(center);
450
451
452 return new Circle2D(c2, r2, direct);
453 }
454
455
456
457
458
459 public Collection<ContinuousBoundary2D> getBoundaryCurves() {
460 ArrayList<ContinuousBoundary2D> list = new ArrayList<ContinuousBoundary2D>(
461 1);
462 list.add(this);
463 return list;
464 }
465
466 public Domain2D getDomain() {
467 return new GenericDomain2D(this);
468 }
469
470 public void fill(Graphics2D g2) {
471 g2.fill(this.getGeneralPath());
472 }
473
474
475
476
477 @Override
478 public double getWindingAngle(java.awt.geom.Point2D point) {
479
480 double angle1 = Angle2D.getHorizontalAngle(-dx, -dy);
481 double angle2 = Angle2D.getHorizontalAngle(dx, dy);
482
483 if (this.isInside(point)) {
484 if (angle2>angle1)
485 return angle2-angle1;
486 else
487 return 2*Math.PI-angle1+angle2;
488 } else {
489 if (angle2>angle1)
490 return angle2-angle1-2*Math.PI;
491 else
492 return angle2-angle1;
493 }
494 }
495
496
497
498
499
500
501
502
503 @Override
504 public Polyline2D getAsPolyline(int n) {
505 throw new UnboundedShapeException(this);
506 }
507
508
509
510
511
512
513 @Override
514 public Point2D getFirstPoint() {
515 throw new UnboundedShapeException(this);
516 }
517
518
519 @Override
520 public Point2D getLastPoint() {
521 throw new UnboundedShapeException(this);
522 }
523
524
525 @Override
526 public Collection<Point2D> getSingularPoints() {
527 return new ArrayList<Point2D>(0);
528 }
529
530
531 @Override
532 public boolean isSingular(double pos) {
533 return false;
534 }
535
536
537
538
539
540 public double getT0() {
541 return Double.NEGATIVE_INFINITY;
542 }
543
544
545
546
547
548 public double getT1() {
549 return Double.POSITIVE_INFINITY;
550 }
551
552
553
554
555 public Point2D getPoint(double t) {
556 return new Point2D(x0+dx*t, y0+dy*t);
557 }
558
559
560
561
562 @Override
563 public Collection<? extends StraightLine2D> getContinuousCurves() {
564 ArrayList<StraightLine2D> list =
565 new ArrayList<StraightLine2D>(1);
566 list.add(this);
567 return list;
568 }
569
570
571
572
573
574 public StraightLine2D getReverseCurve() {
575 return new StraightLine2D(this.x0, this.y0, -this.dx, -this.dy);
576 }
577
578 public GeneralPath appendPath(GeneralPath path) {
579 throw new UnboundedShapeException(this);
580 }
581
582
583
584
585
586 public boolean isBounded() {
587 return false;
588 }
589
590
591
592
593 @Override
594 public double getDistance(double x, double y) {
595 Point2D proj = super.getProjectedPoint(x, y);
596 return proj.distance(x, y);
597 }
598
599 public Box2D getBoundingBox() {
600 if (Math.abs(dx)<0)
601 return new Box2D(
602 x0, x0,
603 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
604 if (Math.abs(dy)<0)
605 return new Box2D(
606 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
607 x0, y0);
608
609 return new Box2D(
610 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
611 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
612 }
613
614
615
616
617 @Override
618 public StraightLine2D transform(AffineTransform2D trans) {
619 double[] tab = trans.getCoefficients();
620 return new StraightLine2D(
621 x0*tab[0]+y0*tab[1]+tab[2],
622 x0*tab[3]+y0*tab[4]+tab[5],
623 dx*tab[0]+dy*tab[1],
624 dx*tab[3]+dy*tab[4]);
625 }
626
627
628
629
630
631
632
633
634
635 public boolean contains(double x, double y) {
636 return super.supportContains(x, y);
637 }
638
639
640
641
642
643 @Override
644 public boolean contains(java.awt.geom.Point2D p) {
645 return super.supportContains(p.getX(), p.getY());
646 }
647
648
649 public java.awt.geom.GeneralPath getGeneralPath() {
650 throw new UnboundedShapeException(this);
651 }
652
653
654
655
656
657 @Override
658 public String toString() {
659 return new String("StraightLine2D(" + x0 + "," + y0 + "," +
660 dx + "," + dy + ")");
661 }
662
663 @Override
664 public boolean equals(Object obj) {
665 if (!(obj instanceof StraightLine2D))
666 return false;
667 StraightLine2D line = (StraightLine2D) obj;
668 if (Math.abs(x0-line.x0)>Shape2D.ACCURACY)
669 return false;
670 if (Math.abs(y0-line.y0)>Shape2D.ACCURACY)
671 return false;
672 if (Math.abs(dx-line.dx)>Shape2D.ACCURACY)
673 return false;
674 if (Math.abs(dy-line.dy)>Shape2D.ACCURACY)
675 return false;
676 return true;
677 }
678
679 @Override
680 public StraightLine2D clone() {
681 return new StraightLine2D(x0, y0, dx, dy);
682 }
683 }