Revision 2669 branches/CqCMSDvp/libraries/libCq CMS for java.old/src/org/cresques/px/Extent.java
Extent.java | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications. |
3 |
* |
|
4 |
* Copyright (C) 2004-5. |
|
5 | 3 |
* |
4 |
* Copyright (C) 2004-5. |
|
5 |
* |
|
6 | 6 |
* This program is free software; you can redistribute it and/or |
7 | 7 |
* modify it under the terms of the GNU General Public License |
8 | 8 |
* as published by the Free Software Foundation; either version 2 |
... | ... | |
18 | 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
19 | 19 |
* |
20 | 20 |
* For more information, contact: |
21 |
*
|
|
21 |
* |
|
22 | 22 |
* cresques@gmail.com |
23 | 23 |
*/ |
24 | 24 |
package org.cresques.px; |
... | ... | |
28 | 28 |
|
29 | 29 |
import java.text.DecimalFormat; |
30 | 30 |
|
31 |
|
|
31 | 32 |
public class Extent { |
32 |
Point2D min = null, max = null; |
|
33 |
|
|
34 |
public interface Has { |
|
35 |
public Extent getExtent(); |
|
36 |
} |
|
37 |
|
|
38 |
public Extent() { |
|
39 |
min = new Point2D.Double(999999999.0,999999999.0); |
|
40 |
max = new Point2D.Double(-999999999.0,-999999999.0); |
|
41 |
} |
|
42 |
|
|
43 |
public Extent(Point2D pt1, Point2D pt2) { |
|
44 |
newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY()); |
|
45 |
} |
|
46 |
public Extent(double x1, double y1, double x2, double y2) { |
|
47 |
newExtent(x1, y1, x2, y2); |
|
48 |
} |
|
49 |
public Extent(Rectangle2D r) { |
|
50 |
newExtent(r.getX(), r.getY(), r.getX()+r.getWidth(), r.getY()+r.getHeight()); |
|
51 |
} |
|
52 |
public Extent(Extent ext) { |
|
53 |
newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY()); |
|
54 |
} |
|
55 |
public Object clone() { |
|
56 |
Extent e = (Extent) clone(); |
|
57 |
e.min = (Point2D) min.clone(); |
|
58 |
e.max = (Point2D) max.clone(); |
|
59 |
return e; |
|
60 |
} |
|
61 |
|
|
62 |
private void newExtent(double x1, double y1, double x2, double y2) { |
|
63 |
double [] e = {x1, y1, x2, y2}; |
|
64 |
min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
|
65 |
max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
|
66 |
} |
|
67 |
|
|
68 |
public double minX(){ return min.getX(); } |
|
69 |
public double minY(){ return min.getY(); } |
|
70 |
public double maxX(){ return max.getX(); } |
|
71 |
public double maxY(){ return max.getY(); } |
|
72 |
public Point2D getMin() { return min; } |
|
73 |
public Point2D getMax() { return max; } |
|
74 |
|
|
75 |
public boolean isAt(Point2D pt) { |
|
76 |
if (pt.getX()<minX()) return false; |
|
77 |
if (pt.getX()>maxX()) return false; |
|
78 |
if (pt.getY()<minY()) return false; |
|
79 |
if (pt.getY()>maxY()) return false; |
|
80 |
return true; |
|
81 |
} |
|
33 |
Point2D min = null; |
|
34 |
Point2D max = null; |
|
82 | 35 |
|
83 |
public double width() { return Math.abs(maxX()-minX()); } |
|
84 |
public double height() { return Math.abs(maxY()-minY()); } |
|
85 |
|
|
86 |
/** |
|
87 |
* Verifica un punto, y modifica el extent si no est? incluido |
|
88 |
*/ |
|
89 |
public void add(Point2D pt) { |
|
90 |
if (pt == null) return; |
|
91 |
min.setLocation( Math.min(pt.getX(),minX()), Math.min(pt.getY(), minY())); |
|
92 |
max.setLocation( Math.max(pt.getX(),maxX()), Math.max(pt.getY(), maxY())); |
|
93 |
} |
|
94 |
|
|
95 |
public void add(Extent ext) { |
|
96 |
if (ext == null) return; |
|
97 |
min.setLocation( Math.min(ext.minX(),minX()), Math.min(ext.minY(), minY())); |
|
98 |
max.setLocation( Math.max(ext.maxX(),maxX()), Math.max(ext.maxY(), maxY())); |
|
99 |
} |
|
100 |
|
|
101 |
public double []getScale(int width, int height) { |
|
102 |
return getScale((double) width, (double) height); |
|
103 |
} |
|
104 |
public double []getScale(double width, double height) { |
|
105 |
double scale[] = new double[2]; |
|
106 |
scale[0] = ((float) width) /width(); |
|
107 |
scale[1] = ((float) height)/height(); |
|
108 |
return scale; |
|
109 |
} |
|
110 |
|
|
111 |
public Rectangle2D toRectangle2D() { |
|
112 |
return new Rectangle2D.Double(minX(), minY(), width(), height()); |
|
113 |
} |
|
114 |
public String toString() { |
|
115 |
DecimalFormat format = new DecimalFormat("####.000"); |
|
36 |
public Extent() { |
|
37 |
min = new Point2D.Double(999999999.0, 999999999.0); |
|
38 |
max = new Point2D.Double(-999999999.0, -999999999.0); |
|
39 |
} |
|
116 | 40 |
|
117 |
return "Extent: ("+format.format(minX())+","+format.format(minY())+ |
|
118 |
"), ("+format.format(maxX())+","+format.format(maxY())+")"; |
|
119 |
} |
|
120 |
} |
|
41 |
public Extent(Point2D pt1, Point2D pt2) { |
|
42 |
newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY()); |
|
43 |
} |
|
44 |
|
|
45 |
public Extent(double x1, double y1, double x2, double y2) { |
|
46 |
newExtent(x1, y1, x2, y2); |
|
47 |
} |
|
48 |
|
|
49 |
public Extent(Rectangle2D r) { |
|
50 |
newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(), |
|
51 |
r.getY() + r.getHeight()); |
|
52 |
} |
|
53 |
|
|
54 |
public Extent(Extent ext) { |
|
55 |
newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY()); |
|
56 |
} |
|
57 |
|
|
58 |
public Object clone() { |
|
59 |
Extent e = (Extent) clone(); |
|
60 |
e.min = (Point2D) min.clone(); |
|
61 |
e.max = (Point2D) max.clone(); |
|
62 |
|
|
63 |
return e; |
|
64 |
} |
|
65 |
|
|
66 |
private void newExtent(double x1, double y1, double x2, double y2) { |
|
67 |
double[] e = { x1, y1, x2, y2 }; |
|
68 |
min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
|
69 |
max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
|
70 |
} |
|
71 |
|
|
72 |
public double minX() { |
|
73 |
return min.getX(); |
|
74 |
} |
|
75 |
|
|
76 |
public double minY() { |
|
77 |
return min.getY(); |
|
78 |
} |
|
79 |
|
|
80 |
public double maxX() { |
|
81 |
return max.getX(); |
|
82 |
} |
|
83 |
|
|
84 |
public double maxY() { |
|
85 |
return max.getY(); |
|
86 |
} |
|
87 |
|
|
88 |
public Point2D getMin() { |
|
89 |
return min; |
|
90 |
} |
|
91 |
|
|
92 |
public Point2D getMax() { |
|
93 |
return max; |
|
94 |
} |
|
95 |
|
|
96 |
public boolean isAt(Point2D pt) { |
|
97 |
if (pt.getX() < minX()) { |
|
98 |
return false; |
|
99 |
} |
|
100 |
|
|
101 |
if (pt.getX() > maxX()) { |
|
102 |
return false; |
|
103 |
} |
|
104 |
|
|
105 |
if (pt.getY() < minY()) { |
|
106 |
return false; |
|
107 |
} |
|
108 |
|
|
109 |
if (pt.getY() > maxY()) { |
|
110 |
return false; |
|
111 |
} |
|
112 |
|
|
113 |
return true; |
|
114 |
} |
|
115 |
|
|
116 |
public double width() { |
|
117 |
return Math.abs(maxX() - minX()); |
|
118 |
} |
|
119 |
|
|
120 |
public double height() { |
|
121 |
return Math.abs(maxY() - minY()); |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* Verifica un punto, y modifica el extent si no est? incluido |
|
126 |
*/ |
|
127 |
public void add(Point2D pt) { |
|
128 |
if (pt == null) { |
|
129 |
return; |
|
130 |
} |
|
131 |
|
|
132 |
min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY())); |
|
133 |
max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY())); |
|
134 |
} |
|
135 |
|
|
136 |
public void add(Extent ext) { |
|
137 |
if (ext == null) { |
|
138 |
return; |
|
139 |
} |
|
140 |
|
|
141 |
min.setLocation(Math.min(ext.minX(), minX()), |
|
142 |
Math.min(ext.minY(), minY())); |
|
143 |
max.setLocation(Math.max(ext.maxX(), maxX()), |
|
144 |
Math.max(ext.maxY(), maxY())); |
|
145 |
} |
|
146 |
|
|
147 |
public double[] getScale(int width, int height) { |
|
148 |
return getScale((double) width, (double) height); |
|
149 |
} |
|
150 |
|
|
151 |
public double[] getScale(double width, double height) { |
|
152 |
double[] scale = new double[2]; |
|
153 |
scale[0] = ((float) width) / width(); |
|
154 |
scale[1] = ((float) height) / height(); |
|
155 |
|
|
156 |
return scale; |
|
157 |
} |
|
158 |
|
|
159 |
public Rectangle2D toRectangle2D() { |
|
160 |
return new Rectangle2D.Double(minX(), minY(), width(), height()); |
|
161 |
} |
|
162 |
|
|
163 |
public String toString() { |
|
164 |
DecimalFormat format = new DecimalFormat("####.000"); |
|
165 |
|
|
166 |
return "Extent: (" + format.format(minX()) + "," + |
|
167 |
format.format(minY()) + "), (" + format.format(maxX()) + "," + |
|
168 |
format.format(maxY()) + ")"; |
|
169 |
} |
|
170 |
|
|
171 |
public interface Has { |
|
172 |
public Extent getExtent(); |
|
173 |
} |
|
174 |
} |
Also available in: Unified diff