From 1edb142e00e8c05a689cd02a5def94c1c51ab75a Mon Sep 17 00:00:00 2001
From: Michael Zangl <michael.zangl@student.kit.edu>
Date: Wed, 1 Jul 2015 14:13:16 +0200
Subject: [PATCH 2/8] Added documentation to EastNorth, changed x - y =
 y.sub(x) to x.subtract(y) and minor code style improvements.

---
 .../josm/actions/mapmode/ExtrudeAction.java        |  8 +--
 .../openstreetmap/josm/command/MoveCommand.java    |  2 +-
 .../openstreetmap/josm/data/coor/Coordinate.java   |  4 +-
 .../openstreetmap/josm/data/coor/EastNorth.java    | 57 ++++++++++++++++++----
 .../dialogs/relation/sort/RelationSortUtils.java   |  4 +-
 .../org/openstreetmap/josm/tools/GeometryTest.java | 11 ++---
 6 files changed, 61 insertions(+), 25 deletions(-)

diff --git a/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java b/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java
index 0a01887..d61bb61 100644
--- a/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java
+++ b/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java
@@ -473,8 +473,8 @@ public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
                 if (mode == Mode.extrude || mode == Mode.create_new) {
                     // nothing here
                 } else if (mode == Mode.translate) {
-                    EastNorth movement1 = initialN1en.sub(newN1en);
-                    EastNorth movement2 = initialN2en.sub(newN2en);
+                    EastNorth movement1 = newN1en.subtract(initialN1en);
+                    EastNorth movement2 = newN2en.subtract(initialN2en);
                     // move nodes to new position
                     if (moveCommand == null || moveCommand2 == null) {
                         // make a new move commands
@@ -733,7 +733,7 @@ public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
     private EastNorth calculateBestMovement(EastNorth mouseEn) {
 
         EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y);
-        EastNorth mouseMovement = initialMouseEn.sub(mouseEn);
+        EastNorth mouseMovement = mouseEn.subtract(initialMouseEn);
 
         double bestDistance = Double.POSITIVE_INFINITY;
         EastNorth bestMovement = null;
@@ -781,7 +781,7 @@ public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
             return null;
         else
             //return distance form base to target position
-            return intersectionPoint.sub(targetPos);
+            return targetPos.subtract(intersectionPoint);
     }
 
     /**
diff --git a/src/org/openstreetmap/josm/command/MoveCommand.java b/src/org/openstreetmap/josm/command/MoveCommand.java
index e26e599..23ab370 100644
--- a/src/org/openstreetmap/josm/command/MoveCommand.java
+++ b/src/org/openstreetmap/josm/command/MoveCommand.java
@@ -68,7 +68,7 @@ public class MoveCommand extends Command {
      * @param position The new location (lat/lon)
      */
     public MoveCommand(Node node, LatLon position) {
-        this(Collections.singleton((OsmPrimitive) node), node.getEastNorth().sub(Projections.project(position)));
+        this(Collections.singleton((OsmPrimitive) node), Projections.project(position).subtract(node.getEastNorth()));
     }
 
     /**
diff --git a/src/org/openstreetmap/josm/data/coor/Coordinate.java b/src/org/openstreetmap/josm/data/coor/Coordinate.java
index 5ec1c9d..dde18e1 100644
--- a/src/org/openstreetmap/josm/data/coor/Coordinate.java
+++ b/src/org/openstreetmap/josm/data/coor/Coordinate.java
@@ -60,9 +60,7 @@ abstract class Coordinate implements Serializable {
      * @since 6166
      */
     public final double distance(final double px, final double py) {
-        final double dx = this.x-px;
-        final double dy = this.y-py;
-        return Math.sqrt(dx*dx + dy*dy);
+        return Math.sqrt(distanceSq(px, py));
     }
 
     /**
diff --git a/src/org/openstreetmap/josm/data/coor/EastNorth.java b/src/org/openstreetmap/josm/data/coor/EastNorth.java
index 76df076..41c6417 100644
--- a/src/org/openstreetmap/josm/data/coor/EastNorth.java
+++ b/src/org/openstreetmap/josm/data/coor/EastNorth.java
@@ -24,25 +24,68 @@ public class EastNorth extends Coordinate {
         return y;
     }
 
-    public EastNorth add(double dx, double dy) {
-        return new EastNorth(x+dx, y+dy);
+    /**
+     * Adds an offset to this {@link EastNorth} instance and returns the result.
+     * @param dEast The offset to add in east direction.
+     * @param dNorth The offset to add in north direction.
+     * @return The result.
+     */
+    public EastNorth add(double dEast, double dNorth) {
+        return new EastNorth(east() + dEast, north() + dNorth);
     }
 
+    /**
+     * Adds the coordinates of an other EastNorth instance to this one.
+     * @param other The other instance.
+     * @return The new EastNorth position.
+     */
     public EastNorth add(EastNorth other) {
-        return new EastNorth(x+other.x, y+other.y);
+        return add(other.east(), other.north());
+    }
+
+    /**
+     * Subtracts the coordinates of this EastNorth instance from the other one.
+     * <p>
+     * This produces result = en2 - this.
+     * @param en2 The instance to subtract this one from.
+     * @return The new EastNorth position.
+     */
+    @Deprecated
+    public EastNorth sub(EastNorth en2) {
+        return en2.subtract(this);
+    }
+
+    /**
+     * Subtracts an east/north value from this point.
+     * @param other The other value to subtract from this.
+     * @return A point with the new coordinates.
+     */
+    public EastNorth subtract(EastNorth other) {
+        return add(-other.east(), -other.north());
     }
 
     public EastNorth scale(double s) {
         return new EastNorth(s * x, s * y);
     }
 
+    /**
+     * Does a linear interpolation between two EastNorth instances.
+     * @param en2 The other EstNort instance.
+     * @param proportion The proportion the other instance influences the result.
+     * @return The new {@link EastNorth} position.
+     */
     public EastNorth interpolate(EastNorth en2, double proportion) {
         return new EastNorth(this.x + proportion * (en2.x - this.x),
                 this.y + proportion * (en2.y - this.y));
     }
 
+    /**
+     * Gets the center between two {@link EastNorth} instances.
+     * @param en2 The other instance.
+     * @return The center between this and the other instance.
+     */
     public EastNorth getCenter(EastNorth en2) {
-        return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
+        return interpolate(en2, .5);
     }
 
     /**
@@ -73,7 +116,7 @@ public class EastNorth extends Coordinate {
      * @return length of this
      */
     public double length() {
-        return Math.sqrt(x*x + y*y);
+        return Math.hypot(east(), north());
     }
 
     /**
@@ -100,10 +143,6 @@ public class EastNorth extends Coordinate {
         return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
     }
 
-    public EastNorth sub(EastNorth en) {
-        return new EastNorth(en.east() - east(), en.north() - north());
-    }
-
     /**
      * Returns an EastNorth representing the this EastNorth rotated around
      * a given EastNorth by a given angle
diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSortUtils.java b/src/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSortUtils.java
index 642c35d..1504bf7 100644
--- a/src/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSortUtils.java
+++ b/src/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSortUtils.java
@@ -40,8 +40,8 @@ final class RelationSortUtils {
             EastNorth en2 = w.getNode(1).getEastNorth();
             EastNorth en3 = w.getNode(2).getEastNorth();
             if (en1 != null && en2 != null && en3 != null) {
-                en1 = en1.sub(en2);
-                en2 = en2.sub(en3);
+                en1 = en2.subtract(en1);
+                en2 = en3.subtract(en2);
                 return en1.north() * en2.east() - en2.north() * en1.east() > 0 ? ROUNDABOUT_LEFT : ROUNDABOUT_RIGHT;
             }
         }
diff --git a/test/unit/org/openstreetmap/josm/tools/GeometryTest.java b/test/unit/org/openstreetmap/josm/tools/GeometryTest.java
index b1c39bf..7731f8c 100644
--- a/test/unit/org/openstreetmap/josm/tools/GeometryTest.java
+++ b/test/unit/org/openstreetmap/josm/tools/GeometryTest.java
@@ -2,9 +2,8 @@
 package org.openstreetmap.josm.tools;
 
 import org.junit.Assert;
-import org.junit.Test;
 import org.junit.BeforeClass;
-
+import org.junit.Test;
 import org.openstreetmap.josm.JOSMFixture;
 import org.openstreetmap.josm.data.coor.EastNorth;
 
@@ -29,14 +28,14 @@ public class GeometryTest {
         EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7);
 
         EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4);
-        
-        EastNorth d1 = intersectionPoint.sub(p3);
-        EastNorth d2 = p2.sub(p1);
+
+        EastNorth d1 = p3.subtract(intersectionPoint);
+        EastNorth d2 = p1.subtract(p2);
         Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east();
         Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north();
         Double len1 = d1.length();
         Double len2 = d2.length();
-        
+
         Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint);
         Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint);
         Assert.assertTrue("intersection point not on line, angle: " + angle1,
-- 
1.9.1

