svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 35613
History | View | Annotate | Download (10.1 KB)
1 | 24207 | jiyarza | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 20761 | jmvivo | *
|
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | 24207 | jiyarza | * Av. Blasco Ib??ez, 50
|
24 | 20761 | jmvivo | * 46010 VALENCIA
|
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 27029 | jpiera | package org.gvsig.fmap.geom.primitive.impl; |
42 | 20761 | jmvivo | |
43 | import java.awt.Rectangle; |
||
44 | import java.awt.geom.AffineTransform; |
||
45 | import java.awt.geom.PathIterator; |
||
46 | import java.awt.geom.Rectangle2D; |
||
47 | import java.util.ArrayList; |
||
48 | |||
49 | 21870 | vcaballero | import org.cresques.cts.ICoordTrans; |
50 | import org.cresques.cts.IProjection; |
||
51 | 33650 | jpiera | |
52 | 24207 | jiyarza | import org.gvsig.fmap.geom.DirectPosition; |
53 | 29097 | jpiera | import org.gvsig.fmap.geom.Geometry; |
54 | 20761 | jmvivo | import org.gvsig.fmap.geom.handler.AbstractHandler; |
55 | import org.gvsig.fmap.geom.handler.FinalHandler; |
||
56 | import org.gvsig.fmap.geom.handler.Handler; |
||
57 | 27029 | jpiera | import org.gvsig.fmap.geom.primitive.Envelope; |
58 | import org.gvsig.fmap.geom.primitive.FShape; |
||
59 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
60 | import org.gvsig.fmap.geom.primitive.Point; |
||
61 | import org.gvsig.fmap.geom.primitive.PointIterator; |
||
62 | 20761 | jmvivo | import org.gvsig.fmap.geom.type.GeometryType; |
63 | |||
64 | /**
|
||
65 | * Punto 2D.
|
||
66 | 21425 | vcaballero | *
|
67 | 20761 | jmvivo | * @author Vicente Caballero Navarro
|
68 | */
|
||
69 | 24207 | jiyarza | public class Point2D extends AbstractPrimitive implements Point, DirectPosition { |
70 | 33388 | jpiera | |
71 | protected static final String COORDINATES_FIELD = "coordinates"; |
||
72 | |||
73 | 29097 | jpiera | private static final long serialVersionUID = 1836257305083881299L; |
74 | 26788 | jpiera | protected double[] coordinates; |
75 | 29097 | jpiera | |
76 | 20761 | jmvivo | /**
|
77 | 29097 | jpiera | * The constructor with the GeometryType like and argument
|
78 | * is used by the {@link GeometryType}{@link #create()}
|
||
79 | * to create the geometry
|
||
80 | * @param type
|
||
81 | * The geometry type
|
||
82 | 20761 | jmvivo | */
|
83 | 29097 | jpiera | public Point2D(GeometryType geometryType) { |
84 | this(geometryType, null, null, 0.0, 0.0); |
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Constructor used in the {@link Geometry#cloneGeometry()} method
|
||
89 | */
|
||
90 | Point2D(GeometryType geometryType, String id, IProjection projection, double x, double y) { |
||
91 | super(geometryType, id, projection);
|
||
92 | 26788 | jpiera | coordinates = new double[getDimension()]; |
93 | coordinates[0] = x;
|
||
94 | coordinates[1] = y;
|
||
95 | 20761 | jmvivo | } |
96 | 29097 | jpiera | |
97 | public Point2D (double x, double y) { |
||
98 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
99 | 26788 | jpiera | coordinates = new double[getDimension()]; |
100 | 29097 | jpiera | coordinates[0] = x;
|
101 | coordinates[1] = y;
|
||
102 | 20761 | jmvivo | } |
103 | 29097 | jpiera | |
104 | 35613 | jpiera | public Point2D (int type, int subtype) { |
105 | super(type, subtype);
|
||
106 | coordinates = new double[getDimension()]; |
||
107 | } |
||
108 | |||
109 | 29097 | jpiera | public Point2D (java.awt.geom.Point2D point) { |
110 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
111 | coordinates = new double[getDimension()]; |
||
112 | coordinates[0] = point.getX();
|
||
113 | coordinates[1] = point.getY();
|
||
114 | 20761 | jmvivo | } |
115 | |||
116 | 26788 | jpiera | /**
|
117 | * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
|
||
118 | * par?metro.
|
||
119 | 21425 | vcaballero | *
|
120 | 20761 | jmvivo | * @param at
|
121 | 26788 | jpiera | * Matriz de transformaci?n.
|
122 | 20761 | jmvivo | */
|
123 | public void transform(AffineTransform at) { |
||
124 | 26788 | jpiera | at.transform(coordinates, 0, coordinates, 0, 1); |
125 | 20761 | jmvivo | } |
126 | |||
127 | /*
|
||
128 | * (non-Javadoc)
|
||
129 | 21425 | vcaballero | *
|
130 | 20761 | jmvivo | * @see java.awt.Shape#contains(double, double)
|
131 | */
|
||
132 | public boolean contains(double x, double y) { |
||
133 | 26788 | jpiera | if ((x == coordinates[0]) || (y == coordinates[1])) { |
134 | 20761 | jmvivo | return true; |
135 | } else {
|
||
136 | return false; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /*
|
||
141 | * (non-Javadoc)
|
||
142 | 21425 | vcaballero | *
|
143 | 20761 | jmvivo | * @see java.awt.Shape#contains(double, double, double, double)
|
144 | */
|
||
145 | public boolean contains(double x, double y, double w, double h) { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | /*
|
||
150 | * (non-Javadoc)
|
||
151 | 21425 | vcaballero | *
|
152 | 20761 | jmvivo | * @see java.awt.Shape#intersects(double, double, double, double)
|
153 | */
|
||
154 | public boolean intersects(double x, double y, double w, double h) { |
||
155 | Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h); |
||
156 | |||
157 | 26788 | jpiera | return rAux.contains(coordinates[0], coordinates[1]); |
158 | 20761 | jmvivo | } |
159 | |||
160 | /*
|
||
161 | * (non-Javadoc)
|
||
162 | 21425 | vcaballero | *
|
163 | 20761 | jmvivo | * @see java.awt.Shape#getBounds()
|
164 | */
|
||
165 | public Rectangle getBounds() { |
||
166 | 26788 | jpiera | return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0); |
167 | 20761 | jmvivo | } |
168 | |||
169 | /**
|
||
170 | * Devuelve la coordenada x del punto.
|
||
171 | 21425 | vcaballero | *
|
172 | 20761 | jmvivo | * @return Coordenada x.
|
173 | */
|
||
174 | public double getX() { |
||
175 | 26788 | jpiera | return coordinates[0]; |
176 | 20761 | jmvivo | } |
177 | |||
178 | /**
|
||
179 | * Devuelve la coordenada y del punto.
|
||
180 | 21425 | vcaballero | *
|
181 | 20761 | jmvivo | * @return Coordenada y.
|
182 | */
|
||
183 | public double getY() { |
||
184 | 26788 | jpiera | return coordinates[1]; |
185 | 20761 | jmvivo | } |
186 | |||
187 | /*
|
||
188 | * (non-Javadoc)
|
||
189 | 21425 | vcaballero | *
|
190 | 20761 | jmvivo | * @see java.awt.Shape#contains(java.awt.geom.Point2D)
|
191 | */
|
||
192 | public boolean contains(java.awt.geom.Point2D p) { |
||
193 | return false; |
||
194 | } |
||
195 | |||
196 | /*
|
||
197 | * (non-Javadoc)
|
||
198 | 21425 | vcaballero | *
|
199 | 20761 | jmvivo | * @see java.awt.Shape#getBounds2D()
|
200 | */
|
||
201 | public Rectangle2D getBounds2D() { |
||
202 | 26788 | jpiera | return new Rectangle2D.Double(coordinates[0] - 0.01, coordinates[1] - 0.01, 0.02, |
203 | 20761 | jmvivo | 0.02);
|
204 | } |
||
205 | |||
206 | /*
|
||
207 | * (non-Javadoc)
|
||
208 | 21425 | vcaballero | *
|
209 | 20761 | jmvivo | * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
|
210 | */
|
||
211 | public boolean contains(Rectangle2D r) { |
||
212 | return false; |
||
213 | } |
||
214 | |||
215 | /*
|
||
216 | * (non-Javadoc)
|
||
217 | 21425 | vcaballero | *
|
218 | 20761 | jmvivo | * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
|
219 | */
|
||
220 | public boolean intersects(Rectangle2D r) { |
||
221 | 26788 | jpiera | return r.contains(coordinates[0], coordinates[1]); |
222 | 20761 | jmvivo | } |
223 | |||
224 | /*
|
||
225 | * (non-Javadoc)
|
||
226 | 21425 | vcaballero | *
|
227 | 20761 | jmvivo | * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
|
228 | */
|
||
229 | public PathIterator getPathIterator(AffineTransform at) { |
||
230 | 26788 | jpiera | return new PointIterator(getPoint2D(), at); |
231 | 20761 | jmvivo | } |
232 | |||
233 | /*
|
||
234 | * (non-Javadoc)
|
||
235 | 21425 | vcaballero | *
|
236 | 20761 | jmvivo | * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
|
237 | * double)
|
||
238 | */
|
||
239 | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
||
240 | 26788 | jpiera | return new PointIterator(getPoint2D(), at); |
241 | 20761 | jmvivo | } |
242 | |||
243 | 26788 | jpiera | private java.awt.geom.Point2D getPoint2D() {
|
244 | return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]); |
||
245 | } |
||
246 | 28089 | vcaballero | |
247 | 20761 | jmvivo | /**
|
248 | * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
|
||
249 | */
|
||
250 | public int getShapeType() { |
||
251 | 28996 | jpiera | return TYPES.POINT;
|
252 | 20761 | jmvivo | } |
253 | 21425 | vcaballero | |
254 | 20761 | jmvivo | /*
|
255 | * (non-Javadoc)
|
||
256 | 21425 | vcaballero | *
|
257 | 20761 | jmvivo | * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
|
258 | */
|
||
259 | public FShape cloneFShape() {
|
||
260 | 29097 | jpiera | return new Point2D(getGeometryType(), id, projection, coordinates[0], coordinates[1]); |
261 | 20761 | jmvivo | } |
262 | |||
263 | /*
|
||
264 | * (non-Javadoc)
|
||
265 | 21425 | vcaballero | *
|
266 | 20761 | jmvivo | * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
|
267 | */
|
||
268 | public void reProject(ICoordTrans ct) { |
||
269 | 26788 | jpiera | java.awt.geom.Point2D p = getPoint2D(); |
270 | 20761 | jmvivo | p = ct.convert(p, p); |
271 | 26788 | jpiera | coordinates[0] = p.getX();
|
272 | coordinates[1] = p.getY();
|
||
273 | 20761 | jmvivo | } |
274 | |||
275 | /*
|
||
276 | * (non-Javadoc)
|
||
277 | 21425 | vcaballero | *
|
278 | 20761 | jmvivo | * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
|
279 | */
|
||
280 | public Handler[] getStretchingHandlers() { |
||
281 | ArrayList handlers = new ArrayList(); |
||
282 | 26788 | jpiera | handlers.add(new PointHandler(0, coordinates[0], coordinates[1])); |
283 | 20761 | jmvivo | return (Handler[]) handlers.toArray(new Handler[0]); |
284 | } |
||
285 | |||
286 | /*
|
||
287 | * (non-Javadoc)
|
||
288 | 21425 | vcaballero | *
|
289 | 20761 | jmvivo | * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
|
290 | */
|
||
291 | public Handler[] getSelectHandlers() { |
||
292 | ArrayList handlers = new ArrayList(); |
||
293 | 26788 | jpiera | handlers.add(new PointHandler(0, coordinates[0], coordinates[1])); |
294 | 20761 | jmvivo | return (Handler[]) handlers.toArray(new Handler[0]); |
295 | } |
||
296 | |||
297 | /**
|
||
298 | * DOCUMENT ME!
|
||
299 | 21425 | vcaballero | *
|
300 | 20761 | jmvivo | * @author Vicente Caballero Navarro
|
301 | */
|
||
302 | class PointHandler extends AbstractHandler implements FinalHandler { |
||
303 | /**
|
||
304 | * Crea un nuevo PointHandler.
|
||
305 | 21425 | vcaballero | *
|
306 | 20761 | jmvivo | * @param x
|
307 | * DOCUMENT ME!
|
||
308 | * @param y
|
||
309 | * DOCUMENT ME!
|
||
310 | */
|
||
311 | public PointHandler(int i, double x, double y) { |
||
312 | point = new java.awt.geom.Point2D.Double(x, y);
|
||
313 | index = i; |
||
314 | } |
||
315 | |||
316 | /**
|
||
317 | * DOCUMENT ME!
|
||
318 | 21425 | vcaballero | *
|
319 | 20761 | jmvivo | * @param x
|
320 | * DOCUMENT ME!
|
||
321 | * @param y
|
||
322 | * DOCUMENT ME!
|
||
323 | 21425 | vcaballero | *
|
324 | 20761 | jmvivo | * @return DOCUMENT ME!
|
325 | */
|
||
326 | public void move(double x, double y) { |
||
327 | 26788 | jpiera | coordinates[0] = coordinates[0] + x; |
328 | coordinates[1] = coordinates[1] + y; |
||
329 | 20761 | jmvivo | } |
330 | |||
331 | /**
|
||
332 | * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
||
333 | */
|
||
334 | public void set(double x, double y) { |
||
335 | 26788 | jpiera | coordinates[0] = x;
|
336 | coordinates[1] = y;
|
||
337 | 20761 | jmvivo | } |
338 | } |
||
339 | |||
340 | /*
|
||
341 | * (non-Javadoc)
|
||
342 | 21425 | vcaballero | *
|
343 | 20761 | jmvivo | * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
|
344 | */
|
||
345 | 26788 | jpiera | public int getDimension() { |
346 | 20761 | jmvivo | return 2; |
347 | } |
||
348 | |||
349 | 21425 | vcaballero | public Envelope getEnvelope() {
|
350 | 27019 | jpiera | return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,coordinates[0]+0.02,coordinates[1]+ |
351 | 0.02);
|
||
352 | 21425 | vcaballero | } |
353 | |||
354 | 21382 | csanchez | public GeneralPathX getGeneralPath() {
|
355 | // TODO Auto-generated method stub
|
||
356 | return null; |
||
357 | } |
||
358 | 26289 | jmvivo | |
359 | 24207 | jiyarza | public DirectPosition getDirectPosition() {
|
360 | return this; |
||
361 | } |
||
362 | 26289 | jmvivo | |
363 | 24207 | jiyarza | public double getOrdinate(int dim) { |
364 | 26289 | jmvivo | if (dim == 0) { |
365 | return getX();
|
||
366 | } else if (dim == 1) { |
||
367 | return getY();
|
||
368 | } else {
|
||
369 | return 0; |
||
370 | } |
||
371 | 24207 | jiyarza | } |
372 | 26289 | jmvivo | |
373 | public boolean equals(Object other) { |
||
374 | if (!super.equals(other)) { |
||
375 | return false; |
||
376 | } |
||
377 | Point2D pother = (Point2D) other; |
||
378 | if (Math.abs(this.getX() - pother.getX()) > 0.0000001) { |
||
379 | return false; |
||
380 | } |
||
381 | if (Math.abs(this.getY() - pother.getY()) > 0.0000001) { |
||
382 | return false; |
||
383 | } |
||
384 | return true; |
||
385 | |||
386 | } |
||
387 | 26788 | jpiera | |
388 | /* (non-Javadoc)
|
||
389 | * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
|
||
390 | */
|
||
391 | public double[] getCoordinates() { |
||
392 | return coordinates;
|
||
393 | } |
||
394 | |||
395 | /* (non-Javadoc)
|
||
396 | * @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
|
||
397 | */
|
||
398 | public double getCoordinateAt(int dimension) { |
||
399 | return coordinates[dimension];
|
||
400 | } |
||
401 | |||
402 | /* (non-Javadoc)
|
||
403 | * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
|
||
404 | */
|
||
405 | public void setCoordinateAt(int dimension, double value) { |
||
406 | 28089 | vcaballero | coordinates[dimension] = value; |
407 | 26788 | jpiera | } |
408 | |||
409 | /* (non-Javadoc)
|
||
410 | * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
|
||
411 | */
|
||
412 | public void setCoordinates(double[] values) { |
||
413 | 28089 | vcaballero | coordinates = values; |
414 | 26788 | jpiera | } |
415 | |||
416 | /* (non-Javadoc)
|
||
417 | * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
|
||
418 | */
|
||
419 | public void setX(double x) { |
||
420 | 28089 | vcaballero | coordinates[0] = x;
|
421 | 26788 | jpiera | } |
422 | |||
423 | /* (non-Javadoc)
|
||
424 | * @see org.gvsig.fmap.geom.primitive.Point#setY(double)
|
||
425 | */
|
||
426 | public void setY(double y) { |
||
427 | 28089 | vcaballero | coordinates[1] = y;
|
428 | 26788 | jpiera | } |
429 | |||
430 | 28625 | jmvivo | public String toString() { |
431 | 32934 | jjdelcerro | StringBuffer buffer = new StringBuffer(); |
432 | buffer.append("Point2D(").append(coordinates[0]); |
||
433 | for(int i=1 ; i<coordinates.length; i++ ) { |
||
434 | buffer.append(",").append(coordinates[i]);
|
||
435 | } |
||
436 | buffer.append(")");
|
||
437 | return buffer.toString();
|
||
438 | 33650 | jpiera | } |
439 | 24207 | jiyarza | } |