root / org.gvsig.projection / trunk / src / main / java / org / cresques / px / Extent.java @ 84
History | View | Annotate | Download (5.91 KB)
1 |
/*
|
---|---|
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 |
package org.cresques.px; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
|
29 |
import java.text.DecimalFormat; |
30 |
|
31 |
/**
|
32 |
* Clase que getiona el extent de una imagen
|
33 |
*
|
34 |
* @author Luis W.Sevilla (sevilla_lui@gva.es)
|
35 |
*/
|
36 |
public class Extent { |
37 |
Point2D min = null; |
38 |
Point2D max = null; |
39 |
|
40 |
/**
|
41 |
* Constructor sin par?metros
|
42 |
*/
|
43 |
public Extent() {
|
44 |
min = new Point2D.Double(999999999.0, 999999999.0); |
45 |
max = new Point2D.Double(-999999999.0, -999999999.0); |
46 |
} |
47 |
|
48 |
/**
|
49 |
* Constructor
|
50 |
* @param pt1 punto que representa la esquina superior izquierda
|
51 |
* @param pt2 punto que representa la esquina inferior derecha
|
52 |
*/
|
53 |
public Extent(Point2D pt1, Point2D pt2) { |
54 |
newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY()); |
55 |
} |
56 |
|
57 |
/**
|
58 |
* Contructor
|
59 |
* @param x1 punto que representa la coordenada X de la esquina superior izquierda
|
60 |
* @param y1 punto que representa la coordenada Y de la esquina superior izquierda
|
61 |
* @param x2 punto que representa la coordenada X de la esquina inferior derecha
|
62 |
* @param y2 punto que representa la coordenada Y de la esquina inferior derecha
|
63 |
*/
|
64 |
public Extent(double x1, double y1, double x2, double y2) { |
65 |
newExtent(x1, y1, x2, y2); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Constructor
|
70 |
* @param r Rectangulo 2D
|
71 |
*/
|
72 |
public Extent(Rectangle2D r) { |
73 |
newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(), |
74 |
r.getY() + r.getHeight()); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Constructor de copia
|
79 |
* @param ext Objeto Extent
|
80 |
*/
|
81 |
public Extent(Extent ext) {
|
82 |
newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY()); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Crea un objeto extent identico y lo retorna
|
87 |
* @return Objeto extent
|
88 |
*/
|
89 |
public Object clone() { |
90 |
Extent e = (Extent) clone(); |
91 |
e.min = (Point2D) min.clone();
|
92 |
e.max = (Point2D) max.clone();
|
93 |
|
94 |
return e;
|
95 |
} |
96 |
|
97 |
private void newExtent(double x1, double y1, double x2, double y2) { |
98 |
min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
99 |
max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Obtiene la coordenada X m?nima
|
104 |
* @return valor de la coordenada X m?nima
|
105 |
*/
|
106 |
public double minX() { |
107 |
return min.getX();
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* Obtiene la coordenada Y m?nima
|
112 |
* @return valor de la coordenada X m?nima
|
113 |
*/
|
114 |
public double minY() { |
115 |
return min.getY();
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Obtiene la coordenada X m?xima
|
120 |
* @return valor de la coordenada X m?xima
|
121 |
*/
|
122 |
public double maxX() { |
123 |
return max.getX();
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Obtiene la coordenada Y m?xima
|
128 |
* @return valor de la coordenada Y m?xima
|
129 |
*/
|
130 |
public double maxY() { |
131 |
return max.getY();
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Obtiene el punto m?nimo
|
136 |
* @return m?nimo
|
137 |
*/
|
138 |
public Point2D getMin() { |
139 |
return min;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* Obtiene el punto m?ximo
|
144 |
* @return m?ximo
|
145 |
*/
|
146 |
public Point2D getMax() { |
147 |
return max;
|
148 |
} |
149 |
|
150 |
public boolean isAt(Point2D pt) { |
151 |
if (pt.getX() < minX()) {
|
152 |
return false; |
153 |
} |
154 |
|
155 |
if (pt.getX() > maxX()) {
|
156 |
return false; |
157 |
} |
158 |
|
159 |
if (pt.getY() < minY()) {
|
160 |
return false; |
161 |
} |
162 |
|
163 |
if (pt.getY() > maxY()) {
|
164 |
return false; |
165 |
} |
166 |
|
167 |
return true; |
168 |
} |
169 |
|
170 |
public double width() { |
171 |
return Math.abs(maxX() - minX()); |
172 |
} |
173 |
|
174 |
public double height() { |
175 |
return Math.abs(maxY() - minY()); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Verifica un punto, y modifica el extent si no est? incluido
|
180 |
*/
|
181 |
public void add(Point2D pt) { |
182 |
if (pt == null) { |
183 |
return;
|
184 |
} |
185 |
|
186 |
min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY())); |
187 |
max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY())); |
188 |
} |
189 |
|
190 |
public void add(Extent ext) { |
191 |
if (ext == null) { |
192 |
return;
|
193 |
} |
194 |
|
195 |
min.setLocation(Math.min(ext.minX(), minX()),
|
196 |
Math.min(ext.minY(), minY()));
|
197 |
max.setLocation(Math.max(ext.maxX(), maxX()),
|
198 |
Math.max(ext.maxY(), maxY()));
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Obtiene la escala
|
203 |
* @param width Ancho
|
204 |
* @param height Alto
|
205 |
* @return
|
206 |
*/
|
207 |
public double[] getScale(int width, int height) { |
208 |
return getScale((double) width, (double) height); |
209 |
} |
210 |
|
211 |
public double[] getScale(double width, double height) { |
212 |
double[] scale = new double[2]; |
213 |
scale[0] = ((float) width) / width(); |
214 |
scale[1] = ((float) height) / height(); |
215 |
|
216 |
return scale;
|
217 |
} |
218 |
|
219 |
public Rectangle2D toRectangle2D() { |
220 |
return new Rectangle2D.Double(minX(), minY(), width(), height()); |
221 |
} |
222 |
|
223 |
public String toString() { |
224 |
DecimalFormat format = new DecimalFormat("####.000"); |
225 |
|
226 |
return "Extent: (" + format.format(minX()) + "," + |
227 |
format.format(minY()) + "), (" + format.format(maxX()) + "," + |
228 |
format.format(maxY()) + ")";
|
229 |
} |
230 |
|
231 |
public interface Has { |
232 |
public Extent getExtent();
|
233 |
} |
234 |
} |