1
2
3
4
5
6
7
8
9 package math.geom2d.curve;
10
11 import java.awt.Graphics2D;
12 import java.awt.Shape;
13 import java.util.ArrayList;
14 import java.util.Collection;
15
16 import math.geom2d.Point2D;
17 import math.geom2d.UnboundedShapeException;
18 import math.geom2d.polygon.LinearRing2D;
19 import math.geom2d.polygon.Polyline2D;
20
21
22
23
24
25
26 public abstract class AbstractContinuousCurve2D
27 implements ContinuousCurve2D, Cloneable {
28
29 protected static <T extends ContinuousCurve2D> Collection<T> wrapCurve(T curve) {
30 ArrayList<T> list = new ArrayList<T> (1);
31 list.add(curve);
32 return list;
33 }
34
35
36
37
38 public Polyline2D getAsPolyline(int n) {
39
40 if (!this.isBounded())
41 throw new UnboundedShapeException(this);
42
43
44 double t0 = this.getT0();
45 double dt = (this.getT1()-t0)/n;
46
47 if(this.isClosed()) {
48
49
50 Point2D[] points = new Point2D[n];
51 for(int i=0; i<n;i++)
52 points[i] = this.getPoint(t0 + i*dt);
53
54 return new LinearRing2D(points);
55 } else {
56
57
58 Point2D[] points = new Point2D[n+1];
59 for(int i=0; i<n+1;i++)
60 points[i] = this.getPoint(t0 + i*dt);
61
62 return new Polyline2D(points);
63 }
64 }
65
66
67
68
69 public Collection<? extends ContinuousCurve2D> getContinuousCurves() {
70 return wrapCurve(this);
71 }
72
73
74
75
76 public Point2D getFirstPoint() {
77 double t0 = this.getT0();
78 if(Double.isInfinite(t0))
79 throw new UnboundedShapeException(this);
80 return this.getPoint(t0);
81 }
82
83
84
85
86
87 public Point2D getLastPoint() {
88 double t1 = this.getT1();
89 if(Double.isInfinite(t1))
90 throw new UnboundedShapeException(this);
91 return this.getPoint(t1);
92 }
93
94
95
96
97 public void draw(Graphics2D g2) {
98 g2.draw(this.getAsAWTShape());
99 }
100
101
102
103
104 public Shape getAsAWTShape() {
105
106 if (!this.isBounded())
107 throw new UnboundedShapeException(this);
108
109 java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
110
111 Point2D point = this.getFirstPoint();
112 path.moveTo((float) point.getX(), (float) point.getY());
113 path = this.appendPath(path);
114 return path;
115 }
116
117 @Override
118 public abstract ContinuousCurve2D clone();
119 }