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

Last change on this file since 11710 was 10300, checked in by Don-vip, 8 years ago

sonar - Performance - Method passes constant String of length 1 to character overridden method + add unit tests/javadoc

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import org.openstreetmap.josm.data.coor.EastNorth;
5import org.openstreetmap.josm.tools.Utils;
6
7/**
8 * This is a simple data class for "rectangular" areas of the world, given in
9 * east/north min/max values.
10 *
11 * @author imi
12 */
13public class ProjectionBounds {
14 /**
15 * The minimum east coordinate.
16 */
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;
30
31 /**
32 * Construct bounds out of two points.
33 * @param min min east/north
34 * @param max max east/north
35 */
36 public ProjectionBounds(EastNorth min, EastNorth max) {
37 this.minEast = min.east();
38 this.minNorth = min.north();
39 this.maxEast = max.east();
40 this.maxNorth = max.north();
41 }
42
43 /**
44 * Construct bounds out of a single point.
45 * @param p east/north
46 */
47 public ProjectionBounds(EastNorth p) {
48 this.minEast = this.maxEast = p.east();
49 this.minNorth = this.maxNorth = p.north();
50 }
51
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 */
58 public ProjectionBounds(EastNorth center, double east, double north) {
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;
63 }
64
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 */
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 }
78
79 /**
80 * Extends bounds to include point {@code e}.
81 * @param e east/north to include
82 */
83 public void extend(EastNorth e) {
84 if (e.east() < minEast) {
85 minEast = e.east();
86 }
87 if (e.east() > maxEast) {
88 maxEast = e.east();
89 }
90 if (e.north() < minNorth) {
91 minNorth = e.north();
92 }
93 if (e.north() > maxNorth) {
94 maxNorth = e.north();
95 }
96 }
97
98 /**
99 * Returns the center east/north.
100 * @return the center east/north
101 */
102 public EastNorth getCenter() {
103 return new EastNorth((minEast + maxEast) / 2.0, (minNorth + maxNorth) / 2.0);
104 }
105
106 @Override
107 public String toString() {
108 return "ProjectionBounds["+minEast+','+minNorth+','+maxEast+','+maxNorth+']';
109 }
110
111 /**
112 * The two bounds intersect? Compared to java Shape.intersects, if does not use
113 * the interior but the closure. ("&gt;=" instead of "&gt;")
114 * @param b projection bounds
115 * @return {@code true} if the two bounds intersect
116 */
117 public boolean intersects(ProjectionBounds b) {
118 return b.maxEast >= minEast &&
119 b.maxNorth >= minNorth &&
120 b.minEast <= maxEast &&
121 b.minNorth <= maxNorth;
122 }
123
124 /**
125 * Check, if a point is within the bounds.
126 * @param en the point
127 * @return true, if <code>en</code> is within the bounds
128 */
129 public boolean contains(EastNorth en) {
130 return minEast <= en.east() && en.east() <= maxEast &&
131 minNorth <= en.north() && en.north() <= maxNorth;
132 }
133
134 /**
135 * Returns the min east/north.
136 * @return the min east/north
137 */
138 public EastNorth getMin() {
139 return new EastNorth(minEast, minNorth);
140 }
141
142 /**
143 * Returns the max east/north.
144 * @return the max east/north
145 */
146 public EastNorth getMax() {
147 return new EastNorth(maxEast, maxNorth);
148 }
149
150 /**
151 * Determines if the bounds area is not null
152 * @return {@code true} if the area is not null
153 */
154 public boolean hasExtend() {
155 return !Utils.equalsEpsilon(minEast, maxEast) || !Utils.equalsEpsilon(minNorth, maxNorth);
156 }
157}
Note: See TracBrowser for help on using the repository browser.