View Javadoc

1   /**
2    * 
3    */
4   
5   package math.geom2d.grid;
6   
7   import java.util.ArrayList;
8   import java.util.Collection;
9   
10  import math.geom2d.Box2D;
11  import math.geom2d.Point2D;
12  import math.geom2d.point.PointArray2D;
13  import math.geom2d.point.PointSet2D;
14  import math.geom2d.Shape2D;
15  import math.geom2d.line.LineSegment2D;
16  
17  /**
18   * Defines a square grid, which can have different size in each direction. The
19   * grid is always parallel to the main axes.
20   * 
21   * @author dlegland
22   */
23  public class SquareGrid2D implements Grid2D {
24  
25      double x0 = 0;
26      double y0 = 0;
27  
28      double sx = 1;
29      double sy = 1;
30  
31      public SquareGrid2D() {
32  
33      }
34  
35      public SquareGrid2D(java.awt.geom.Point2D origin) {
36          this(origin.getX(), origin.getY(), 1, 1);
37      }
38  
39      public SquareGrid2D(java.awt.geom.Point2D origin, double s) {
40          this(origin.getX(), origin.getY(), s, s);
41      }
42  
43      public SquareGrid2D(java.awt.geom.Point2D origin, double sx, double sy) {
44          this(origin.getX(), origin.getY(), sx, sy);
45      }
46  
47      public SquareGrid2D(double x0, double y0, double s) {
48          this(x0, y0, s, s);
49      }
50  
51      public SquareGrid2D(double s) {
52          this(0, 0, s, s);
53      }
54  
55      public SquareGrid2D(double sx, double sy) {
56          this(0, 0, sx, sy);
57      }
58  
59      public SquareGrid2D(double x0, double y0, double sx, double sy) {
60          this.x0 = x0;
61          this.y0 = y0;
62          this.sx = sx;
63          this.sy = sy;
64      }
65  
66      /**
67       * @deprecated grids are supposed to be immutable (0.8.0)
68       */
69      @Deprecated
70      public void setOrigin(Point2D point) {
71          this.x0 = point.getX();
72          this.y0 = point.getY();
73      }
74  
75      public Point2D getOrigin() {
76          return new Point2D(x0, y0);
77      }
78  
79      public double getSizeX() {
80          return sx;
81      }
82  
83      public double getSizeY() {
84          return sy;
85      }
86  
87      /**
88       * @deprecated grids are supposed to be immutable (0.8.0)
89       */
90      @Deprecated
91      public void setSize(double s) {
92          sx = s;
93          sy = s;
94      }
95  
96      /**
97       * @deprecated grids are supposed to be immutable (0.8.0)
98       */
99      @Deprecated
100     public void setSize(double sx, double sy) {
101         this.sx = sx;
102         this.sy = sy;
103     }
104 
105     /*
106      * (non-Javadoc)
107      * 
108      * @see math.geom2d.grid.Grid2D#getClosestVertex(math.geom2d.Point2D)
109      */
110     public Point2D getClosestVertex(java.awt.geom.Point2D point) {
111         double nx = Math.round((point.getX()-x0)/sx);
112         double ny = Math.round((point.getY()-y0)/sy);
113         return new Point2D(nx*sx+x0, ny*sy+y0);
114     }
115 
116     /*
117      * (non-Javadoc)
118      * 
119      * @see math.geom2d.grid.Grid2D#getEdges(math.geom2d.Box2D)
120      */
121     public Collection<LineSegment2D> getEdges(Box2D box) {
122         double x, y; // iterations
123         double xmin, ymin, xmax, ymax; // limits
124         double xi, yi; // first point in the box
125 
126         // extract bounds of the box
127         xmin = box.getMinX();
128         ymin = box.getMinY();
129         xmax = box.getMaxX();
130         ymax = box.getMaxY();
131 
132         // coordinates of first vertex in the box
133         xi = Math.ceil((xmin-x0)/sx)*sx+x0;
134         yi = Math.ceil((ymin-y0)/sy)*sy+y0;
135 
136         ArrayList<LineSegment2D> array = new ArrayList<LineSegment2D>();
137 
138         // add horizontal lines
139         for (y = yi; y-ymax<Shape2D.ACCURACY; y += sy)
140             array.add(new LineSegment2D(xmin, y, xmax, y));
141 
142         // add vertical lines
143         for (x = xi; x-xmax<Shape2D.ACCURACY; x += sx)
144             array.add(new LineSegment2D(x, ymin, x, ymax));
145 
146         // return the set of lines
147         return array;
148     }
149 
150     /*
151      * (non-Javadoc)
152      * 
153      * @see math.geom2d.grid.Grid2D#getVertices(math.geom2d.Box2D)
154      */
155     public PointSet2D getVertices(Box2D box) {
156         double x, y; // iterations
157         double xmin, ymin, xmax, ymax; // limits
158         double xi, yi; // first point in the box
159 
160         // extract bounds of the box
161         xmin = box.getMinX();
162         ymin = box.getMinY();
163         xmax = box.getMaxX();
164         ymax = box.getMaxY();
165 
166         // coordinates of first vertex in the box
167         xi = Math.ceil((xmin-x0)/sx)*sx+x0;
168         yi = Math.ceil((ymin-y0)/sy)*sy+y0;
169 
170         ArrayList<Point2D> array = new ArrayList<Point2D>();
171 
172         // iterate on lines in each direction
173         for (y = yi; y-ymax<Shape2D.ACCURACY; y += sy)
174             for (x = xi; x-xmax<Shape2D.ACCURACY; x += sx)
175                 array.add(new Point2D(x, y));
176 
177         // return the set of lines
178         return new PointArray2D(array);
179     }
180 }