Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / px / Extent.java @ 8026

History | View | Annotate | Download (5.95 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
        double[] e = { x1, y1, x2, y2 };
99
        min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2));
100
        max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2));
101
    }
102

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

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

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

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

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

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

    
156
        if (pt.getX() > maxX()) {
157
            return false;
158
        }
159

    
160
        if (pt.getY() < minY()) {
161
            return false;
162
        }
163

    
164
        if (pt.getY() > maxY()) {
165
            return false;
166
        }
167

    
168
        return true;
169
    }
170

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

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

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

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

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

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

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

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

    
217
        return scale;
218
    }
219

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

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

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

    
232
    public interface Has {
233
        public Extent getExtent();
234
    }
235
}