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 | 36221 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 20761 | jmvivo | *
|
3 | 36221 | cordinyana | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | 20761 | jmvivo | * 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 | 36221 | cordinyana | *
|
11 | 20761 | jmvivo | * 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 | 36221 | cordinyana | *
|
16 | 20761 | jmvivo | * 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 | 36221 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 20761 | jmvivo | */
|
22 | 27029 | jpiera | package org.gvsig.fmap.geom.primitive.impl; |
23 | 20761 | jmvivo | |
24 | import java.awt.Rectangle; |
||
25 | 38599 | cordinyana | import java.awt.Shape; |
26 | 20761 | jmvivo | import java.awt.geom.AffineTransform; |
27 | import java.awt.geom.PathIterator; |
||
28 | import java.awt.geom.Rectangle2D; |
||
29 | |||
30 | 21870 | vcaballero | import org.cresques.cts.ICoordTrans; |
31 | import org.cresques.cts.IProjection; |
||
32 | 33650 | jpiera | |
33 | 24207 | jiyarza | import org.gvsig.fmap.geom.DirectPosition; |
34 | 29097 | jpiera | import org.gvsig.fmap.geom.Geometry; |
35 | 20761 | jmvivo | import org.gvsig.fmap.geom.handler.AbstractHandler; |
36 | import org.gvsig.fmap.geom.handler.FinalHandler; |
||
37 | import org.gvsig.fmap.geom.handler.Handler; |
||
38 | 27029 | jpiera | 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 | 20761 | jmvivo | import org.gvsig.fmap.geom.type.GeometryType; |
44 | |||
45 | /**
|
||
46 | 38599 | cordinyana | * 2D point implementation.
|
47 | 36221 | cordinyana | *
|
48 | 20761 | jmvivo | * @author Vicente Caballero Navarro
|
49 | 38599 | cordinyana | * @author gvSIG team
|
50 | 20761 | jmvivo | */
|
51 | 24207 | jiyarza | public class Point2D extends AbstractPrimitive implements Point, DirectPosition { |
52 | 38599 | cordinyana | |
53 | private static final long serialVersionUID = 1836257305083881299L; |
||
54 | 36221 | cordinyana | |
55 | protected static final String COORDINATES_FIELD = "coordinates"; |
||
56 | 38599 | cordinyana | protected double x; |
57 | protected double y; |
||
58 | 36221 | cordinyana | |
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 | 38599 | cordinyana | super(geometryType, null, null); |
69 | this.x = 0.0d; |
||
70 | this.y = 0.0d; |
||
71 | 36221 | cordinyana | } |
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 | 38599 | cordinyana | this.x = x;
|
80 | this.y = y;
|
||
81 | 35613 | jpiera | } |
82 | 20761 | jmvivo | |
83 | 36221 | cordinyana | public Point2D(double x, double y) { |
84 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
85 | 38599 | cordinyana | this.x = x;
|
86 | this.y = y;
|
||
87 | 36221 | cordinyana | } |
88 | 20761 | jmvivo | |
89 | 38599 | cordinyana | public Point2D(GeometryType geometryType, double x, double y) { |
90 | super(geometryType, null, null); |
||
91 | this.x = x;
|
||
92 | this.y = y;
|
||
93 | } |
||
94 | |||
95 | 36221 | cordinyana | public Point2D(int type, int subtype) { |
96 | super(type, subtype);
|
||
97 | 38599 | cordinyana | this.x = 0.0d; |
98 | this.y = 0.0d; |
||
99 | 36221 | cordinyana | } |
100 | 20761 | jmvivo | |
101 | 36221 | cordinyana | public Point2D(java.awt.geom.Point2D point) { |
102 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
103 | 38599 | cordinyana | this.x = point.getX();
|
104 | this.y = point.getY();
|
||
105 | 36221 | cordinyana | } |
106 | 20761 | jmvivo | |
107 | 36221 | cordinyana | /**
|
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 | 38599 | cordinyana | double[] coordinates = getCoordinates(); |
116 | 36221 | cordinyana | at.transform(coordinates, 0, coordinates, 0, 1); |
117 | 38599 | cordinyana | setCoordinates(coordinates); |
118 | 36221 | cordinyana | } |
119 | 20761 | jmvivo | |
120 | 36221 | cordinyana | public boolean contains(double x, double y) { |
121 | 38599 | cordinyana | if ((this.x == x) || (this.y == y)) { |
122 | 36221 | cordinyana | return true; |
123 | } else {
|
||
124 | return false; |
||
125 | } |
||
126 | } |
||
127 | 20761 | jmvivo | |
128 | 36221 | cordinyana | public boolean contains(double x, double y, double w, double h) { |
129 | return false; |
||
130 | } |
||
131 | 20761 | jmvivo | |
132 | 36221 | cordinyana | public boolean intersects(double x, double y, double w, double h) { |
133 | Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h); |
||
134 | 20761 | jmvivo | |
135 | 38599 | cordinyana | return rAux.contains(this.x, this.y); |
136 | 36221 | cordinyana | } |
137 | 20761 | jmvivo | |
138 | 36221 | cordinyana | public Rectangle getBounds() { |
139 | 38599 | cordinyana | return new Rectangle((int) x, (int) y, 0, 0); |
140 | 36221 | cordinyana | } |
141 | 20761 | jmvivo | |
142 | 36221 | cordinyana | public double getX() { |
143 | 38599 | cordinyana | return x;
|
144 | 36221 | cordinyana | } |
145 | 20761 | jmvivo | |
146 | 36221 | cordinyana | public double getY() { |
147 | 38599 | cordinyana | return y;
|
148 | 36221 | cordinyana | } |
149 | 20761 | jmvivo | |
150 | 36221 | cordinyana | public boolean contains(java.awt.geom.Point2D p) { |
151 | return false; |
||
152 | } |
||
153 | 20761 | jmvivo | |
154 | 36221 | cordinyana | public Rectangle2D getBounds2D() { |
155 | 38599 | cordinyana | return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02); |
156 | 36221 | cordinyana | } |
157 | 20761 | jmvivo | |
158 | 36221 | cordinyana | public boolean contains(Rectangle2D r) { |
159 | return false; |
||
160 | } |
||
161 | 20761 | jmvivo | |
162 | 36221 | cordinyana | public boolean intersects(Rectangle2D r) { |
163 | 38599 | cordinyana | return r.contains(x, y);
|
164 | 36221 | cordinyana | } |
165 | 28089 | vcaballero | |
166 | 38599 | cordinyana | public Shape getShape(AffineTransform affineTransform) { |
167 | return this; |
||
168 | } |
||
169 | |||
170 | 36221 | cordinyana | public PathIterator getPathIterator(AffineTransform at) { |
171 | return new PointIterator(getPoint2D(), at); |
||
172 | } |
||
173 | 21425 | vcaballero | |
174 | 36221 | cordinyana | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
175 | return new PointIterator(getPoint2D(), at); |
||
176 | } |
||
177 | 20761 | jmvivo | |
178 | 36221 | cordinyana | private java.awt.geom.Point2D getPoint2D() {
|
179 | 38599 | cordinyana | return new java.awt.geom.Point2D.Double(x, y); |
180 | 36221 | cordinyana | } |
181 | 20761 | jmvivo | |
182 | 36221 | cordinyana | public int getShapeType() { |
183 | return TYPES.POINT;
|
||
184 | } |
||
185 | 20761 | jmvivo | |
186 | 36221 | cordinyana | public FShape cloneFShape() {
|
187 | 38599 | cordinyana | return new Point2D(getGeometryType(), id, projection, x, y); |
188 | 36221 | cordinyana | } |
189 | 20761 | jmvivo | |
190 | 36221 | cordinyana | public void reProject(ICoordTrans ct) { |
191 | java.awt.geom.Point2D p = getPoint2D(); |
||
192 | p = ct.convert(p, p); |
||
193 | 38599 | cordinyana | this.x = p.getX();
|
194 | this.y = p.getY();
|
||
195 | 36221 | cordinyana | } |
196 | 20761 | jmvivo | |
197 | 36221 | cordinyana | public Handler[] getStretchingHandlers() { |
198 | 38599 | cordinyana | return new Handler[] { new PointHandler(0, x, y, this) }; |
199 | 36221 | cordinyana | } |
200 | 20761 | jmvivo | |
201 | 36221 | cordinyana | public Handler[] getSelectHandlers() { |
202 | 38599 | cordinyana | return new Handler[] { new PointHandler(0, x, y, this) }; |
203 | 36221 | cordinyana | } |
204 | 20761 | jmvivo | |
205 | 36221 | cordinyana | /**
|
206 | *
|
||
207 | * @author Vicente Caballero Navarro
|
||
208 | 38599 | cordinyana | * @author gvSIG team
|
209 | 36221 | cordinyana | */
|
210 | class PointHandler extends AbstractHandler implements FinalHandler { |
||
211 | 20761 | jmvivo | |
212 | 38599 | cordinyana | private final Point gvSIGPoint; |
213 | |||
214 | public PointHandler(int i, double x, double y, Point gvSIGPoint) { |
||
215 | this.gvSIGPoint = gvSIGPoint;
|
||
216 | 36221 | cordinyana | point = new java.awt.geom.Point2D.Double(x, y);
|
217 | index = i; |
||
218 | } |
||
219 | 21425 | vcaballero | |
220 | 38599 | cordinyana | public void move(double movex, double movey) { |
221 | gvSIGPoint.setX(gvSIGPoint.getX() + movex); |
||
222 | gvSIGPoint.setY(gvSIGPoint.getY() + movey); |
||
223 | 36221 | cordinyana | } |
224 | 26289 | jmvivo | |
225 | 38599 | cordinyana | public void set(double setx, double sety) { |
226 | gvSIGPoint.setX(setx); |
||
227 | gvSIGPoint.setY(sety); |
||
228 | 36221 | cordinyana | } |
229 | } |
||
230 | 26289 | jmvivo | |
231 | 36221 | cordinyana | public int getDimension() { |
232 | return 2; |
||
233 | } |
||
234 | 26289 | jmvivo | |
235 | 36221 | cordinyana | public Envelope getEnvelope() {
|
236 | 38599 | cordinyana | return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02); |
237 | 36221 | cordinyana | } |
238 | 26289 | jmvivo | |
239 | 36221 | cordinyana | public GeneralPathX getGeneralPath() {
|
240 | return null; |
||
241 | } |
||
242 | 26788 | jpiera | |
243 | 36221 | cordinyana | public DirectPosition getDirectPosition() {
|
244 | return this; |
||
245 | } |
||
246 | 26788 | jpiera | |
247 | 36221 | cordinyana | public double getOrdinate(int dim) { |
248 | 38599 | cordinyana | if (dim == Geometry.DIMENSIONS.X) {
|
249 | 36221 | cordinyana | return getX();
|
250 | } else
|
||
251 | 38599 | cordinyana | if (dim == Geometry.DIMENSIONS.Y) {
|
252 | 36221 | cordinyana | return getY();
|
253 | } else {
|
||
254 | return 0; |
||
255 | } |
||
256 | } |
||
257 | 26788 | jpiera | |
258 | 36221 | cordinyana | 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 | 26788 | jpiera | |
271 | 36221 | cordinyana | } |
272 | 26788 | jpiera | |
273 | 36221 | cordinyana | public double[] getCoordinates() { |
274 | 38599 | cordinyana | return new double[] { x, y }; |
275 | 36221 | cordinyana | } |
276 | 26788 | jpiera | |
277 | 36221 | cordinyana | public double getCoordinateAt(int dimension) { |
278 | 38599 | cordinyana | 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 | 36221 | cordinyana | } |
287 | 26788 | jpiera | |
288 | 36221 | cordinyana | public void setCoordinateAt(int dimension, double value) { |
289 | 38599 | cordinyana | 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 | 36221 | cordinyana | } |
300 | |||
301 | public void setCoordinates(double[] values) { |
||
302 | 38599 | cordinyana | this.x = values[Geometry.DIMENSIONS.X];
|
303 | this.y = values[Geometry.DIMENSIONS.Y];
|
||
304 | 36221 | cordinyana | } |
305 | |||
306 | public void setX(double x) { |
||
307 | 38599 | cordinyana | this.x = x;
|
308 | 36221 | cordinyana | } |
309 | |||
310 | public void setY(double y) { |
||
311 | 38599 | cordinyana | this.y = y;
|
312 | 36221 | cordinyana | } |
313 | |||
314 | public String toString() { |
||
315 | StringBuffer buffer = new StringBuffer(); |
||
316 | 38599 | cordinyana | 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 | 36221 | cordinyana | buffer.append(")");
|
322 | return buffer.toString();
|
||
323 | } |
||
324 | |||
325 | 38599 | cordinyana | protected String getFullTypeName() { |
326 | return "Point2D"; |
||
327 | 36221 | cordinyana | } |
328 | 38599 | cordinyana | |
329 | public int getType() { |
||
330 | return TYPES.POINT;
|
||
331 | } |
||
332 | 36221 | cordinyana | } |