View Javadoc

1   /* file : ClosedPolyline2D.java
2    * 
3    * Project : geometry
4    *
5    * ===========================================
6    * 
7    * This library is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation, either version 2.1 of the License, or (at
10   * your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful, but 
13   * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   * or FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   * See the GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with this library. if not, write to :
20   * The Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21   * Boston, MA 02111-1307, USA.
22   * 
23   * Created on 16 avr. 2007
24   *
25   */
26  
27  package math.geom2d.line;
28  
29  import java.util.Collection;
30  
31  import math.geom2d.AffineTransform2D;
32  import math.geom2d.Point2D;
33  import math.geom2d.Shape2D;
34  import math.geom2d.polygon.Ring2D;
35  
36  /**
37   * Extends Polyline2D, by considering that the last point is connected to the
38   * first one. A ClosedPolyline2D can be used as boundary for Polygons.
39   * 
40   * @deprecated use math.geom2d.polygon.Ring2D instead (0.7.0)
41   * @author dlegland
42   */
43  @Deprecated
44  public class ClosedPolyline2D extends Ring2D  {
45  
46      public ClosedPolyline2D() {
47          super();
48      }
49  
50      public ClosedPolyline2D(Point2D initialPoint) {
51          super(initialPoint);
52      }
53  
54      public ClosedPolyline2D(Point2D[] points) {
55          super(points);
56      }
57  
58      public ClosedPolyline2D(double[] xcoords, double[] ycoords) {
59          super(xcoords, ycoords);
60      }
61  
62      public ClosedPolyline2D(Collection<? extends Point2D> points) {
63          super(points);
64      }
65  
66      // ===================================================================
67      // Methods specific to ClosedPolyline2D
68  
69  
70      /**
71       * Returns the closed polyline with same points taken in reverse order. The
72       * first points is still the same. Points of reverse curve are the same as
73       * the original curve (same pointers).
74       */
75      @Override
76      public ClosedPolyline2D getReverseCurve() {
77          Point2D[] points2 = new Point2D[points.size()];
78          int n = points.size();
79          if (n>0) {
80              points2[0] = points.get(0);
81              for (int i = 1; i<n; i++)
82                  points2[i] = points.get(n-i);
83          }
84          return new ClosedPolyline2D(points2);
85      }
86  
87      /**
88       * Return an instance of Polyline2D. If t1 is lower than t0, the returned
89       * Polyline contains the origin of the curve.
90       */
91      @Override
92      public Polyline2D getSubCurve(double t0, double t1) {
93          // code adapted from CurveSet2D
94  
95          Polyline2D res = new Polyline2D();
96  
97          // number of points in the polyline
98          int indMax = (int) this.getT1();
99  
100         // format to ensure t is between T0 and T1
101         t0 = Math.min(Math.max(t0, 0), indMax);
102         t1 = Math.min(Math.max(t1, 0), indMax);
103 
104         // find curves index
105         int ind0 = (int) Math.floor(t0+Shape2D.ACCURACY);
106         int ind1 = (int) Math.floor(t1+Shape2D.ACCURACY);
107 
108         // need to subdivide only one line segment
109         if (ind0==ind1&&t0<t1) {
110             // extract limit points
111             res.addPoint(this.getPoint(t0));
112             res.addPoint(this.getPoint(t1));
113             // return result
114             return res;
115         }
116 
117         // add the point corresponding to t0
118         res.addPoint(this.getPoint(t0));
119 
120         if (ind1>ind0) {
121             // add all the whole points between the 2 cuts
122             for (int n = ind0+1; n<=ind1; n++)
123                 res.addPoint(points.get(n));
124         } else {
125             // add all points until the end of the set
126             for (int n = ind0+1; n<indMax; n++)
127                 res.addPoint(points.get(n));
128 
129             // add all points from the beginning of the set
130             for (int n = 0; n<=ind1; n++)
131                 res.addPoint(points.get(n));
132         }
133 
134         // add the last point
135         res.addPoint(this.getPoint(t1));
136 
137         // return the curve set
138         return res;
139     }
140 
141     // ===================================================================
142     // Methods inherited from interface Shape2D
143 
144     /**
145      * Return the transformed shape, as a ClosePolyline2D.
146      */
147     @Override
148     public ClosedPolyline2D transform(AffineTransform2D trans) {
149         Point2D[] pts = new Point2D[points.size()];
150         for (int i = 0; i<points.size(); i++)
151             pts[i] = trans.transform(points.get(i));
152         return new ClosedPolyline2D(pts);
153     }
154     
155 }