Added an attribute to each yacht: 'DistanceToNextMark'

This attribute is calculated at each update of the boat as prompted by the game state regularly
Removed the lat and lng attribute from the Yacht class and replaced its usage with the GeoPoint object instead

Removed redundant test files and merged GeoUtility and testGeoUtil test classes into one

tags: #story[1124] #pair[hyi25, wmu16]
This commit is contained in:
William Muir
2017-08-03 16:29:12 +12:00
parent db078538ff
commit 454e9ac9f1
7 changed files with 218 additions and 234 deletions
+35 -11
View File
@@ -1,7 +1,5 @@
package seng302.model;
import static seng302.utilities.GeoUtility.getGeoCoordinate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -14,6 +12,8 @@ import javafx.beans.property.ReadOnlyLongWrapper;
import javafx.scene.paint.Color;
import seng302.gameServer.GameState;
import seng302.model.mark.CompoundMark;
import seng302.model.mark.Mark;
import seng302.utilities.GeoUtility;
/**
* Yacht class for the racing boat.
@@ -38,11 +38,10 @@ public class Yacht {
private Long estimateTimeAtFinish;
private Long timeTillNext;
private Double distanceToNextMark;
private Long markRoundTime;
private CompoundMark nextMark;
private Double heading;
private Double lat;
private Double lon;
private Integer legNumber = 0;
//SERVER SIDE
@@ -110,7 +109,28 @@ public class Yacht {
}
Double metersCovered = velocity * secondsElapsed;
location = getGeoCoordinate(location, heading, metersCovered);
location = GeoUtility.getGeoCoordinate(location, heading, metersCovered);
distanceToNextMark = calcDistanceToNextMark();
}
/**
* Calculates the distance to the next mark (closest of the two if a gate mark).
*
* @return A distance in metres. Returns -1 if there is no next mark
*/
public Double calcDistanceToNextMark() {
if (nextMark == null) {
return -1d;
} else if (nextMark.isGate()) {
Mark sub1 = nextMark.getSubMark(1);
Mark sub2 = nextMark.getSubMark(2);
Double distance1 = GeoUtility.getDistance(location, sub1);
Double distance2 = GeoUtility.getDistance(location, sub2);
return (distance1 < distance2) ? distance1 : distance2;
} else {
return GeoUtility.getDistance(location, nextMark.getSubMark(1));
}
}
public void adjustHeading(Double amount) {
@@ -326,19 +346,19 @@ public class Yacht {
}
public Double getLat() {
return lat;
return location.getLat();
}
public void setLat(Double lat) {
this.lat = lat;
location.setLat(lat);
}
public Double getLon() {
return lon;
return location.getLng();
}
public void setLon(Double lon) {
this.lon = lon;
location.setLng(lon);
}
public Double getHeading() {
@@ -392,9 +412,13 @@ public class Yacht {
this.velocity = velocity;
}
public Double getDistanceToNextMark() {
return distanceToNextMark;
}
public void updateLocation (double lat, double lon, double heading, double velocity) {
this.lat = lat;
this.lon = lon;
location.setLat(lat);
location.setLng(lon);
this.heading = heading;
this.velocity = velocity;
updateVelocityProperty(velocity);
-35
View File
@@ -1,35 +0,0 @@
//package seng302;
//
//import org.junit.Test;
//import seng302.model.Boat;
//
//import static org.junit.Assert.assertEquals;
//
///**
// * Unit test for the Team class.
// */
//public class BoatTest {
//
// @Test
// public void testBoatCreation() {
// Boat boat1 = new Boat("Team 1");
// assertEquals(boat1.getTeamName(), "Team 1");
// assertEquals(boat1.getVelocity(), (double) 10.0, 1e-15);
// }
//
// @Test
// public void testChangeTeamName() {
// Boat boat1 = new Boat("Team 1");
// boat1.setTeamName("Team 2");
// assertEquals(boat1.getTeamName(), "Team 2");
// }
//
// @Test
// public void testSetVelocity() {
// Boat boat1 = new Boat("Team 1", 29.0, "", 100);
// assertEquals(boat1.getVelocity(), (double) 29.0, 1e-15);
//
// boat1.setVelocity(12.0);
// assertEquals(boat1.getVelocity(), (double)12.0, 1e-15);
// }
//}
-67
View File
@@ -1,67 +0,0 @@
package seng302;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import javafx.geometry.Point2D;
import org.junit.Before;
import org.junit.Test;
import seng302.utilities.GeoUtility;
/**
* Test Class for the GeometryUtils class
* Created by wmu16 on 24/05/17.
*/
public class TestGeoUtils {
//Line in x = y
private Point2D linePoint1 = new Point2D(0, 0);
private Point2D linePoint2 = new Point2D(1, 1);
//Point below x = y
private Point2D arbitraryPoint1 = new Point2D(1, 0);
//Point above x = y
private Point2D arbitraryPoint2 = new Point2D(0, 1);
//Point on x = y
private Point2D arbitraryPoint3 = new Point2D(2, 2);
@Before
public void setUp() throws Exception {
}
@Test
public void testLineFunction() {
Integer lineFunctionResult1 = GeoUtility.lineFunction(linePoint1, linePoint2, arbitraryPoint1);
Integer lineFunctionResult2 = GeoUtility.lineFunction(linePoint1, linePoint2, arbitraryPoint2);
Integer lineFunctionResult3 = GeoUtility.lineFunction(linePoint1, linePoint2, arbitraryPoint3);
//Point1 and Point2 are on opposite sides
assertEquals(Math.abs(lineFunctionResult1), Math.abs(lineFunctionResult2));
assertNotEquals(lineFunctionResult1, lineFunctionResult2);
//Point3 is on the line
assertEquals((long) lineFunctionResult3, 0L);
}
@Test
public void testMakeArbitraryVectorPoint() {
//Make a point (1,0) from point (0,0)
Point2D newPoint = GeoUtility.makeArbitraryVectorPoint(linePoint1, 0d, 1d);
Point2D expected = new Point2D(1,0);
assertEquals(expected.getX(), newPoint.getX(), 1E-6);
assertEquals(expected.getY(), newPoint.getY(), 1E-6);
newPoint = GeoUtility.makeArbitraryVectorPoint(linePoint1, 90d, 1d);
expected = new Point2D(0, 1);
assertEquals(expected.getX(), newPoint.getX(), 1E-6);
assertEquals(expected.getY(), newPoint.getY(), 1E-6);
}
}
@@ -1,76 +0,0 @@
package seng302.gameServer.server.simulator;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import seng302.model.GeoPoint;
import seng302.utilities.GeoUtility;
/**
* To test methods in GeoUtility.
* Created by Haoming on 28/04/17.
*/
public class GeoUtilityTest {
private GeoPoint p1 = new GeoPoint(57.670333, 11.827833);
private GeoPoint p2 = new GeoPoint(57.671524, 11.844495);
private GeoPoint p3 = new GeoPoint(57.670822, 11.843392);
private GeoPoint p4 = new GeoPoint(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 {
GeoPoint 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);
}
}
@@ -0,0 +1,55 @@
package seng302.model;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import seng302.model.mark.CompoundMark;
import seng302.model.mark.Mark;
/**
* Use this link to test geo distances
* http://www.csgnetwork.com/gpsdistcalc.html
* Created by wmu16 on 3/08/17.
*/
public class YachtTest {
private Yacht yacht;
private CompoundMark compoundMark;
private Double toleranceRatio = 0.01;
private GeoPoint p1 = new GeoPoint(57.670333, 11.827833);
private GeoPoint p2 = new GeoPoint(57.671524, 11.844495);
private GeoPoint p3 = new GeoPoint(57.670822, 11.843392);
private GeoPoint p4 = new GeoPoint(25.694829, 98.392049);
@Before
public void setup() {
yacht = new Yacht("Yacht",
0,
"0",
"WillIsCool",
"HaomingIsOk",
"NZL");
yacht.setLat(57.670333);
yacht.setLon(11.827833);
compoundMark = new CompoundMark(0, "HaomingsMark");
Mark subMark1 = new Mark("H", 57.671524, 11.844495, 0);
Mark subMark2 = new Mark("H", 57.670822, 11.843392, 0);
compoundMark.addSubMarks(subMark1, subMark2);
yacht.setNextMark(compoundMark);
}
@Test
public void testDistanceToNextMark() {
Double actual, expected;
actual = yacht.calcDistanceToNextMark();
expected = 927d;
assertEquals(expected, actual, expected * toleranceRatio);
}
}
@@ -1,45 +0,0 @@
//package seng302.model.mark;
//
//import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
//
//import org.junit.Before;
//import org.junit.Test;
//
///**
// * Created by Haoming on 17/3/17.
// */
//public class MarkTest {
//
// private SingleMark singleMark1;
// private SingleMark singleMark2;
// private GateMark gateMark;
//
// @Before
// public void setUp() throws Exception {
// this.singleMark1 = new SingleMark("testMark_SM1", 12.23234, -34.342, 1, 0);
// this.singleMark2 = new SingleMark("testMark_SM2", 12.23239, -34.352, 2, 1);
// this.gateMark = new GateMark("testMark_GM", MarkType.OPEN_GATE, singleMark1, singleMark2, singleMark1.getLatitude(), singleMark2.getLongitude(), 2);
// }
//
// @Test
// public void getName() throws Exception {
// assertEquals("testMark_SM1", this.singleMark1.getName());
// assertEquals("testMark_GM", this.gateMark.getName());
// }
//
// @Test
// public void getMarkType() throws Exception {
// assertTrue(this.singleMark2.getMarkType() == MarkType.SINGLE_MARK);
// assertTrue(this.gateMark.getMarkType() == MarkType.OPEN_GATE);
// }
//
// @Test
// public void getMarkContent() throws Exception {
// assertEquals(12.23234, this.singleMark1.getLatitude(), 1e-10);
// assertEquals(-34.342, this.singleMark1.getLongitude(), 1e-10);
// assertEquals("testMark_SM1", this.gateMark.getSingleMark1().getName());
// assertEquals(-34.352, this.gateMark.getSingleMark2().getLongitude(), 1e-10);
// }
//
//}
@@ -0,0 +1,128 @@
package seng302.utilities;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import javafx.geometry.Point2D;
import org.junit.Before;
import org.junit.Test;
import seng302.model.GeoPoint;
import seng302.utilities.GeoUtility;
/**
* To test methods in GeoUtility.
* Use this site to calculate distances
* https://rechneronline.de/geo-coordinates/#distance
* Created by Haoming on 28/04/17.
*/
public class GeoUtilityTest {
//Line in x = y
private Point2D linePoint1 = new Point2D(0, 0);
private Point2D linePoint2 = new Point2D(1, 1);
private Point2D arbitraryPoint1 = new Point2D(1, 0); //Point below x = y
private Point2D arbitraryPoint2 = new Point2D(0, 1); //Point above x = y
private Point2D arbitraryPoint3 = new Point2D(2, 2); //Point on x = y
private GeoPoint p1 = new GeoPoint(57.670333, 11.827833);
private GeoPoint p2 = new GeoPoint(57.671524, 11.844495);
private GeoPoint p3 = new GeoPoint(57.670822, 11.843392);
private GeoPoint p4 = new GeoPoint(25.694829, 98.392049);
private double toleranceRate = 0.01;
@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 {
GeoPoint 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);
}
@Test
public void testGetDistance() 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 testLineFunction() {
Integer lineFunctionResult1 = GeoUtility
.lineFunction(linePoint1, linePoint2, arbitraryPoint1);
Integer lineFunctionResult2 = GeoUtility
.lineFunction(linePoint1, linePoint2, arbitraryPoint2);
Integer lineFunctionResult3 = GeoUtility
.lineFunction(linePoint1, linePoint2, arbitraryPoint3);
//Point1 and Point2 are on opposite sides
assertEquals(Math.abs(lineFunctionResult1), Math.abs(lineFunctionResult2));
assertNotEquals(lineFunctionResult1, lineFunctionResult2);
//Point3 is on the line
assertEquals((long) lineFunctionResult3, 0L);
}
@Test
public void testMakeArbitraryVectorPoint() {
//Make a point (1,0) from point (0,0)
Point2D newPoint = GeoUtility.makeArbitraryVectorPoint(linePoint1, 0d, 1d);
Point2D expected = new Point2D(1, 0);
assertEquals(expected.getX(), newPoint.getX(), 1E-6);
assertEquals(expected.getY(), newPoint.getY(), 1E-6);
newPoint = GeoUtility.makeArbitraryVectorPoint(linePoint1, 90d, 1d);
expected = new Point2D(0, 1);
assertEquals(expected.getX(), newPoint.getX(), 1E-6);
assertEquals(expected.getY(), newPoint.getY(), 1E-6);
}
}