root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 38599
History | View | Annotate | Download (8.83 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.fmap.geom.primitive.impl; |
23 |
|
24 |
import java.awt.Rectangle; |
25 |
import java.awt.Shape; |
26 |
import java.awt.geom.AffineTransform; |
27 |
import java.awt.geom.PathIterator; |
28 |
import java.awt.geom.Rectangle2D; |
29 |
|
30 |
import org.cresques.cts.ICoordTrans; |
31 |
import org.cresques.cts.IProjection; |
32 |
|
33 |
import org.gvsig.fmap.geom.DirectPosition; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.handler.AbstractHandler; |
36 |
import org.gvsig.fmap.geom.handler.FinalHandler; |
37 |
import org.gvsig.fmap.geom.handler.Handler; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.geom.primitive.FShape; |
40 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.fmap.geom.primitive.PointIterator; |
43 |
import org.gvsig.fmap.geom.type.GeometryType; |
44 |
|
45 |
/**
|
46 |
* 2D point implementation.
|
47 |
*
|
48 |
* @author Vicente Caballero Navarro
|
49 |
* @author gvSIG team
|
50 |
*/
|
51 |
public class Point2D extends AbstractPrimitive implements Point, DirectPosition { |
52 |
|
53 |
private static final long serialVersionUID = 1836257305083881299L; |
54 |
|
55 |
protected static final String COORDINATES_FIELD = "coordinates"; |
56 |
protected double x; |
57 |
protected double y; |
58 |
|
59 |
/**
|
60 |
* The constructor with the GeometryType like and argument
|
61 |
* is used by the {@link GeometryType}{@link #create()} to create the
|
62 |
* geometry
|
63 |
*
|
64 |
* @param type
|
65 |
* The geometry type
|
66 |
*/
|
67 |
public Point2D(GeometryType geometryType) { |
68 |
super(geometryType, null, null); |
69 |
this.x = 0.0d; |
70 |
this.y = 0.0d; |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Constructor used in the {@link Geometry#cloneGeometry()} method
|
75 |
*/
|
76 |
Point2D(GeometryType geometryType, String id, IProjection projection, |
77 |
double x, double y) { |
78 |
super(geometryType, id, projection);
|
79 |
this.x = x;
|
80 |
this.y = y;
|
81 |
} |
82 |
|
83 |
public Point2D(double x, double y) { |
84 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
85 |
this.x = x;
|
86 |
this.y = y;
|
87 |
} |
88 |
|
89 |
public Point2D(GeometryType geometryType, double x, double y) { |
90 |
super(geometryType, null, null); |
91 |
this.x = x;
|
92 |
this.y = y;
|
93 |
} |
94 |
|
95 |
public Point2D(int type, int subtype) { |
96 |
super(type, subtype);
|
97 |
this.x = 0.0d; |
98 |
this.y = 0.0d; |
99 |
} |
100 |
|
101 |
public Point2D(java.awt.geom.Point2D point) { |
102 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
103 |
this.x = point.getX();
|
104 |
this.y = point.getY();
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
|
109 |
* par?metro.
|
110 |
*
|
111 |
* @param at
|
112 |
* Matriz de transformaci?n.
|
113 |
*/
|
114 |
public void transform(AffineTransform at) { |
115 |
double[] coordinates = getCoordinates(); |
116 |
at.transform(coordinates, 0, coordinates, 0, 1); |
117 |
setCoordinates(coordinates); |
118 |
} |
119 |
|
120 |
public boolean contains(double x, double y) { |
121 |
if ((this.x == x) || (this.y == y)) { |
122 |
return true; |
123 |
} else {
|
124 |
return false; |
125 |
} |
126 |
} |
127 |
|
128 |
public boolean contains(double x, double y, double w, double h) { |
129 |
return false; |
130 |
} |
131 |
|
132 |
public boolean intersects(double x, double y, double w, double h) { |
133 |
Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h); |
134 |
|
135 |
return rAux.contains(this.x, this.y); |
136 |
} |
137 |
|
138 |
public Rectangle getBounds() { |
139 |
return new Rectangle((int) x, (int) y, 0, 0); |
140 |
} |
141 |
|
142 |
public double getX() { |
143 |
return x;
|
144 |
} |
145 |
|
146 |
public double getY() { |
147 |
return y;
|
148 |
} |
149 |
|
150 |
public boolean contains(java.awt.geom.Point2D p) { |
151 |
return false; |
152 |
} |
153 |
|
154 |
public Rectangle2D getBounds2D() { |
155 |
return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02); |
156 |
} |
157 |
|
158 |
public boolean contains(Rectangle2D r) { |
159 |
return false; |
160 |
} |
161 |
|
162 |
public boolean intersects(Rectangle2D r) { |
163 |
return r.contains(x, y);
|
164 |
} |
165 |
|
166 |
public Shape getShape(AffineTransform affineTransform) { |
167 |
return this; |
168 |
} |
169 |
|
170 |
public PathIterator getPathIterator(AffineTransform at) { |
171 |
return new PointIterator(getPoint2D(), at); |
172 |
} |
173 |
|
174 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
175 |
return new PointIterator(getPoint2D(), at); |
176 |
} |
177 |
|
178 |
private java.awt.geom.Point2D getPoint2D() {
|
179 |
return new java.awt.geom.Point2D.Double(x, y); |
180 |
} |
181 |
|
182 |
public int getShapeType() { |
183 |
return TYPES.POINT;
|
184 |
} |
185 |
|
186 |
public FShape cloneFShape() {
|
187 |
return new Point2D(getGeometryType(), id, projection, x, y); |
188 |
} |
189 |
|
190 |
public void reProject(ICoordTrans ct) { |
191 |
java.awt.geom.Point2D p = getPoint2D(); |
192 |
p = ct.convert(p, p); |
193 |
this.x = p.getX();
|
194 |
this.y = p.getY();
|
195 |
} |
196 |
|
197 |
public Handler[] getStretchingHandlers() { |
198 |
return new Handler[] { new PointHandler(0, x, y, this) }; |
199 |
} |
200 |
|
201 |
public Handler[] getSelectHandlers() { |
202 |
return new Handler[] { new PointHandler(0, x, y, this) }; |
203 |
} |
204 |
|
205 |
/**
|
206 |
*
|
207 |
* @author Vicente Caballero Navarro
|
208 |
* @author gvSIG team
|
209 |
*/
|
210 |
class PointHandler extends AbstractHandler implements FinalHandler { |
211 |
|
212 |
private final Point gvSIGPoint; |
213 |
|
214 |
public PointHandler(int i, double x, double y, Point gvSIGPoint) { |
215 |
this.gvSIGPoint = gvSIGPoint;
|
216 |
point = new java.awt.geom.Point2D.Double(x, y);
|
217 |
index = i; |
218 |
} |
219 |
|
220 |
public void move(double movex, double movey) { |
221 |
gvSIGPoint.setX(gvSIGPoint.getX() + movex); |
222 |
gvSIGPoint.setY(gvSIGPoint.getY() + movey); |
223 |
} |
224 |
|
225 |
public void set(double setx, double sety) { |
226 |
gvSIGPoint.setX(setx); |
227 |
gvSIGPoint.setY(sety); |
228 |
} |
229 |
} |
230 |
|
231 |
public int getDimension() { |
232 |
return 2; |
233 |
} |
234 |
|
235 |
public Envelope getEnvelope() {
|
236 |
return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02); |
237 |
} |
238 |
|
239 |
public GeneralPathX getGeneralPath() {
|
240 |
return null; |
241 |
} |
242 |
|
243 |
public DirectPosition getDirectPosition() {
|
244 |
return this; |
245 |
} |
246 |
|
247 |
public double getOrdinate(int dim) { |
248 |
if (dim == Geometry.DIMENSIONS.X) {
|
249 |
return getX();
|
250 |
} else
|
251 |
if (dim == Geometry.DIMENSIONS.Y) {
|
252 |
return getY();
|
253 |
} else {
|
254 |
return 0; |
255 |
} |
256 |
} |
257 |
|
258 |
public boolean equals(Object other) { |
259 |
if (!super.equals(other)) { |
260 |
return false; |
261 |
} |
262 |
Point2D pother = (Point2D) other; |
263 |
if (Math.abs(this.getX() - pother.getX()) > 0.0000001) { |
264 |
return false; |
265 |
} |
266 |
if (Math.abs(this.getY() - pother.getY()) > 0.0000001) { |
267 |
return false; |
268 |
} |
269 |
return true; |
270 |
|
271 |
} |
272 |
|
273 |
public double[] getCoordinates() { |
274 |
return new double[] { x, y }; |
275 |
} |
276 |
|
277 |
public double getCoordinateAt(int dimension) { |
278 |
switch (dimension) {
|
279 |
case Geometry.DIMENSIONS.X:
|
280 |
return x;
|
281 |
case Geometry.DIMENSIONS.Y:
|
282 |
return y;
|
283 |
default:
|
284 |
throw new ArrayIndexOutOfBoundsException(dimension); |
285 |
} |
286 |
} |
287 |
|
288 |
public void setCoordinateAt(int dimension, double value) { |
289 |
switch (dimension) {
|
290 |
case Geometry.DIMENSIONS.X:
|
291 |
this.x = value;
|
292 |
break;
|
293 |
case Geometry.DIMENSIONS.Y:
|
294 |
this.y = value;
|
295 |
break;
|
296 |
default:
|
297 |
throw new ArrayIndexOutOfBoundsException(dimension); |
298 |
} |
299 |
} |
300 |
|
301 |
public void setCoordinates(double[] values) { |
302 |
this.x = values[Geometry.DIMENSIONS.X];
|
303 |
this.y = values[Geometry.DIMENSIONS.Y];
|
304 |
} |
305 |
|
306 |
public void setX(double x) { |
307 |
this.x = x;
|
308 |
} |
309 |
|
310 |
public void setY(double y) { |
311 |
this.y = y;
|
312 |
} |
313 |
|
314 |
public String toString() { |
315 |
StringBuffer buffer = new StringBuffer(); |
316 |
double[] coordinates = getCoordinates(); |
317 |
buffer.append(getFullTypeName()).append("(").append(coordinates[0]); |
318 |
for (int i = 1; i < coordinates.length; i++) { |
319 |
buffer.append(",").append(coordinates[i]);
|
320 |
} |
321 |
buffer.append(")");
|
322 |
return buffer.toString();
|
323 |
} |
324 |
|
325 |
protected String getFullTypeName() { |
326 |
return "Point2D"; |
327 |
} |
328 |
|
329 |
public int getType() { |
330 |
return TYPES.POINT;
|
331 |
} |
332 |
} |