001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.coor; 003 004/** 005 * Northing, Easting of the projected coordinates. 006 * 007 * This class is immutable. 008 * 009 * @author Imi 010 */ 011public class EastNorth extends Coordinate { 012 013 private static final long serialVersionUID = 1L; 014 015 /** 016 * A zero constant 017 */ 018 public static final EastNorth ZERO = new EastNorth(0, 0); 019 020 /** 021 * Constructs a new {@code EastNorth}. 022 * @param east easting 023 * @param north northing 024 */ 025 public EastNorth(double east, double north) { 026 super(east, north); 027 } 028 029 /** 030 * Returns easting. 031 * @return easting 032 */ 033 public double east() { 034 return x; 035 } 036 037 /** 038 * Returns northing. 039 * @return northing 040 */ 041 public double north() { 042 return y; 043 } 044 045 /** 046 * Adds an offset to this {@link EastNorth} instance and returns the result. 047 * @param dEast The offset to add in east direction. 048 * @param dNorth The offset to add in north direction. 049 * @return The result. 050 */ 051 public EastNorth add(double dEast, double dNorth) { 052 return new EastNorth(east()+dEast, north()+dNorth); 053 } 054 055 /** 056 * Adds the coordinates of an other EastNorth instance to this one. 057 * @param other The other instance. 058 * @return The new EastNorth position. 059 */ 060 public EastNorth add(EastNorth other) { 061 return new EastNorth(x+other.x, y+other.y); 062 } 063 064 /** 065 * Subtracts an east/north value from this point. 066 * @param other The other value to subtract from this. 067 * @return A point with the new coordinates. 068 */ 069 public EastNorth subtract(EastNorth other) { 070 return new EastNorth(x-other.x, y-other.y); 071 } 072 073 /** 074 * Scales this {@link EastNorth} instance to a given factor and returns the result. 075 * @param s factor 076 * @return The result. 077 */ 078 public EastNorth scale(double s) { 079 return new EastNorth(s * x, s * y); 080 } 081 082 /** 083 * Does a linear interpolation between two EastNorth instances. 084 * @param en2 The other EstNort instance. 085 * @param proportion The proportion the other instance influences the result. 086 * @return The new {@link EastNorth} position. 087 */ 088 public EastNorth interpolate(EastNorth en2, double proportion) { 089 // this is an alternate form of this.x + proportion * (en2.x - this.x) that is slightly faster 090 return new EastNorth((1 - proportion) * this.x + proportion * en2.x, 091 (1 - proportion) * this.y + proportion * en2.y); 092 } 093 094 /** 095 * Gets the center between two {@link EastNorth} instances. 096 * @param en2 The other instance. 097 * @return The center between this and the other instance. 098 */ 099 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 * Replies true if east and north are different from Double.NaN and not infinite 152 * 153 * @return true if east and north are different from Double.NaN and not infinite 154 */ 155 public boolean isValid() { 156 return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y); 157 } 158 159 /** 160 * Returns an EastNorth representing the this EastNorth rotated around 161 * a given EastNorth by a given angle 162 * @param pivot the center of the rotation 163 * @param angle the angle of the rotation 164 * @return EastNorth rotated object 165 */ 166 public EastNorth rotate(EastNorth pivot, double angle) { 167 double cosPhi = Math.cos(angle); 168 double sinPhi = Math.sin(angle); 169 double x = east() - pivot.east(); 170 double y = north() - pivot.north(); 171 // CHECKSTYLE.OFF: SingleSpaceSeparator 172 double nx = cosPhi * x + sinPhi * y + pivot.east(); 173 double ny = -sinPhi * x + cosPhi * y + pivot.north(); 174 // CHECKSTYLE.ON: SingleSpaceSeparator 175 return new EastNorth(nx, ny); 176 } 177 178 @Override 179 public String toString() { 180 return "EastNorth[e="+x+", n="+y+']'; 181 } 182 183 /** 184 * Compares two EastNorth values 185 * @param other other east.north 186 * @param e epsilon 187 * 188 * @return true if "x" and "y" values are within epsilon {@code e} of each other 189 */ 190 public boolean equalsEpsilon(EastNorth other, double e) { 191 return Math.abs(x - other.x) < e && Math.abs(y - other.y) < e; 192 } 193}