Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 34118

History | View | Annotate | Download (10 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
        public Point2D (java.awt.geom.Point2D point) {
105
                super(TYPES.POINT, SUBTYPES.GEOM2D);
106
                coordinates = new double[getDimension()];
107
                coordinates[0] = point.getX();
108
                coordinates[1] = point.getY();
109 20761 jmvivo
        }
110
111 26788 jpiera
        /**
112
         * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
113
         * par?metro.
114 21425 vcaballero
         *
115 20761 jmvivo
         * @param at
116 26788 jpiera
         *            Matriz de transformaci?n.
117 20761 jmvivo
         */
118
        public void transform(AffineTransform at) {
119 26788 jpiera
                at.transform(coordinates, 0, coordinates, 0, 1);
120 20761 jmvivo
        }
121
122
        /*
123
         * (non-Javadoc)
124 21425 vcaballero
         *
125 20761 jmvivo
         * @see java.awt.Shape#contains(double, double)
126
         */
127
        public boolean contains(double x, double y) {
128 26788 jpiera
                if ((x == coordinates[0]) || (y == coordinates[1])) {
129 20761 jmvivo
                        return true;
130
                } else {
131
                        return false;
132
                }
133
        }
134
135
        /*
136
         * (non-Javadoc)
137 21425 vcaballero
         *
138 20761 jmvivo
         * @see java.awt.Shape#contains(double, double, double, double)
139
         */
140
        public boolean contains(double x, double y, double w, double h) {
141
                return false;
142
        }
143
144
        /*
145
         * (non-Javadoc)
146 21425 vcaballero
         *
147 20761 jmvivo
         * @see java.awt.Shape#intersects(double, double, double, double)
148
         */
149
        public boolean intersects(double x, double y, double w, double h) {
150
                Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
151
152 26788 jpiera
                return rAux.contains(coordinates[0], coordinates[1]);
153 20761 jmvivo
        }
154
155
        /*
156
         * (non-Javadoc)
157 21425 vcaballero
         *
158 20761 jmvivo
         * @see java.awt.Shape#getBounds()
159
         */
160
        public Rectangle getBounds() {
161 26788 jpiera
                return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0);
162 20761 jmvivo
        }
163
164
        /**
165
         * Devuelve la coordenada x del punto.
166 21425 vcaballero
         *
167 20761 jmvivo
         * @return Coordenada x.
168
         */
169
        public double getX() {
170 26788 jpiera
                return coordinates[0];
171 20761 jmvivo
        }
172
173
        /**
174
         * Devuelve la coordenada y del punto.
175 21425 vcaballero
         *
176 20761 jmvivo
         * @return Coordenada y.
177
         */
178
        public double getY() {
179 26788 jpiera
                return coordinates[1];
180 20761 jmvivo
        }
181
182
        /*
183
         * (non-Javadoc)
184 21425 vcaballero
         *
185 20761 jmvivo
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
186
         */
187
        public boolean contains(java.awt.geom.Point2D p) {
188
                return false;
189
        }
190
191
        /*
192
         * (non-Javadoc)
193 21425 vcaballero
         *
194 20761 jmvivo
         * @see java.awt.Shape#getBounds2D()
195
         */
196
        public Rectangle2D getBounds2D() {
197 26788 jpiera
                return new Rectangle2D.Double(coordinates[0] - 0.01, coordinates[1] - 0.01, 0.02,
198 20761 jmvivo
                                0.02);
199
        }
200
201
        /*
202
         * (non-Javadoc)
203 21425 vcaballero
         *
204 20761 jmvivo
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
205
         */
206
        public boolean contains(Rectangle2D r) {
207
                return false;
208
        }
209
210
        /*
211
         * (non-Javadoc)
212 21425 vcaballero
         *
213 20761 jmvivo
         * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
214
         */
215
        public boolean intersects(Rectangle2D r) {
216 26788 jpiera
                return r.contains(coordinates[0], coordinates[1]);
217 20761 jmvivo
        }
218
219
        /*
220
         * (non-Javadoc)
221 21425 vcaballero
         *
222 20761 jmvivo
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
223
         */
224
        public PathIterator getPathIterator(AffineTransform at) {
225 26788 jpiera
                return new PointIterator(getPoint2D(), at);
226 20761 jmvivo
        }
227
228
        /*
229
         * (non-Javadoc)
230 21425 vcaballero
         *
231 20761 jmvivo
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
232
         *      double)
233
         */
234
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
235 26788 jpiera
                return new PointIterator(getPoint2D(), at);
236 20761 jmvivo
        }
237
238 26788 jpiera
        private java.awt.geom.Point2D getPoint2D()  {
239
                return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]);
240
        }
241 28089 vcaballero
242 20761 jmvivo
        /**
243
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
244
         */
245
        public int getShapeType() {
246 28996 jpiera
                return TYPES.POINT;
247 20761 jmvivo
        }
248 21425 vcaballero
249 20761 jmvivo
        /*
250
         * (non-Javadoc)
251 21425 vcaballero
         *
252 20761 jmvivo
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
253
         */
254
        public FShape cloneFShape() {
255 29097 jpiera
                return new Point2D(getGeometryType(), id, projection, coordinates[0], coordinates[1]);
256 20761 jmvivo
        }
257
258
        /*
259
         * (non-Javadoc)
260 21425 vcaballero
         *
261 20761 jmvivo
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
262
         */
263
        public void reProject(ICoordTrans ct) {
264 26788 jpiera
                java.awt.geom.Point2D p = getPoint2D();
265 20761 jmvivo
                p = ct.convert(p, p);
266 26788 jpiera
                coordinates[0] = p.getX();
267
                coordinates[1] = p.getY();
268 20761 jmvivo
        }
269
270
        /*
271
         * (non-Javadoc)
272 21425 vcaballero
         *
273 20761 jmvivo
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
274
         */
275
        public Handler[] getStretchingHandlers() {
276
                ArrayList handlers = new ArrayList();
277 26788 jpiera
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
278 20761 jmvivo
                return (Handler[]) handlers.toArray(new Handler[0]);
279
        }
280
281
        /*
282
         * (non-Javadoc)
283 21425 vcaballero
         *
284 20761 jmvivo
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
285
         */
286
        public Handler[] getSelectHandlers() {
287
                ArrayList handlers = new ArrayList();
288 26788 jpiera
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
289 20761 jmvivo
                return (Handler[]) handlers.toArray(new Handler[0]);
290
        }
291
292
        /**
293
         * DOCUMENT ME!
294 21425 vcaballero
         *
295 20761 jmvivo
         * @author Vicente Caballero Navarro
296
         */
297
        class PointHandler extends AbstractHandler implements FinalHandler {
298
                /**
299
                 * Crea un nuevo PointHandler.
300 21425 vcaballero
                 *
301 20761 jmvivo
                 * @param x
302
                 *            DOCUMENT ME!
303
                 * @param y
304
                 *            DOCUMENT ME!
305
                 */
306
                public PointHandler(int i, double x, double y) {
307
                        point = new java.awt.geom.Point2D.Double(x, y);
308
                        index = i;
309
                }
310
311
                /**
312
                 * DOCUMENT ME!
313 21425 vcaballero
                 *
314 20761 jmvivo
                 * @param x
315
                 *            DOCUMENT ME!
316
                 * @param y
317
                 *            DOCUMENT ME!
318 21425 vcaballero
                 *
319 20761 jmvivo
                 * @return DOCUMENT ME!
320
                 */
321
                public void move(double x, double y) {
322 26788 jpiera
                        coordinates[0] = coordinates[0] + x;
323
                        coordinates[1] = coordinates[1] + y;
324 20761 jmvivo
                }
325
326
                /**
327
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
328
                 */
329
                public void set(double x, double y) {
330 26788 jpiera
                        coordinates[0] = x;
331
                        coordinates[1] = y;
332 20761 jmvivo
                }
333
        }
334
335
        /*
336
         * (non-Javadoc)
337 21425 vcaballero
         *
338 20761 jmvivo
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
339
         */
340 26788 jpiera
        public int getDimension() {
341 20761 jmvivo
                return 2;
342
        }
343
344 21425 vcaballero
        public Envelope getEnvelope() {
345 27019 jpiera
                return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,coordinates[0]+0.02,coordinates[1]+
346
                                0.02);
347 21425 vcaballero
        }
348
349 21382 csanchez
        public GeneralPathX getGeneralPath() {
350
                // TODO Auto-generated method stub
351
                return null;
352
        }
353 26289 jmvivo
354 24207 jiyarza
        public DirectPosition getDirectPosition() {
355
                return this;
356
        }
357 26289 jmvivo
358 24207 jiyarza
        public double getOrdinate(int dim) {
359 26289 jmvivo
                if (dim == 0) {
360
                        return getX();
361
                } else if (dim == 1) {
362
                        return getY();
363
                } else {
364
                        return 0;
365
                }
366 24207 jiyarza
        }
367 26289 jmvivo
368
        public boolean equals(Object other) {
369
                if (!super.equals(other)) {
370
                        return false;
371
                }
372
                Point2D pother = (Point2D) other;
373
                if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
374
                        return false;
375
                }
376
                if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
377
                        return false;
378
                }
379
                return true;
380
381
        }
382 26788 jpiera
383
        /* (non-Javadoc)
384
         * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
385
         */
386
        public double[] getCoordinates() {
387
                return coordinates;
388
        }
389
390
        /* (non-Javadoc)
391
         * @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
392
         */
393
        public double getCoordinateAt(int dimension) {
394
                return coordinates[dimension];
395
        }
396
397
        /* (non-Javadoc)
398
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
399
         */
400
        public void setCoordinateAt(int dimension, double value) {
401 28089 vcaballero
                coordinates[dimension] = value;
402 26788 jpiera
        }
403
404
        /* (non-Javadoc)
405
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
406
         */
407
        public void setCoordinates(double[] values) {
408 28089 vcaballero
                coordinates = values;
409 26788 jpiera
        }
410
411
        /* (non-Javadoc)
412
         * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
413
         */
414
        public void setX(double x) {
415 28089 vcaballero
                coordinates[0] = x;
416 26788 jpiera
        }
417
418
        /* (non-Javadoc)
419
         * @see org.gvsig.fmap.geom.primitive.Point#setY(double)
420
         */
421
        public void setY(double y) {
422 28089 vcaballero
                coordinates[1] = y;
423 26788 jpiera
        }
424
425 28625 jmvivo
        public String toString() {
426 32934 jjdelcerro
                StringBuffer buffer = new StringBuffer();
427
                buffer.append("Point2D(").append(coordinates[0]);
428
                for(int i=1 ; i<coordinates.length; i++ ) {
429
                        buffer.append(",").append(coordinates[i]);
430
                }
431
                buffer.append(")");
432
                return buffer.toString();
433 33650 jpiera
        }
434 24207 jiyarza
}