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
19
20
21
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
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
89
90 @Deprecated
91 public void setSize(double s) {
92 sx = s;
93 sy = s;
94 }
95
96
97
98
99 @Deprecated
100 public void setSize(double sx, double sy) {
101 this.sx = sx;
102 this.sy = sy;
103 }
104
105
106
107
108
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
118
119
120
121 public Collection<LineSegment2D> getEdges(Box2D box) {
122 double x, y;
123 double xmin, ymin, xmax, ymax;
124 double xi, yi;
125
126
127 xmin = box.getMinX();
128 ymin = box.getMinY();
129 xmax = box.getMaxX();
130 ymax = box.getMaxY();
131
132
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
139 for (y = yi; y-ymax<Shape2D.ACCURACY; y += sy)
140 array.add(new LineSegment2D(xmin, y, xmax, y));
141
142
143 for (x = xi; x-xmax<Shape2D.ACCURACY; x += sx)
144 array.add(new LineSegment2D(x, ymin, x, ymax));
145
146
147 return array;
148 }
149
150
151
152
153
154
155 public PointSet2D getVertices(Box2D box) {
156 double x, y;
157 double xmin, ymin, xmax, ymax;
158 double xi, yi;
159
160
161 xmin = box.getMinX();
162 ymin = box.getMinY();
163 xmax = box.getMaxX();
164 ymax = box.getMaxY();
165
166
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
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
178 return new PointArray2D(array);
179 }
180 }