View Javadoc

1   /**
2    * File: 	AbstractContinuousCurve2D.java
3    * Project: javaGeom
4    * 
5    * Distributed under the LGPL License.
6    *
7    * Created: 21 mai 09
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   * Provides a base implementation for smooth curves.
24   * @author dlegland
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  	/* (non-Javadoc)
36  	 * @see math.geom2d.curve.ContinuousCurve2D#getAsPolyline(int)
37  	 */
38  	public Polyline2D getAsPolyline(int n) {
39  		// Check that the curve is bounded
40          if (!this.isBounded())
41              throw new UnboundedShapeException(this);
42  
43          // compute start and increment values
44          double t0 = this.getT0();
45          double dt = (this.getT1()-t0)/n;
46  
47  		if(this.isClosed()) {
48  			// compute position of points, without the last one, 
49  			// which is included by default with linear rings
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  			// allocate array of points, and compute each value.
57  			// Computes also value for last point.
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  	/* (non-Javadoc)
67  	 * @see math.geom2d.curve.Curve2D#getContinuousCurves()
68  	 */
69  	public Collection<? extends ContinuousCurve2D> getContinuousCurves() {
70  		return wrapCurve(this);
71  	}
72  
73  	/* (non-Javadoc)
74  	 * @see math.geom2d.curve.Curve2D#getFirstPoint()
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  	/* (non-Javadoc)
85  	 * @see math.geom2d.curve.Curve2D#getLastPoint()
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  	/* (non-Javadoc)
95  	 * @see math.geom2d.curve.Curve2D#draw(java.awt.Graphics2D)
96  	 */
97  	public void draw(Graphics2D g2) {
98  		g2.draw(this.getAsAWTShape());
99  	}
100 
101 	/* (non-Javadoc)
102 	 * @see math.geom2d.curve.Curve2D#getAsAWTShape()
103 	 */
104 	public Shape getAsAWTShape() {
105 		// Check that the curve is bounded
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 }