source: josm/trunk/src/org/openstreetmap/josm/data/ProjectionBounds.java@ 14273

Last change on this file since 14273 was 12818, checked in by bastiK, 7 years ago

see #15229 - move Bounds#visitEdge to Projection#visitOutline

  • joins 2 implementations of the same algorithm
  • removes dependency of Bounds on Projection
  • Property svn:eol-style set to native
File size: 5.4 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[1722]2package org.openstreetmap.josm.data;
3
4import org.openstreetmap.josm.data.coor.EastNorth;
[8384]5import org.openstreetmap.josm.tools.Utils;
[1722]6
7/**
8 * This is a simple data class for "rectangular" areas of the world, given in
[4065]9 * east/north min/max values.
[1722]10 *
11 * @author imi
12 */
13public class ProjectionBounds {
14 /**
[9243]15 * The minimum east coordinate.
[1722]16 */
[9243]17 public double minEast;
18 /**
19 * The minimum north coordinate.
20 */
21 public double minNorth;
22 /**
23 * The maximum east coordinate.
24 */
25 public double maxEast;
26 /**
27 * The minimum north coordinate.
28 */
29 public double maxNorth;
[1722]30
31 /**
[5670]32 * Construct bounds out of two points.
[9243]33 * @param min min east/north
34 * @param max max east/north
[1722]35 */
36 public ProjectionBounds(EastNorth min, EastNorth max) {
[4065]37 this.minEast = min.east();
38 this.minNorth = min.north();
39 this.maxEast = max.east();
40 this.maxNorth = max.north();
[1722]41 }
[8384]42
[9243]43 /**
44 * Construct bounds out of a single point.
45 * @param p east/north
46 */
[1724]47 public ProjectionBounds(EastNorth p) {
[4065]48 this.minEast = this.maxEast = p.east();
49 this.minNorth = this.maxNorth = p.north();
[1724]50 }
[8384]51
[9243]52 /**
53 * Construct bounds out of a center point and east/north dimensions.
54 * @param center center east/north
55 * @param east east dimension
56 * @param north north dimension
57 */
[1722]58 public ProjectionBounds(EastNorth center, double east, double north) {
[4065]59 this.minEast = center.east()-east/2.0;
60 this.minNorth = center.north()-north/2.0;
61 this.maxEast = center.east()+east/2.0;
62 this.maxNorth = center.north()+north/2.0;
[1722]63 }
[8384]64
[9243]65 /**
66 * Construct bounds out of two points.
67 * @param minEast min east
68 * @param minNorth min north
69 * @param maxEast max east
70 * @param maxNorth max north
71 */
[4065]72 public ProjectionBounds(double minEast, double minNorth, double maxEast, double maxNorth) {
73 this.minEast = minEast;
74 this.minNorth = minNorth;
75 this.maxEast = maxEast;
76 this.maxNorth = maxNorth;
77 }
[8384]78
[9243]79 /**
[12818]80 * Construct uninitialized bounds.
81 * <p>
82 * At least one call to {@link #extend(EastNorth)} or {@link #extend(ProjectionBounds)}
83 * is required immediately after construction to initialize the {@code ProjectionBounds}
84 * instance and make it valid.
85 * <p>
86 * Uninitialized {@code ProjectionBounds} must not be passed to other methods
87 * or used in any way other than initializing it.
88 */
89 public ProjectionBounds() {
90 this.minEast = Double.POSITIVE_INFINITY;
91 this.minNorth = Double.POSITIVE_INFINITY;
92 this.maxEast = Double.NEGATIVE_INFINITY;
93 this.maxNorth = Double.NEGATIVE_INFINITY;
94 }
95
96 /**
[9243]97 * Extends bounds to include point {@code e}.
98 * @param e east/north to include
99 */
[8384]100 public void extend(EastNorth e) {
[4065]101 if (e.east() < minEast) {
102 minEast = e.east();
103 }
104 if (e.east() > maxEast) {
105 maxEast = e.east();
106 }
107 if (e.north() < minNorth) {
108 minNorth = e.north();
109 }
110 if (e.north() > maxNorth) {
111 maxNorth = e.north();
112 }
[1722]113 }
[8384]114
[9243]115 /**
[11774]116 * Extends bounds to include bounds {@code b}.
117 * @param b bounds to include
118 * @since 11774
119 */
120 public void extend(ProjectionBounds b) {
121 if (b.minEast < minEast) {
122 minEast = b.minEast;
123 }
124 if (b.maxEast > maxEast) {
125 maxEast = b.maxEast;
126 }
127 if (b.minNorth < minNorth) {
128 minNorth = b.minNorth;
129 }
130 if (b.maxNorth > maxNorth) {
131 maxNorth = b.maxNorth;
132 }
133 }
134
135 /**
[9243]136 * Returns the center east/north.
137 * @return the center east/north
138 */
[8384]139 public EastNorth getCenter() {
[4065]140 return new EastNorth((minEast + maxEast) / 2.0, (minNorth + maxNorth) / 2.0);
[1722]141 }
[1724]142
[8384]143 @Override
144 public String toString() {
[10300]145 return "ProjectionBounds["+minEast+','+minNorth+','+maxEast+','+maxNorth+']';
[1724]146 }
[4065]147
148 /**
149 * The two bounds intersect? Compared to java Shape.intersects, if does not use
[6830]150 * the interior but the closure. ("&gt;=" instead of "&gt;")
[9243]151 * @param b projection bounds
152 * @return {@code true} if the two bounds intersect
[4065]153 */
154 public boolean intersects(ProjectionBounds b) {
155 return b.maxEast >= minEast &&
156 b.maxNorth >= minNorth &&
157 b.minEast <= maxEast &&
158 b.minNorth <= maxNorth;
159 }
160
[9243]161 /**
[9419]162 * Check, if a point is within the bounds.
163 * @param en the point
164 * @return true, if <code>en</code> is within the bounds
165 */
166 public boolean contains(EastNorth en) {
167 return minEast <= en.east() && en.east() <= maxEast &&
168 minNorth <= en.north() && en.north() <= maxNorth;
169 }
170
171 /**
[9243]172 * Returns the min east/north.
173 * @return the min east/north
174 */
[4065]175 public EastNorth getMin() {
176 return new EastNorth(minEast, minNorth);
177 }
178
[9243]179 /**
180 * Returns the max east/north.
181 * @return the max east/north
182 */
[4065]183 public EastNorth getMax() {
184 return new EastNorth(maxEast, maxNorth);
185 }
186
[8927]187 /**
188 * Determines if the bounds area is not null
189 * @return {@code true} if the area is not null
190 */
[7816]191 public boolean hasExtend() {
[8384]192 return !Utils.equalsEpsilon(minEast, maxEast) || !Utils.equalsEpsilon(minNorth, maxNorth);
[7816]193 }
[1722]194}
Note: See TracBrowser for help on using the repository browser.