Began implementing function to calculate boat's new coordinates

-Using haversine function
-current values being produced are too small

#implement #story[9]
main
Erika Savell 9 years ago
parent 2551dd7362
commit 8090108b12

@ -28,15 +28,29 @@ public class ConstantVelocityRace extends Race {
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg(); double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
boat.setDistanceTravelledInLeg(totalDistanceTravelled); boat.setDistanceTravelledInLeg(totalDistanceTravelled);
boat.setCurrentPosition(calculate_position(totalDistanceTravelled, boat.calculateHeading())); boat.setCurrentPosition(calculate_position(boat.getCurrentLeg().getStartGraphCoordinate(),
totalDistanceTravelled, boat.calculateHeading()));
//Calculate new coordinates based on boat's heading for the leg, and distance traveled //Calculate new coordinates based on boat's heading for the leg, and distance traveled
} }
protected GPSCoordinate calculate_position(double distanceTravelled, double heading) { protected static GPSCoordinate calculate_position(GPSCoordinate oldCoordinates, double distanceTravelled, double heading) {
//Find new coordinate using current heading and distance //Find new coordinate using current heading and distance
return new GPSCoordinate(-1, -1); double oldLatitude = oldCoordinates.getLatitude();
double EARTH_RADIUS = 6371; //km
double oldLongitude = oldCoordinates.getLongitude();
double angularDistance = distanceTravelled / EARTH_RADIUS;
double newLatitude = Math.asin( Math.sin(oldLatitude)*Math.cos(angularDistance) +
Math.cos(oldLatitude)*Math.sin(angularDistance)*Math.cos(heading) );
double newLongitude = oldLongitude + Math.atan2(Math.sin(heading)*Math.sin(angularDistance)*Math.cos(oldLatitude),
Math.cos(angularDistance)-Math.sin(oldLatitude)*Math.sin(newLatitude));
return new GPSCoordinate(newLatitude, newLongitude);
} }

@ -12,6 +12,7 @@ public abstract class Race {
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>(); protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
protected Leg[] legs; protected Leg[] legs;
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
/** /**

@ -0,0 +1,17 @@
package seng302.Model;
import org.junit.Test;
import seng302.GPSCoordinate;
/**
* Created by esa46 on 16/03/17.
*/
public class ConstantVelocityRaceTest {
@Test
public void travelling5nmNorthGivesCorrectNewCoordinates() {
GPSCoordinate oldPos = new GPSCoordinate(0, 0);
System.out.print(ConstantVelocityRace.calculate_position(oldPos, 1, 0).getLatitude());
}
}

@ -0,0 +1,42 @@
package seng302.Model;
import org.junit.Test;
import seng302.GPSCoordinate;
import seng302.Model.BoatInRace;
import seng302.Model.ConstantVelocityRace;
import seng302.Model.Leg;
import static org.junit.Assert.assertTrue;
/**
* Created by esa46 on 15/03/17.
*/
public class RaceTest {
@Test
public void singleBoatRaceRunsAndFinishes() {
BoatInRace boat = new BoatInRace("NZ", 2500);
BoatInRace[] boats = new BoatInRace[] {boat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(3, 4), 0);
Leg[] legs = new Leg[] {leg1};
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
race.run();
assertTrue(race.finishingBoats.size() == 1);
}
@Test
public void fasterBoatFinishesFirst() {
BoatInRace fasterBoat = new BoatInRace("NZ", 2800);
BoatInRace slowerBoat = new BoatInRace("AU", 1800);
BoatInRace[] boats = new BoatInRace[] {slowerBoat, fasterBoat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(3, 4), 0);
Leg[] legs = new Leg[] {leg1};
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
race.run();
assertTrue(race.finishingBoats.get(0).getName() == "NZ");
assertTrue(race.finishingBoats.get(1).getName() == "AU");
}
}

@ -1,24 +0,0 @@
package seng302;
import org.junit.Test;
import seng302.Model.BoatInRace;
import seng302.Model.ConstantVelocityRace;
import seng302.Model.Leg;
/**
* Created by esa46 on 15/03/17.
*/
public class RaceTest {
@Test
public void singleBoatRaceRunsAndFinishes(){
BoatInRace boat = new BoatInRace("NZ", 240);
BoatInRace[] boats = new BoatInRace[] {boat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(3, 4), 0);
Leg[] legs = new Leg[] {leg1};
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
race.run();
}
}
Loading…
Cancel
Save