diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 6efce9e7..caa4313e 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -24,6 +24,4 @@ public class App extends Application new Thread(new Simulator(1000)).run(); launch(args); } -} - - +} \ No newline at end of file diff --git a/src/main/java/seng302/server/simulator/GeoUtility.java b/src/main/java/seng302/server/simulator/GeoUtility.java index 288e0ee1..dff67e50 100644 --- a/src/main/java/seng302/server/simulator/GeoUtility.java +++ b/src/main/java/seng302/server/simulator/GeoUtility.java @@ -33,8 +33,14 @@ public class GeoUtility { * * @param p1 the first geographical position, start point * @param p2 the second geographical position, end point - * @return the bearing in degree from p1 to p2, value range (0 ~ 360 deg.). + * @return the initial bearing in degree from p1 to p2, value range (0 ~ 360 deg.). * vertical up is 0 deg. horizontal right is 90 deg. + * + * NOTE: + * The final bearing will differ from the initial bearing by varying degrees + * according to distance and latitude (if you were to go from say 35°N,45°E + * (≈ Baghdad) to 35°N,135°E (≈ Osaka), you would start on a heading of 60° + * and end up on a heading of 120° */ public static Double getBearing(Position p1, Position p2) { diff --git a/src/test/java/seng302/server/simulator/GeoUtilityTest.java b/src/test/java/seng302/server/simulator/GeoUtilityTest.java new file mode 100644 index 00000000..4cdf01df --- /dev/null +++ b/src/test/java/seng302/server/simulator/GeoUtilityTest.java @@ -0,0 +1,75 @@ +package seng302.server.simulator; + +import org.junit.Test; +import seng302.server.simulator.mark.Position; + +import static org.junit.Assert.*; + +/** + * To test methods in GeoUtility. + * Created by Haoming on 28/04/17. + */ +public class GeoUtilityTest { + + private Position p1 = new Position(57.670333, 11.827833); + private Position p2 = new Position(57.671524, 11.844495); + private Position p3 = new Position(57.670822, 11.843392); + private Position p4 = new Position(25.694829, 98.392049); + + private double toleranceRate = 0.01; + + @Test + public void getDistance() throws Exception { + double expected, actual; + + actual = GeoUtility.getDistance(p1, p2); + expected = 1000; + assertEquals(expected, actual, expected * toleranceRate); + + actual = GeoUtility.getDistance(p1, p3); + expected = 927; + assertEquals(expected, actual, expected * toleranceRate); + + actual = GeoUtility.getDistance(p2, p4); + expected = 7430180; + assertEquals(expected, actual, expected * toleranceRate); + } + + @Test + public void getBearing() throws Exception { + double expected, actual; + + actual = GeoUtility.getBearing(p1, p2); + expected = 82; + assertEquals(expected, actual, expected * toleranceRate); + + actual = GeoUtility.getBearing(p1, p3); + expected = 86; + assertEquals(expected, actual, expected * toleranceRate); + + actual = GeoUtility.getBearing(p2, p4); + expected = 78; + assertEquals(expected, actual, expected * toleranceRate); + } + + @Test + public void getGeoCoordinate() throws Exception { + Position expected, actual; + + actual = GeoUtility.getGeoCoordinate(p1, 82.0, 1000.0); + expected = p2; + assertEquals(expected.getLat(), actual.getLat(), expected.getLat() * toleranceRate); + assertEquals(expected.getLng(), actual.getLng(), expected.getLng() * toleranceRate); + + actual = GeoUtility.getGeoCoordinate(p1, 86.0, 927.0); + expected = p3; + assertEquals(expected.getLat(), actual.getLat(), expected.getLat() * toleranceRate); + assertEquals(expected.getLng(), actual.getLng(), expected.getLng() * toleranceRate); + + actual = GeoUtility.getGeoCoordinate(p2, 78.0, 7430180.0); + expected = p4; + assertEquals(expected.getLat(), actual.getLat(), expected.getLat() * toleranceRate); + assertEquals(expected.getLng(), actual.getLng(), expected.getLng() * toleranceRate); + } + +} \ No newline at end of file