[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm.data.coor;
|
---|
[477] | 3 |
|
---|
[626] | 4 | /**
|
---|
[655] | 5 | * Northing, Easting of the projected coordinates.
|
---|
[1169] | 6 | *
|
---|
[626] | 7 | * This class is immutable.
|
---|
[1169] | 8 | *
|
---|
[626] | 9 | * @author Imi
|
---|
| 10 | */
|
---|
| 11 | public class EastNorth extends Coordinate {
|
---|
| 12 |
|
---|
[1169] | 13 | public EastNorth(double east, double north) {
|
---|
| 14 | super(east,north);
|
---|
| 15 | }
|
---|
[626] | 16 |
|
---|
[1169] | 17 | public double east() {
|
---|
| 18 | return x;
|
---|
| 19 | }
|
---|
[626] | 20 |
|
---|
[1169] | 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 interpolate(EastNorth en2, double proportion) {
|
---|
| 30 | return new EastNorth(this.x + proportion * (en2.x - this.x),
|
---|
[1076] | 31 | this.y + proportion * (en2.y - this.y));
|
---|
[1169] | 32 | }
|
---|
| 33 |
|
---|
[1724] | 34 | public EastNorth getCenter(EastNorth en2) {
|
---|
| 35 | return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[1076] | 38 | /**
|
---|
[1169] | 39 | * Returns the heading, in radians, that you have to use to get from
|
---|
[1076] | 40 | * this EastNorth to another. Heading is mapped into [0, 2pi)
|
---|
[1169] | 41 | *
|
---|
[1076] | 42 | * @param other the "destination" position
|
---|
[1169] | 43 | * @return heading
|
---|
[1076] | 44 | */
|
---|
| 45 | public double heading(EastNorth other) {
|
---|
| 46 | double hd = Math.atan2(other.east() - east(), other.north() - north());
|
---|
| 47 | if(hd < 0) hd = 2 * Math.PI + hd;
|
---|
[1169] | 48 | return hd;
|
---|
[1076] | 49 | }
|
---|
[1169] | 50 |
|
---|
[1076] | 51 | public EastNorth sub(EastNorth en) {
|
---|
| 52 | return new EastNorth(en.east() - east(), en.north() - north());
|
---|
| 53 | }
|
---|
[1169] | 54 |
|
---|
[1076] | 55 | /**
|
---|
[1209] | 56 | * Returns an EastNorth representing the this EastNorth rotated around
|
---|
[1076] | 57 | * a given EastNorth by a given angle
|
---|
| 58 | * @param pivot the center of the rotation
|
---|
| 59 | * @param angle the angle of the rotation
|
---|
[1169] | 60 | * @return EastNorth rotated object
|
---|
[1076] | 61 | */
|
---|
| 62 | public EastNorth rotate(EastNorth pivot, double angle) {
|
---|
| 63 | double cosPhi = Math.cos(angle);
|
---|
| 64 | double sinPhi = Math.sin(angle);
|
---|
| 65 | double x = east() - pivot.east();
|
---|
| 66 | double y = north() - pivot.north();
|
---|
| 67 | double nx = cosPhi * x + sinPhi * y + pivot.east();
|
---|
| 68 | double ny = -sinPhi * x + cosPhi * y + pivot.north();
|
---|
| 69 | return new EastNorth(nx, ny);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[1169] | 72 | @Override public String toString() {
|
---|
| 73 | return "EastNorth[e="+x+", n="+y+"]";
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[1076] | 76 | /**
|
---|
| 77 | * Compares two EastNorth values
|
---|
| 78 | *
|
---|
| 79 | * @return true if "x" and "y" values are within 1E-6 of each other
|
---|
| 80 | */
|
---|
| 81 | public boolean equalsEpsilon(EastNorth other, double e) {
|
---|
| 82 | return (Math.abs(x - other.x) < e && Math.abs(y - other.y) < e);
|
---|
| 83 | }
|
---|
[626] | 84 | }
|
---|