root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / viewport / Extent.java @ 21606
History | View | Annotate | Download (6.46 KB)
1 | 21606 | jldominguez | /*
|
---|---|---|---|
2 | * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
||
3 | *
|
||
4 | * Copyright (C) 2004-5.
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * cresques@gmail.com
|
||
23 | */
|
||
24 | /************************************************
|
||
25 | * *
|
||
26 | * Modfied By: *
|
||
27 | * Prodevelop Integraci?n de Tecnolog?as SL *
|
||
28 | * Conde Salvatierra de ?lava , 34-10 *
|
||
29 | * 46004 Valencia *
|
||
30 | * Spain *
|
||
31 | * *
|
||
32 | * +34 963 510 612 *
|
||
33 | * +34 963 510 968 *
|
||
34 | * gis@prodevelop.es *
|
||
35 | * http://www.prodevelop.es *
|
||
36 | * *
|
||
37 | * gvSIG Mobile Team 2006 *
|
||
38 | * *
|
||
39 | ************************************************/
|
||
40 | |||
41 | package es.prodevelop.gvsig.mobile.fmap.viewport; |
||
42 | |||
43 | import java.awt.geom.Point2D; |
||
44 | import java.awt.geom.Rectangle2D; |
||
45 | import java.text.DecimalFormat; |
||
46 | |||
47 | /**
|
||
48 | * Clase que getiona el extent de una imagen
|
||
49 | *
|
||
50 | * @author Luis W.Sevilla (sevilla_lui@gva.es)
|
||
51 | */
|
||
52 | public class Extent { |
||
53 | Point2D min = null; |
||
54 | Point2D max = null; |
||
55 | |||
56 | /**
|
||
57 | * Constructor sin par?metros
|
||
58 | */
|
||
59 | public Extent() {
|
||
60 | min = new Point2D.Double(999999999.0, 999999999.0); |
||
61 | max = new Point2D.Double(-999999999.0, -999999999.0); |
||
62 | } |
||
63 | |||
64 | /**
|
||
65 | * Constructor
|
||
66 | * @param pt1 punto que representa la esquina superior izquierda
|
||
67 | * @param pt2 punto que representa la esquina inferior derecha
|
||
68 | */
|
||
69 | public Extent(Point2D pt1, Point2D pt2) { |
||
70 | newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY()); |
||
71 | } |
||
72 | |||
73 | /**
|
||
74 | * Contructor
|
||
75 | * @param x1 punto que representa la coordenada X de la esquina superior izquierda
|
||
76 | * @param y1 punto que representa la coordenada Y de la esquina superior izquierda
|
||
77 | * @param x2 punto que representa la coordenada X de la esquina inferior derecha
|
||
78 | * @param y2 punto que representa la coordenada Y de la esquina inferior derecha
|
||
79 | */
|
||
80 | public Extent(double x1, double y1, double x2, double y2) { |
||
81 | newExtent(x1, y1, x2, y2); |
||
82 | } |
||
83 | |||
84 | /**
|
||
85 | * Constructor
|
||
86 | * @param r Rectangulo 2D
|
||
87 | */
|
||
88 | public Extent(Rectangle2D r) { |
||
89 | newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(), |
||
90 | r.getY() + r.getHeight()); |
||
91 | } |
||
92 | |||
93 | /**
|
||
94 | * Constructor de copia
|
||
95 | * @param ext Objeto Extent
|
||
96 | */
|
||
97 | public Extent(Extent ext) {
|
||
98 | newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY()); |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * Crea un objeto extent identico y lo retorna
|
||
103 | * @return Objeto extent
|
||
104 | */
|
||
105 | public Object clone() { |
||
106 | Extent e = (Extent) clone(); |
||
107 | e.min = (Point2D) min.clone();
|
||
108 | e.max = (Point2D) max.clone();
|
||
109 | |||
110 | return e;
|
||
111 | } |
||
112 | |||
113 | private void newExtent(double x1, double y1, double x2, double y2) { |
||
114 | double[] e = { x1, y1, x2, y2 }; |
||
115 | min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
||
116 | max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * Obtiene la coordenada X m?nima
|
||
121 | * @return valor de la coordenada X m?nima
|
||
122 | */
|
||
123 | public double minX() { |
||
124 | return min.getX();
|
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * Obtiene la coordenada Y m?nima
|
||
129 | * @return valor de la coordenada X m?nima
|
||
130 | */
|
||
131 | public double minY() { |
||
132 | return min.getY();
|
||
133 | } |
||
134 | |||
135 | /**
|
||
136 | * Obtiene la coordenada X m?xima
|
||
137 | * @return valor de la coordenada X m?xima
|
||
138 | */
|
||
139 | public double maxX() { |
||
140 | return max.getX();
|
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * Obtiene la coordenada Y m?xima
|
||
145 | * @return valor de la coordenada Y m?xima
|
||
146 | */
|
||
147 | public double maxY() { |
||
148 | return max.getY();
|
||
149 | } |
||
150 | |||
151 | /**
|
||
152 | * Obtiene el punto m?nimo
|
||
153 | * @return m?nimo
|
||
154 | */
|
||
155 | public Point2D getMin() { |
||
156 | return min;
|
||
157 | } |
||
158 | |||
159 | /**
|
||
160 | * Obtiene el punto m?ximo
|
||
161 | * @return m?ximo
|
||
162 | */
|
||
163 | public Point2D getMax() { |
||
164 | return max;
|
||
165 | } |
||
166 | |||
167 | public boolean isAt(Point2D pt) { |
||
168 | if (pt.getX() < minX()) {
|
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | if (pt.getX() > maxX()) {
|
||
173 | return false; |
||
174 | } |
||
175 | |||
176 | if (pt.getY() < minY()) {
|
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | if (pt.getY() > maxY()) {
|
||
181 | return false; |
||
182 | } |
||
183 | |||
184 | return true; |
||
185 | } |
||
186 | |||
187 | public double width() { |
||
188 | return Math.abs(maxX() - minX()); |
||
189 | } |
||
190 | |||
191 | public double height() { |
||
192 | return Math.abs(maxY() - minY()); |
||
193 | } |
||
194 | |||
195 | /**
|
||
196 | * Verifica un punto, y modifica el extent si no est? incluido
|
||
197 | */
|
||
198 | public void add(Point2D pt) { |
||
199 | if (pt == null) { |
||
200 | return;
|
||
201 | } |
||
202 | |||
203 | min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY())); |
||
204 | max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY())); |
||
205 | } |
||
206 | |||
207 | public void add(Extent ext) { |
||
208 | if (ext == null) { |
||
209 | return;
|
||
210 | } |
||
211 | |||
212 | min.setLocation(Math.min(ext.minX(), minX()),
|
||
213 | Math.min(ext.minY(), minY()));
|
||
214 | max.setLocation(Math.max(ext.maxX(), maxX()),
|
||
215 | Math.max(ext.maxY(), maxY()));
|
||
216 | } |
||
217 | |||
218 | /**
|
||
219 | * Obtiene la escala
|
||
220 | * @param width Ancho
|
||
221 | * @param height Alto
|
||
222 | * @return
|
||
223 | */
|
||
224 | public double[] getScale(int width, int height) { |
||
225 | return getScale((double) width, (double) height); |
||
226 | } |
||
227 | |||
228 | public double[] getScale(double width, double height) { |
||
229 | double[] scale = new double[2]; |
||
230 | scale[0] = ((float) width) / width(); |
||
231 | scale[1] = ((float) height) / height(); |
||
232 | |||
233 | return scale;
|
||
234 | } |
||
235 | |||
236 | public Rectangle2D toRectangle2D() { |
||
237 | return new Rectangle2D.Double(minX(), minY(), width(), height()); |
||
238 | } |
||
239 | |||
240 | public String toString() { |
||
241 | DecimalFormat format = new DecimalFormat("####.000"); |
||
242 | |||
243 | return "Extent: (" + format.format(minX()) + "," + |
||
244 | format.format(minY()) + "), (" + format.format(maxX()) + "," + |
||
245 | format.format(maxY()) + ")";
|
||
246 | } |
||
247 | |||
248 | public interface Has { |
||
249 | public Extent getExtent();
|
||
250 | } |
||
251 | } |