| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.coor;
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Northing, Easting of the projected coordinates.
|
|---|
| 6 | *
|
|---|
| 7 | * This class is immutable.
|
|---|
| 8 | *
|
|---|
| 9 | * @author Imi
|
|---|
| 10 | */
|
|---|
| 11 | public class EastNorth extends Coordinate {
|
|---|
| 12 |
|
|---|
| 13 | private static final long serialVersionUID = 1L;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * A zero constant
|
|---|
| 17 | */
|
|---|
| 18 | public static final EastNorth ZERO = new EastNorth(0, 0);
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Constructs a new {@code EastNorth}.
|
|---|
| 22 | * @param east easting
|
|---|
| 23 | * @param north northing
|
|---|
| 24 | */
|
|---|
| 25 | public EastNorth(double east, double north) {
|
|---|
| 26 | super(east, north);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Returns easting.
|
|---|
| 31 | * @return easting
|
|---|
| 32 | */
|
|---|
| 33 | public double east() {
|
|---|
| 34 | return x;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Returns northing.
|
|---|
| 39 | * @return northing
|
|---|
| 40 | */
|
|---|
| 41 | public double north() {
|
|---|
| 42 | return y;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Adds an offset to this {@link EastNorth} instance and returns the result.
|
|---|
| 47 | * @param dEast The offset to add in east direction.
|
|---|
| 48 | * @param dNorth The offset to add in north direction.
|
|---|
| 49 | * @return The result.
|
|---|
| 50 | */
|
|---|
| 51 | public EastNorth add(double dEast, double dNorth) {
|
|---|
| 52 | return new EastNorth(east()+dEast, north()+dNorth);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Adds the coordinates of another EastNorth instance to this one.
|
|---|
| 57 | * @param other The other instance.
|
|---|
| 58 | * @return The new EastNorth position.
|
|---|
| 59 | */
|
|---|
| 60 | public EastNorth add(EastNorth other) {
|
|---|
| 61 | return new EastNorth(x+other.x, y+other.y);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Subtracts an east/north value from this point.
|
|---|
| 66 | * @param other The other value to subtract from this.
|
|---|
| 67 | * @return A point with the new coordinates.
|
|---|
| 68 | */
|
|---|
| 69 | public EastNorth subtract(EastNorth other) {
|
|---|
| 70 | return new EastNorth(x-other.x, y-other.y);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Scales this {@link EastNorth} instance to a given factor and returns the result.
|
|---|
| 75 | * @param s factor
|
|---|
| 76 | * @return The result.
|
|---|
| 77 | */
|
|---|
| 78 | public EastNorth scale(double s) {
|
|---|
| 79 | return new EastNorth(s * x, s * y);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Does a linear interpolation between two EastNorth instances.
|
|---|
| 84 | * @param en2 The other EstNort instance.
|
|---|
| 85 | * @param proportion The proportion the other instance influences the result.
|
|---|
| 86 | * @return The new {@link EastNorth} position.
|
|---|
| 87 | */
|
|---|
| 88 | public EastNorth interpolate(EastNorth en2, double proportion) {
|
|---|
| 89 | // this is an alternate form of this.x + proportion * (en2.x - this.x) that is slightly faster
|
|---|
| 90 | return new EastNorth((1 - proportion) * this.x + proportion * en2.x,
|
|---|
| 91 | (1 - proportion) * this.y + proportion * en2.y);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Gets the center between two {@link EastNorth} instances.
|
|---|
| 96 | * @param en2 The other instance.
|
|---|
| 97 | * @return The center between this and the other instance.
|
|---|
| 98 | */
|
|---|
| 99 | public EastNorth getCenter(EastNorth en2) {
|
|---|
| 100 | // The JIT will inline this for us, it is as fast as the normal /2 approach
|
|---|
| 101 | return interpolate(en2, .5);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Returns the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
|---|
| 106 | *
|
|---|
| 107 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
|---|
| 108 | * @return the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
|---|
| 109 | * @since 6166
|
|---|
| 110 | */
|
|---|
| 111 | public double distance(final EastNorth en) {
|
|---|
| 112 | return super.distance(en);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Returns the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
|---|
| 117 | *
|
|---|
| 118 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
|---|
| 119 | * @return the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
|---|
| 120 | * @since 6166
|
|---|
| 121 | */
|
|---|
| 122 | public double distanceSq(final EastNorth en) {
|
|---|
| 123 | return super.distanceSq(en);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * Counts length (distance from [0,0]) of this.
|
|---|
| 128 | *
|
|---|
| 129 | * @return length of this
|
|---|
| 130 | */
|
|---|
| 131 | public double length() {
|
|---|
| 132 | return Math.sqrt(x*x + y*y);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Returns the heading, in radians, that you have to use to get from
|
|---|
| 137 | * this EastNorth to another. Heading is mapped into [0, 2pi)
|
|---|
| 138 | *
|
|---|
| 139 | * @param other the "destination" position
|
|---|
| 140 | * @return heading
|
|---|
| 141 | */
|
|---|
| 142 | public double heading(EastNorth other) {
|
|---|
| 143 | double hd = Math.atan2(other.east() - east(), other.north() - north());
|
|---|
| 144 | if (hd < 0) {
|
|---|
| 145 | hd = 2 * Math.PI + hd;
|
|---|
| 146 | }
|
|---|
| 147 | return hd;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Behaves exactly like {@link EastNorth#heading(EastNorth)} if {@code refHeading} is {@code 0.0},
|
|---|
| 152 | * otherwise gives a heading not relative to north (0.0). It replies the radians needed to add to
|
|---|
| 153 | * {@code refHeading} to get from this EastNorth to another.
|
|---|
| 154 | * @param other the "destination" position
|
|---|
| 155 | * @param refHeading origin ("false" north if different from {@code 0.0})
|
|---|
| 156 | * @return heading with respect to {@code refHeading} as origin
|
|---|
| 157 | * @since 18468
|
|---|
| 158 | */
|
|---|
| 159 | public double heading(EastNorth other, double refHeading) {
|
|---|
| 160 | double hd = heading(other);
|
|---|
| 161 | hd -= refHeading;
|
|---|
| 162 | if (hd < 0) {
|
|---|
| 163 | hd = 2 * Math.PI + hd;
|
|---|
| 164 | }
|
|---|
| 165 | return hd;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Replies true if east and north are different from Double.NaN and not infinite
|
|---|
| 170 | *
|
|---|
| 171 | * @return true if east and north are different from Double.NaN and not infinite
|
|---|
| 172 | */
|
|---|
| 173 | public boolean isValid() {
|
|---|
| 174 | return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Returns an EastNorth representing this EastNorth rotated around
|
|---|
| 179 | * a given EastNorth by a given angle
|
|---|
| 180 | * @param pivot the center of the rotation
|
|---|
| 181 | * @param angle the angle of the rotation
|
|---|
| 182 | * @return EastNorth rotated object
|
|---|
| 183 | */
|
|---|
| 184 | public EastNorth rotate(EastNorth pivot, double angle) {
|
|---|
| 185 | double cosPhi = Math.cos(angle);
|
|---|
| 186 | double sinPhi = Math.sin(angle);
|
|---|
| 187 | double x = east() - pivot.east();
|
|---|
| 188 | double y = north() - pivot.north();
|
|---|
| 189 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
|---|
| 190 | double nx = cosPhi * x + sinPhi * y + pivot.east();
|
|---|
| 191 | double ny = -sinPhi * x + cosPhi * y + pivot.north();
|
|---|
| 192 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
|---|
| 193 | return new EastNorth(nx, ny);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | @Override
|
|---|
| 197 | public String toString() {
|
|---|
| 198 | return "EastNorth[e="+x+", n="+y+']';
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * Compares two EastNorth values
|
|---|
| 203 | * @param other other east.north
|
|---|
| 204 | * @param e epsilon
|
|---|
| 205 | *
|
|---|
| 206 | * @return true if "x" and "y" values are within epsilon {@code e} of each other
|
|---|
| 207 | */
|
|---|
| 208 | public boolean equalsEpsilon(EastNorth other, double e) {
|
|---|
| 209 | return Math.abs(x - other.x) < e && Math.abs(y - other.y) < e;
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|