| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 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 | public EastNorth(double east, double north) { |
|---|
| 14 | super(east,north); |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | public double east() { |
|---|
| 18 | return x; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | public double north() { |
|---|
| 22 | return y; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | public EastNorth add(double dx, double dy) { |
|---|
| 26 | return new EastNorth(x+dx, y+dy); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public EastNorth add(EastNorth other) { |
|---|
| 30 | return new EastNorth(x+other.x, y+other.y); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | public EastNorth scale(double s) { |
|---|
| 34 | return new EastNorth(s * x, s * y); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public EastNorth interpolate(EastNorth en2, double proportion) { |
|---|
| 38 | return new EastNorth(this.x + proportion * (en2.x - this.x), |
|---|
| 39 | this.y + proportion * (en2.y - this.y)); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | public EastNorth getCenter(EastNorth en2) { |
|---|
| 43 | return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public double distance(EastNorth en2) { |
|---|
| 47 | return Math.sqrt((this.x-en2.x)*(this.x-en2.x) + (this.y-en2.y)*(this.y-en2.y)); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Returns the heading, in radians, that you have to use to get from |
|---|
| 52 | * this EastNorth to another. Heading is mapped into [0, 2pi) |
|---|
| 53 | * |
|---|
| 54 | * @param other the "destination" position |
|---|
| 55 | * @return heading |
|---|
| 56 | */ |
|---|
| 57 | public double heading(EastNorth other) { |
|---|
| 58 | double hd = Math.atan2(other.east() - east(), other.north() - north()); |
|---|
| 59 | if(hd < 0) hd = 2 * Math.PI + hd; |
|---|
| 60 | return hd; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Replies true if east and north are different from Double.NaN |
|---|
| 65 | * |
|---|
| 66 | * @return true if east and north are different from Double.NaN |
|---|
| 67 | */ |
|---|
| 68 | public boolean isValid() { |
|---|
| 69 | return !java.lang.Double.isNaN(x) && !java.lang.Double.isNaN(y); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public EastNorth sub(EastNorth en) { |
|---|
| 73 | return new EastNorth(en.east() - east(), en.north() - north()); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Returns an EastNorth representing the this EastNorth rotated around |
|---|
| 78 | * a given EastNorth by a given angle |
|---|
| 79 | * @param pivot the center of the rotation |
|---|
| 80 | * @param angle the angle of the rotation |
|---|
| 81 | * @return EastNorth rotated object |
|---|
| 82 | */ |
|---|
| 83 | public EastNorth rotate(EastNorth pivot, double angle) { |
|---|
| 84 | double cosPhi = Math.cos(angle); |
|---|
| 85 | double sinPhi = Math.sin(angle); |
|---|
| 86 | double x = east() - pivot.east(); |
|---|
| 87 | double y = north() - pivot.north(); |
|---|
| 88 | double nx = cosPhi * x + sinPhi * y + pivot.east(); |
|---|
| 89 | double ny = -sinPhi * x + cosPhi * y + pivot.north(); |
|---|
| 90 | return new EastNorth(nx, ny); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | @Override public String toString() { |
|---|
| 94 | return "EastNorth[e="+x+", n="+y+"]"; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Compares two EastNorth values |
|---|
| 99 | * |
|---|
| 100 | * @return true if "x" and "y" values are within 1E-6 of each other |
|---|
| 101 | */ |
|---|
| 102 | public boolean equalsEpsilon(EastNorth other, double e) { |
|---|
| 103 | return (Math.abs(x - other.x) < e && Math.abs(y - other.y) < e); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|