[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1722] | 2 | package org.openstreetmap.josm.data;
|
---|
| 3 |
|
---|
| 4 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[8384] | 5 | import 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 | */
|
---|
| 13 | public 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 | /**
|
---|
| 80 | * Extends bounds to include point {@code e}.
|
---|
| 81 | * @param e east/north to include
|
---|
| 82 | */
|
---|
[8384] | 83 | public void extend(EastNorth e) {
|
---|
[4065] | 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 | }
|
---|
[1722] | 96 | }
|
---|
[8384] | 97 |
|
---|
[9243] | 98 | /**
|
---|
| 99 | * Returns the center east/north.
|
---|
| 100 | * @return the center east/north
|
---|
| 101 | */
|
---|
[8384] | 102 | public EastNorth getCenter() {
|
---|
[4065] | 103 | return new EastNorth((minEast + maxEast) / 2.0, (minNorth + maxNorth) / 2.0);
|
---|
[1722] | 104 | }
|
---|
[1724] | 105 |
|
---|
[8384] | 106 | @Override
|
---|
| 107 | public String toString() {
|
---|
[10300] | 108 | return "ProjectionBounds["+minEast+','+minNorth+','+maxEast+','+maxNorth+']';
|
---|
[1724] | 109 | }
|
---|
[4065] | 110 |
|
---|
| 111 | /**
|
---|
| 112 | * The two bounds intersect? Compared to java Shape.intersects, if does not use
|
---|
[6830] | 113 | * the interior but the closure. (">=" instead of ">")
|
---|
[9243] | 114 | * @param b projection bounds
|
---|
| 115 | * @return {@code true} if the two bounds intersect
|
---|
[4065] | 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 |
|
---|
[9243] | 124 | /**
|
---|
[9419] | 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 | /**
|
---|
[9243] | 135 | * Returns the min east/north.
|
---|
| 136 | * @return the min east/north
|
---|
| 137 | */
|
---|
[4065] | 138 | public EastNorth getMin() {
|
---|
| 139 | return new EastNorth(minEast, minNorth);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[9243] | 142 | /**
|
---|
| 143 | * Returns the max east/north.
|
---|
| 144 | * @return the max east/north
|
---|
| 145 | */
|
---|
[4065] | 146 | public EastNorth getMax() {
|
---|
| 147 | return new EastNorth(maxEast, maxNorth);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[8927] | 150 | /**
|
---|
| 151 | * Determines if the bounds area is not null
|
---|
| 152 | * @return {@code true} if the area is not null
|
---|
| 153 | */
|
---|
[7816] | 154 | public boolean hasExtend() {
|
---|
[8384] | 155 | return !Utils.equalsEpsilon(minEast, maxEast) || !Utils.equalsEpsilon(minNorth, maxNorth);
|
---|
[7816] | 156 | }
|
---|
[1722] | 157 | }
|
---|