svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 36221
History | View | Annotate | Download (11.4 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.geom.AffineTransform; |
26 |
import java.awt.geom.PathIterator; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
import java.util.ArrayList; |
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 |
* Punto 2D.
|
47 |
*
|
48 |
* @author Vicente Caballero Navarro
|
49 |
*/
|
50 |
public class Point2D extends AbstractPrimitive implements Point, DirectPosition { |
51 |
|
52 |
protected static final String COORDINATES_FIELD = "coordinates"; |
53 |
|
54 |
private static final long serialVersionUID = 1836257305083881299L; |
55 |
protected double[] coordinates; |
56 |
|
57 |
/**
|
58 |
* The constructor with the GeometryType like and argument
|
59 |
* is used by the {@link GeometryType}{@link #create()} to create the
|
60 |
* geometry
|
61 |
*
|
62 |
* @param type
|
63 |
* The geometry type
|
64 |
*/
|
65 |
public Point2D(GeometryType geometryType) { |
66 |
this(geometryType, null, null, 0.0, 0.0); |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Constructor used in the {@link Geometry#cloneGeometry()} method
|
71 |
*/
|
72 |
Point2D(GeometryType geometryType, String id, IProjection projection, |
73 |
double x, double y) { |
74 |
super(geometryType, id, projection);
|
75 |
coordinates = new double[getDimension()]; |
76 |
coordinates[0] = x;
|
77 |
coordinates[1] = y;
|
78 |
} |
79 |
|
80 |
public Point2D(double x, double y) { |
81 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
82 |
coordinates = new double[getDimension()]; |
83 |
coordinates[0] = x;
|
84 |
coordinates[1] = y;
|
85 |
} |
86 |
|
87 |
public Point2D(int type, int subtype) { |
88 |
super(type, subtype);
|
89 |
coordinates = new double[getDimension()]; |
90 |
} |
91 |
|
92 |
public Point2D(java.awt.geom.Point2D point) { |
93 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
94 |
coordinates = new double[getDimension()]; |
95 |
coordinates[0] = point.getX();
|
96 |
coordinates[1] = point.getY();
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
|
101 |
* par?metro.
|
102 |
*
|
103 |
* @param at
|
104 |
* Matriz de transformaci?n.
|
105 |
*/
|
106 |
public void transform(AffineTransform at) { |
107 |
at.transform(coordinates, 0, coordinates, 0, 1); |
108 |
} |
109 |
|
110 |
/*
|
111 |
* (non-Javadoc)
|
112 |
*
|
113 |
* @see java.awt.Shape#contains(double, double)
|
114 |
*/
|
115 |
public boolean contains(double x, double y) { |
116 |
if ((x == coordinates[0]) || (y == coordinates[1])) { |
117 |
return true; |
118 |
} else {
|
119 |
return false; |
120 |
} |
121 |
} |
122 |
|
123 |
/*
|
124 |
* (non-Javadoc)
|
125 |
*
|
126 |
* @see java.awt.Shape#contains(double, double, double, double)
|
127 |
*/
|
128 |
public boolean contains(double x, double y, double w, double h) { |
129 |
return false; |
130 |
} |
131 |
|
132 |
/*
|
133 |
* (non-Javadoc)
|
134 |
*
|
135 |
* @see java.awt.Shape#intersects(double, double, double, double)
|
136 |
*/
|
137 |
public boolean intersects(double x, double y, double w, double h) { |
138 |
Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h); |
139 |
|
140 |
return rAux.contains(coordinates[0], coordinates[1]); |
141 |
} |
142 |
|
143 |
/*
|
144 |
* (non-Javadoc)
|
145 |
*
|
146 |
* @see java.awt.Shape#getBounds()
|
147 |
*/
|
148 |
public Rectangle getBounds() { |
149 |
return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0); |
150 |
} |
151 |
|
152 |
/**
|
153 |
* Devuelve la coordenada x del punto.
|
154 |
*
|
155 |
* @return Coordenada x.
|
156 |
*/
|
157 |
public double getX() { |
158 |
return coordinates[0]; |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Devuelve la coordenada y del punto.
|
163 |
*
|
164 |
* @return Coordenada y.
|
165 |
*/
|
166 |
public double getY() { |
167 |
return coordinates[1]; |
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
*
|
173 |
* @see java.awt.Shape#contains(java.awt.geom.Point2D)
|
174 |
*/
|
175 |
public boolean contains(java.awt.geom.Point2D p) { |
176 |
return false; |
177 |
} |
178 |
|
179 |
/*
|
180 |
* (non-Javadoc)
|
181 |
*
|
182 |
* @see java.awt.Shape#getBounds2D()
|
183 |
*/
|
184 |
public Rectangle2D getBounds2D() { |
185 |
return new Rectangle2D.Double(coordinates[0] - 0.01, |
186 |
coordinates[1] - 0.01, 0.02, 0.02); |
187 |
} |
188 |
|
189 |
/*
|
190 |
* (non-Javadoc)
|
191 |
*
|
192 |
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
|
193 |
*/
|
194 |
public boolean contains(Rectangle2D r) { |
195 |
return false; |
196 |
} |
197 |
|
198 |
/*
|
199 |
* (non-Javadoc)
|
200 |
*
|
201 |
* @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
|
202 |
*/
|
203 |
public boolean intersects(Rectangle2D r) { |
204 |
return r.contains(coordinates[0], coordinates[1]); |
205 |
} |
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
*
|
210 |
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
|
211 |
*/
|
212 |
public PathIterator getPathIterator(AffineTransform at) { |
213 |
return new PointIterator(getPoint2D(), at); |
214 |
} |
215 |
|
216 |
/*
|
217 |
* (non-Javadoc)
|
218 |
*
|
219 |
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
|
220 |
* double)
|
221 |
*/
|
222 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
223 |
return new PointIterator(getPoint2D(), at); |
224 |
} |
225 |
|
226 |
private java.awt.geom.Point2D getPoint2D() {
|
227 |
return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]); |
228 |
} |
229 |
|
230 |
/**
|
231 |
* @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
|
232 |
*/
|
233 |
public int getShapeType() { |
234 |
return TYPES.POINT;
|
235 |
} |
236 |
|
237 |
/*
|
238 |
* (non-Javadoc)
|
239 |
*
|
240 |
* @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
|
241 |
*/
|
242 |
public FShape cloneFShape() {
|
243 |
return new Point2D(getGeometryType(), id, projection, coordinates[0], |
244 |
coordinates[1]);
|
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
*
|
250 |
* @see
|
251 |
* com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans
|
252 |
* )
|
253 |
*/
|
254 |
public void reProject(ICoordTrans ct) { |
255 |
java.awt.geom.Point2D p = getPoint2D(); |
256 |
p = ct.convert(p, p); |
257 |
coordinates[0] = p.getX();
|
258 |
coordinates[1] = p.getY();
|
259 |
} |
260 |
|
261 |
/*
|
262 |
* (non-Javadoc)
|
263 |
*
|
264 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
|
265 |
*/
|
266 |
public Handler[] getStretchingHandlers() { |
267 |
ArrayList handlers = new ArrayList(); |
268 |
handlers.add(new PointHandler(0, coordinates[0], coordinates[1])); |
269 |
return (Handler[]) handlers.toArray(new Handler[0]); |
270 |
} |
271 |
|
272 |
/*
|
273 |
* (non-Javadoc)
|
274 |
*
|
275 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
|
276 |
*/
|
277 |
public Handler[] getSelectHandlers() { |
278 |
ArrayList handlers = new ArrayList(); |
279 |
handlers.add(new PointHandler(0, coordinates[0], coordinates[1])); |
280 |
return (Handler[]) handlers.toArray(new Handler[0]); |
281 |
} |
282 |
|
283 |
/**
|
284 |
* DOCUMENT ME!
|
285 |
*
|
286 |
* @author Vicente Caballero Navarro
|
287 |
*/
|
288 |
class PointHandler extends AbstractHandler implements FinalHandler { |
289 |
|
290 |
/**
|
291 |
* Crea un nuevo PointHandler.
|
292 |
*
|
293 |
* @param x
|
294 |
* DOCUMENT ME!
|
295 |
* @param y
|
296 |
* DOCUMENT ME!
|
297 |
*/
|
298 |
public PointHandler(int i, double x, double y) { |
299 |
point = new java.awt.geom.Point2D.Double(x, y);
|
300 |
index = i; |
301 |
} |
302 |
|
303 |
/**
|
304 |
* DOCUMENT ME!
|
305 |
*
|
306 |
* @param x
|
307 |
* DOCUMENT ME!
|
308 |
* @param y
|
309 |
* DOCUMENT ME!
|
310 |
*
|
311 |
* @return DOCUMENT ME!
|
312 |
*/
|
313 |
public void move(double x, double y) { |
314 |
coordinates[0] = coordinates[0] + x; |
315 |
coordinates[1] = coordinates[1] + y; |
316 |
} |
317 |
|
318 |
/**
|
319 |
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
320 |
*/
|
321 |
public void set(double x, double y) { |
322 |
coordinates[0] = x;
|
323 |
coordinates[1] = y;
|
324 |
} |
325 |
} |
326 |
|
327 |
/*
|
328 |
* (non-Javadoc)
|
329 |
*
|
330 |
* @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
|
331 |
*/
|
332 |
public int getDimension() { |
333 |
return 2; |
334 |
} |
335 |
|
336 |
public Envelope getEnvelope() {
|
337 |
return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01, |
338 |
coordinates[0] + 0.02, coordinates[1] + 0.02); |
339 |
} |
340 |
|
341 |
public GeneralPathX getGeneralPath() {
|
342 |
// TODO Auto-generated method stub
|
343 |
return null; |
344 |
} |
345 |
|
346 |
public DirectPosition getDirectPosition() {
|
347 |
return this; |
348 |
} |
349 |
|
350 |
public double getOrdinate(int dim) { |
351 |
if (dim == 0) { |
352 |
return getX();
|
353 |
} else
|
354 |
if (dim == 1) { |
355 |
return getY();
|
356 |
} else {
|
357 |
return 0; |
358 |
} |
359 |
} |
360 |
|
361 |
public boolean equals(Object other) { |
362 |
if (!super.equals(other)) { |
363 |
return false; |
364 |
} |
365 |
Point2D pother = (Point2D) other; |
366 |
if (Math.abs(this.getX() - pother.getX()) > 0.0000001) { |
367 |
return false; |
368 |
} |
369 |
if (Math.abs(this.getY() - pother.getY()) > 0.0000001) { |
370 |
return false; |
371 |
} |
372 |
return true; |
373 |
|
374 |
} |
375 |
|
376 |
/*
|
377 |
* (non-Javadoc)
|
378 |
*
|
379 |
* @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
|
380 |
*/
|
381 |
public double[] getCoordinates() { |
382 |
return coordinates;
|
383 |
} |
384 |
|
385 |
/*
|
386 |
* (non-Javadoc)
|
387 |
*
|
388 |
* @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
|
389 |
*/
|
390 |
public double getCoordinateAt(int dimension) { |
391 |
return coordinates[dimension];
|
392 |
} |
393 |
|
394 |
/*
|
395 |
* (non-Javadoc)
|
396 |
*
|
397 |
* @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
|
398 |
*/
|
399 |
public void setCoordinateAt(int dimension, double value) { |
400 |
coordinates[dimension] = value; |
401 |
} |
402 |
|
403 |
/*
|
404 |
* (non-Javadoc)
|
405 |
*
|
406 |
* @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
|
407 |
*/
|
408 |
public void setCoordinates(double[] values) { |
409 |
coordinates = values; |
410 |
} |
411 |
|
412 |
/*
|
413 |
* (non-Javadoc)
|
414 |
*
|
415 |
* @see org.gvsig.fmap.geom.primitive.Point#setX(double)
|
416 |
*/
|
417 |
public void setX(double x) { |
418 |
coordinates[0] = x;
|
419 |
} |
420 |
|
421 |
/*
|
422 |
* (non-Javadoc)
|
423 |
*
|
424 |
* @see org.gvsig.fmap.geom.primitive.Point#setY(double)
|
425 |
*/
|
426 |
public void setY(double y) { |
427 |
coordinates[1] = y;
|
428 |
} |
429 |
|
430 |
public String toString() { |
431 |
StringBuffer buffer = new StringBuffer(); |
432 |
buffer.append("Point2D(");
|
433 |
toStringCoordinates(buffer); |
434 |
buffer.append(")");
|
435 |
return buffer.toString();
|
436 |
} |
437 |
|
438 |
protected void toStringCoordinates(StringBuffer buffer) { |
439 |
buffer.append(coordinates[0]);
|
440 |
for (int i = 1; i < coordinates.length; i++) { |
441 |
buffer.append(",").append(coordinates[i]);
|
442 |
} |
443 |
} |
444 |
} |