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
|
b
|
public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
|
473 | 473 | if (mode == Mode.extrude || mode == Mode.create_new) { |
474 | 474 | // nothing here |
475 | 475 | } else if (mode == Mode.translate) { |
476 | | EastNorth movement1 = initialN1en.sub(newN1en); |
477 | | EastNorth movement2 = initialN2en.sub(newN2en); |
| 476 | EastNorth movement1 = newN1en.subtract(initialN1en); |
| 477 | EastNorth movement2 = newN2en.subtract(initialN2en); |
478 | 478 | // move nodes to new position |
479 | 479 | if (moveCommand == null || moveCommand2 == null) { |
480 | 480 | // make a new move commands |
… |
… |
public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
|
733 | 733 | private EastNorth calculateBestMovement(EastNorth mouseEn) { |
734 | 734 | |
735 | 735 | EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y); |
736 | | EastNorth mouseMovement = initialMouseEn.sub(mouseEn); |
| 736 | EastNorth mouseMovement = mouseEn.subtract(initialMouseEn); |
737 | 737 | |
738 | 738 | double bestDistance = Double.POSITIVE_INFINITY; |
739 | 739 | EastNorth bestMovement = null; |
… |
… |
public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPress
|
781 | 781 | return null; |
782 | 782 | else |
783 | 783 | //return distance form base to target position |
784 | | return intersectionPoint.sub(targetPos); |
| 784 | return targetPos.subtract(intersectionPoint); |
785 | 785 | } |
786 | 786 | |
787 | 787 | /** |
diff --git a/src/org/openstreetmap/josm/command/MoveCommand.java b/src/org/openstreetmap/josm/command/MoveCommand.java
index e26e599..23ab370 100644
a
|
b
|
public class MoveCommand extends Command {
|
68 | 68 | * @param position The new location (lat/lon) |
69 | 69 | */ |
70 | 70 | public MoveCommand(Node node, LatLon position) { |
71 | | this(Collections.singleton((OsmPrimitive) node), node.getEastNorth().sub(Projections.project(position))); |
| 71 | this(Collections.singleton((OsmPrimitive) node), Projections.project(position).subtract(node.getEastNorth())); |
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
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
|
b
|
abstract class Coordinate implements Serializable {
|
60 | 60 | * @since 6166 |
61 | 61 | */ |
62 | 62 | public final double distance(final double px, final double py) { |
63 | | final double dx = this.x-px; |
64 | | final double dy = this.y-py; |
65 | | return Math.sqrt(dx*dx + dy*dy); |
| 63 | return Math.sqrt(distanceSq(px, py)); |
66 | 64 | } |
67 | 65 | |
68 | 66 | /** |
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
|
b
|
public class EastNorth extends Coordinate {
|
24 | 24 | return y; |
25 | 25 | } |
26 | 26 | |
27 | | public EastNorth add(double dx, double dy) { |
28 | | return new EastNorth(x+dx, y+dy); |
| 27 | /** |
| 28 | * Adds an offset to this {@link EastNorth} instance and returns the result. |
| 29 | * @param dEast The offset to add in east direction. |
| 30 | * @param dNorth The offset to add in north direction. |
| 31 | * @return The result. |
| 32 | */ |
| 33 | public EastNorth add(double dEast, double dNorth) { |
| 34 | return new EastNorth(east() + dEast, north() + dNorth); |
29 | 35 | } |
30 | 36 | |
| 37 | /** |
| 38 | * Adds the coordinates of an other EastNorth instance to this one. |
| 39 | * @param other The other instance. |
| 40 | * @return The new EastNorth position. |
| 41 | */ |
31 | 42 | public EastNorth add(EastNorth other) { |
32 | | return new EastNorth(x+other.x, y+other.y); |
| 43 | return add(other.east(), other.north()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Subtracts the coordinates of this EastNorth instance from the other one. |
| 48 | * <p> |
| 49 | * This produces result = en2 - this. |
| 50 | * @param en2 The instance to subtract this one from. |
| 51 | * @return The new EastNorth position. |
| 52 | */ |
| 53 | @Deprecated |
| 54 | public EastNorth sub(EastNorth en2) { |
| 55 | return en2.subtract(this); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Subtracts an east/north value from this point. |
| 60 | * @param other The other value to subtract from this. |
| 61 | * @return A point with the new coordinates. |
| 62 | */ |
| 63 | public EastNorth subtract(EastNorth other) { |
| 64 | return add(-other.east(), -other.north()); |
33 | 65 | } |
34 | 66 | |
35 | 67 | public EastNorth scale(double s) { |
36 | 68 | return new EastNorth(s * x, s * y); |
37 | 69 | } |
38 | 70 | |
| 71 | /** |
| 72 | * Does a linear interpolation between two EastNorth instances. |
| 73 | * @param en2 The other EstNort instance. |
| 74 | * @param proportion The proportion the other instance influences the result. |
| 75 | * @return The new {@link EastNorth} position. |
| 76 | */ |
39 | 77 | public EastNorth interpolate(EastNorth en2, double proportion) { |
40 | 78 | return new EastNorth(this.x + proportion * (en2.x - this.x), |
41 | 79 | this.y + proportion * (en2.y - this.y)); |
42 | 80 | } |
43 | 81 | |
| 82 | /** |
| 83 | * Gets the center between two {@link EastNorth} instances. |
| 84 | * @param en2 The other instance. |
| 85 | * @return The center between this and the other instance. |
| 86 | */ |
44 | 87 | public EastNorth getCenter(EastNorth en2) { |
45 | | return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0); |
| 88 | return interpolate(en2, .5); |
46 | 89 | } |
47 | 90 | |
48 | 91 | /** |
… |
… |
public class EastNorth extends Coordinate {
|
73 | 116 | * @return length of this |
74 | 117 | */ |
75 | 118 | public double length() { |
76 | | return Math.sqrt(x*x + y*y); |
| 119 | return Math.hypot(east(), north()); |
77 | 120 | } |
78 | 121 | |
79 | 122 | /** |
… |
… |
public class EastNorth extends Coordinate {
|
100 | 143 | return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y); |
101 | 144 | } |
102 | 145 | |
103 | | public EastNorth sub(EastNorth en) { |
104 | | return new EastNorth(en.east() - east(), en.north() - north()); |
105 | | } |
106 | | |
107 | 146 | /** |
108 | 147 | * Returns an EastNorth representing the this EastNorth rotated around |
109 | 148 | * 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
|
b
|
final class RelationSortUtils {
|
40 | 40 | EastNorth en2 = w.getNode(1).getEastNorth(); |
41 | 41 | EastNorth en3 = w.getNode(2).getEastNorth(); |
42 | 42 | if (en1 != null && en2 != null && en3 != null) { |
43 | | en1 = en1.sub(en2); |
44 | | en2 = en2.sub(en3); |
| 43 | en1 = en2.subtract(en1); |
| 44 | en2 = en3.subtract(en2); |
45 | 45 | return en1.north() * en2.east() - en2.north() * en1.east() > 0 ? ROUNDABOUT_LEFT : ROUNDABOUT_RIGHT; |
46 | 46 | } |
47 | 47 | } |
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
|
b
|
|
2 | 2 | package org.openstreetmap.josm.tools; |
3 | 3 | |
4 | 4 | import org.junit.Assert; |
5 | | import org.junit.Test; |
6 | 5 | import org.junit.BeforeClass; |
7 | | |
| 6 | import org.junit.Test; |
8 | 7 | import org.openstreetmap.josm.JOSMFixture; |
9 | 8 | import org.openstreetmap.josm.data.coor.EastNorth; |
10 | 9 | |
… |
… |
public class GeometryTest {
|
29 | 28 | EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7); |
30 | 29 | |
31 | 30 | EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4); |
32 | | |
33 | | EastNorth d1 = intersectionPoint.sub(p3); |
34 | | EastNorth d2 = p2.sub(p1); |
| 31 | |
| 32 | EastNorth d1 = p3.subtract(intersectionPoint); |
| 33 | EastNorth d2 = p1.subtract(p2); |
35 | 34 | Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east(); |
36 | 35 | Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north(); |
37 | 36 | Double len1 = d1.length(); |
38 | 37 | Double len2 = d2.length(); |
39 | | |
| 38 | |
40 | 39 | Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint); |
41 | 40 | Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint); |
42 | 41 | Assert.assertTrue("intersection point not on line, angle: " + angle1, |