Statistics
| Revision:

root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.lib / org.gvsig.proj.lib.api / src / main / java / org / cresques / px / Extent.java @ 827

History | View | Annotate | Download (5.97 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.cresques.px;
25

    
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.text.DecimalFormat;
29

    
30
/**
31
 *        Clase que getiona el extent de una imagen
32
 *        
33
 *  @author Luis W.Sevilla (sevilla_lui@gva.es)
34
 */
35
public class Extent {
36
    Point2D min = null;
37
    Point2D max = null;
38

    
39
    /**
40
     * Constructor sin par?metros
41
     */
42
    public Extent() {
43
        min = new Point2D.Double(999999999.0, 999999999.0);
44
        max = new Point2D.Double(-999999999.0, -999999999.0);
45
    }
46

    
47
    /**
48
     * Constructor 
49
     * @param pt1        punto que representa la esquina superior izquierda
50
     * @param pt2        punto que representa la esquina inferior derecha
51
     */
52
    public Extent(Point2D pt1, Point2D pt2) {
53
        newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
54
    }
55

    
56
    /**
57
     * Contructor
58
     * @param x1 punto que representa la coordenada X de la esquina superior izquierda
59
     * @param y1 punto que representa la coordenada Y de la esquina superior izquierda
60
     * @param x2 punto que representa la coordenada X de la esquina inferior derecha
61
     * @param y2 punto que representa la coordenada Y de la esquina inferior derecha
62
     */
63
    public Extent(double x1, double y1, double x2, double y2) {
64
        newExtent(x1, y1, x2, y2);
65
    }
66

    
67
    /**
68
     * Constructor
69
     * @param r        Rectangulo 2D
70
     */
71
    public Extent(Rectangle2D r) {
72
        newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(),
73
                  r.getY() + r.getHeight());
74
    }
75

    
76
    /**
77
     * Constructor de copia
78
     * @param ext        Objeto Extent
79
     */
80
    public Extent(Extent ext) {
81
        newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY());
82
    }
83

    
84
    /**
85
     * Crea un objeto extent identico y lo retorna
86
     * @return Objeto extent
87
     */
88
    public Object clone() {
89
        Extent e = (Extent) clone();
90
        e.min = (Point2D) min.clone();
91
        e.max = (Point2D) max.clone();
92

    
93
        return e;
94
    }
95

    
96
    private void newExtent(double x1, double y1, double x2, double y2) {
97
        min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2));
98
        max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2));
99
    }
100

    
101
    /**
102
     * Obtiene la coordenada X m?nima
103
     * @return valor de la coordenada X m?nima
104
     */
105
    public double minX() {
106
        return min.getX();
107
    }
108

    
109
    /**
110
     * Obtiene la coordenada Y m?nima
111
     * @return valor de la coordenada X m?nima
112
     */
113
    public double minY() {
114
        return min.getY();
115
    }
116

    
117
    /**
118
     * Obtiene la coordenada X m?xima
119
     * @return valor de la coordenada X m?xima
120
     */
121
    public double maxX() {
122
        return max.getX();
123
    }
124

    
125
    /**
126
     * Obtiene la coordenada Y m?xima
127
     * @return valor de la coordenada Y m?xima
128
     */
129
    public double maxY() {
130
        return max.getY();
131
    }
132
    
133
    /**
134
     * Obtiene el punto m?nimo
135
     * @return m?nimo
136
     */
137
    public Point2D getMin() {
138
        return min;
139
    }
140

    
141
    /**
142
     * Obtiene el punto m?ximo
143
     * @return m?ximo
144
     */
145
    public Point2D getMax() {
146
        return max;
147
    }
148

    
149
    public boolean isAt(Point2D pt) {
150
        if (pt.getX() < minX()) {
151
            return false;
152
        }
153

    
154
        if (pt.getX() > maxX()) {
155
            return false;
156
        }
157

    
158
        if (pt.getY() < minY()) {
159
            return false;
160
        }
161

    
162
        if (pt.getY() > maxY()) {
163
            return false;
164
        }
165

    
166
        return true;
167
    }
168

    
169
    public double width() {
170
        return Math.abs(maxX() - minX());
171
    }
172

    
173
    public double height() {
174
        return Math.abs(maxY() - minY());
175
    }
176

    
177
    /**
178
     * Verifica un punto, y modifica el extent si no est? incluido
179
     */
180
    public void add(Point2D pt) {
181
        if (pt == null) {
182
            return;
183
        }
184

    
185
        min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY()));
186
        max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY()));
187
    }
188

    
189
    public void add(Extent ext) {
190
        if (ext == null) {
191
            return;
192
        }
193

    
194
        min.setLocation(Math.min(ext.minX(), minX()),
195
                        Math.min(ext.minY(), minY()));
196
        max.setLocation(Math.max(ext.maxX(), maxX()),
197
                        Math.max(ext.maxY(), maxY()));
198
    }
199

    
200
    /**
201
     * Obtiene la escala
202
     * @param width        Ancho
203
     * @param height        Alto
204
     * @return
205
     */
206
    public double[] getScale(int width, int height) {
207
        return getScale((double) width, (double) height);
208
    }
209

    
210
    public double[] getScale(double width, double height) {
211
        double[] scale = new double[2];
212
        scale[0] = ((float) width) / width();
213
        scale[1] = ((float) height) / height();
214

    
215
        return scale;
216
    }
217

    
218
    public Rectangle2D toRectangle2D() {
219
        return new Rectangle2D.Double(minX(), minY(), width(), height());
220
    }
221

    
222
    public String toString() {
223
        DecimalFormat format = new DecimalFormat("####.000");
224

    
225
        return "Extent: (" + format.format(minX()) + "," +
226
               format.format(minY()) + "), (" + format.format(maxX()) + "," +
227
               format.format(maxY()) + ")";
228
    }
229

    
230
    public interface Has {
231
        public Extent getExtent();
232
    }
233
}