Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / px / Extent.java @ 14

History | View | Annotate | Download (2.82 KB)

1
package org.cresques.px;
2

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5

    
6
import java.text.DecimalFormat;
7

    
8
public class Extent {
9
        Point2D min = null, max = null;
10
        
11
        public interface Has {
12
                public Extent getExtent();
13
        }
14
                
15
        public Extent() {
16
                min = new Point2D.Double(999999999.0,999999999.0);
17
                max = new Point2D.Double(-999999999.0,-999999999.0);
18
        }
19
        
20
        public Extent(Point2D pt1, Point2D pt2) {
21
                newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
22
        }
23
        public Extent(double x1, double y1, double x2, double y2) {
24
                newExtent(x1, y1, x2, y2);
25
        }
26
        public Extent(Rectangle2D r) {
27
                newExtent(r.getX(), r.getY(), r.getX()+r.getWidth(), r.getY()+r.getHeight());
28
        }
29
        public Extent(Extent ext) {
30
                newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY());
31
        }
32
        public Object clone() {
33
                Extent e = (Extent) clone();
34
                e.min = (Point2D) min.clone();
35
                e.max = (Point2D) max.clone();
36
                return e;
37
        }
38
        
39
        private void newExtent(double x1, double y1, double x2, double y2) {
40
                double [] e = {x1, y1, x2, y2};
41
                min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2));
42
                max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2));
43
        }
44
        
45
        public double minX(){ return min.getX();        }
46
        public double minY(){ return min.getY();        }
47
        public double maxX(){ return max.getX();        }
48
        public double maxY(){ return max.getY();        }
49
        public Point2D getMin() { return min; }
50
        public Point2D getMax() { return max; }
51
        
52
        public boolean isAt(Point2D pt) {
53
                if (pt.getX()<minX()) return false;
54
                if (pt.getX()>maxX()) return false;
55
                if (pt.getY()<minY()) return false;
56
                if (pt.getY()>maxY()) return false;
57
                return true;
58
        }
59

    
60
        public double width() { return Math.abs(maxX()-minX()); }
61
        public double height() { return Math.abs(maxY()-minY()); }
62
        
63
        /**
64
         * Verifica un punto, y modifica el extent si no est? incluido
65
         */
66
        public void add(Point2D pt) {
67
                if (pt == null) return;
68
                min.setLocation( Math.min(pt.getX(),minX()), Math.min(pt.getY(), minY()));
69
                max.setLocation( Math.max(pt.getX(),maxX()), Math.max(pt.getY(), maxY()));
70
        }
71
        
72
        public void add(Extent ext) {
73
                if (ext == null) return;
74
                min.setLocation( Math.min(ext.minX(),minX()), Math.min(ext.minY(), minY()));
75
                max.setLocation( Math.max(ext.maxX(),maxX()), Math.max(ext.maxY(), maxY()));
76
        }
77
        
78
        public double []getScale(int width, int height) {
79
                return getScale((double) width, (double) height);
80
        }
81
        public double []getScale(double width, double height) {
82
                double scale[] = new double[2];
83
                scale[0] = ((float) width) /width();                
84
                scale[1] = ((float) height)/height();                
85
                return scale;
86
        }
87
        
88
        public Rectangle2D toRectangle2D() {
89
                return new Rectangle2D.Double(minX(), minY(), width(), height());
90
        }
91
        public String toString() {
92
                DecimalFormat format = new DecimalFormat("####.000");
93

    
94
                return "Extent: ("+format.format(minX())+","+format.format(minY())+
95
                        "), ("+format.format(maxX())+","+format.format(maxY())+")";
96
        }
97
}