Merge pull request #30 from PM2-IT21bWIN-ruiz-mach-krea/Game
refactoring of Method calculatePath from Game.java to Track.java
This commit is contained in:
commit
4ce26578b9
|
@ -271,63 +271,10 @@ public class Game implements GameSpecification {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<PositionVector> calculatePath(PositionVector startPosition, PositionVector endPosition) {
|
public List<PositionVector> calculatePath(PositionVector startPosition, PositionVector endPosition) {
|
||||||
ArrayList<PositionVector> pathList = new ArrayList<>();
|
return track.calculatePointsOnPath(startPosition, endPosition);
|
||||||
// Use Bresenham's algorithm to determine positions.
|
|
||||||
int x = startPosition.getX();
|
|
||||||
int y = startPosition.getY();
|
|
||||||
|
|
||||||
// Relative Distance (x & y axis) between end- and starting position
|
|
||||||
int diffX = endPosition.getX() - startPosition.getX();
|
|
||||||
int diffY = endPosition.getY() - startPosition.getY();
|
|
||||||
|
|
||||||
// Absolute distance (x & y axis) between end- and starting position
|
|
||||||
int distX = Math.abs(diffX);
|
|
||||||
int distY = Math.abs(diffY);
|
|
||||||
|
|
||||||
// Direction of vector on x & y axis (-1: to left/down, 0: none, +1 : to right/up)
|
|
||||||
int dirX = Integer.signum(diffX);
|
|
||||||
int dirY = Integer.signum(diffY);
|
|
||||||
|
|
||||||
// Determine which axis is the fast direction and set parallel/diagonal step values
|
|
||||||
int parallelStepX, parallelStepY;
|
|
||||||
int diagonalStepX, diagonalStepY;
|
|
||||||
int distanceSlowAxis, distanceFastAxis;
|
|
||||||
if (distX > distY) {
|
|
||||||
// x axis is the 'fast' direction
|
|
||||||
parallelStepX = dirX;
|
|
||||||
parallelStepY = 0; // parallel step only moves in x direction
|
|
||||||
diagonalStepX = dirX;
|
|
||||||
diagonalStepY = dirY; // diagonal step moves in both directions
|
|
||||||
distanceSlowAxis = distY;
|
|
||||||
distanceFastAxis = distX;
|
|
||||||
} else {
|
|
||||||
// y axis is the 'fast' direction
|
|
||||||
parallelStepX = 0;
|
|
||||||
parallelStepY = dirY; // parallel step only moves in y direction
|
|
||||||
diagonalStepX = dirX;
|
|
||||||
diagonalStepY = dirY; // diagonal step moves in both directions
|
|
||||||
distanceSlowAxis = distX;
|
|
||||||
distanceFastAxis = distY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int error = distanceFastAxis / 2;
|
|
||||||
for (int step = 0; step < distanceFastAxis; step++) {
|
|
||||||
error -= distanceSlowAxis;
|
|
||||||
if (error < 0) {
|
|
||||||
error += distanceFastAxis; // correct error value to be positive again
|
|
||||||
// step into slow direction; diagonal step
|
|
||||||
x += diagonalStepX;
|
|
||||||
y += diagonalStepY;
|
|
||||||
} else {
|
|
||||||
// step into fast direction; parallel step
|
|
||||||
x += parallelStepX;
|
|
||||||
y += parallelStepY;
|
|
||||||
}
|
|
||||||
|
|
||||||
pathList.add(new PositionVector(x, y));
|
|
||||||
}
|
|
||||||
return pathList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
||||||
List<PositionVector> path = calculatePath(start, finish);
|
List<PositionVector> path = calculatePath(start, finish);
|
||||||
|
|
|
@ -355,6 +355,65 @@ public class Track implements TrackSpecification {
|
||||||
return currentSpace.getValue();
|
return currentSpace.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<PositionVector> calculatePointsOnPath(PositionVector startPosition, PositionVector endPosition) {
|
||||||
|
ArrayList<PositionVector> pathList = new ArrayList<>();
|
||||||
|
// Use Bresenham's algorithm to determine positions.
|
||||||
|
int x = startPosition.getX();
|
||||||
|
int y = startPosition.getY();
|
||||||
|
|
||||||
|
// Relative Distance (x & y axis) between end- and starting position
|
||||||
|
int diffX = endPosition.getX() - startPosition.getX();
|
||||||
|
int diffY = endPosition.getY() - startPosition.getY();
|
||||||
|
|
||||||
|
// Absolute distance (x & y axis) between end- and starting position
|
||||||
|
int distX = Math.abs(diffX);
|
||||||
|
int distY = Math.abs(diffY);
|
||||||
|
|
||||||
|
// Direction of vector on x & y axis (-1: to left/down, 0: none, +1 : to right/up)
|
||||||
|
int dirX = Integer.signum(diffX);
|
||||||
|
int dirY = Integer.signum(diffY);
|
||||||
|
|
||||||
|
// Determine which axis is the fast direction and set parallel/diagonal step values
|
||||||
|
int parallelStepX, parallelStepY;
|
||||||
|
int diagonalStepX, diagonalStepY;
|
||||||
|
int distanceSlowAxis, distanceFastAxis;
|
||||||
|
if (distX > distY) {
|
||||||
|
// x axis is the 'fast' direction
|
||||||
|
parallelStepX = dirX;
|
||||||
|
parallelStepY = 0; // parallel step only moves in x direction
|
||||||
|
diagonalStepX = dirX;
|
||||||
|
diagonalStepY = dirY; // diagonal step moves in both directions
|
||||||
|
distanceSlowAxis = distY;
|
||||||
|
distanceFastAxis = distX;
|
||||||
|
} else {
|
||||||
|
// y axis is the 'fast' direction
|
||||||
|
parallelStepX = 0;
|
||||||
|
parallelStepY = dirY; // parallel step only moves in y direction
|
||||||
|
diagonalStepX = dirX;
|
||||||
|
diagonalStepY = dirY; // diagonal step moves in both directions
|
||||||
|
distanceSlowAxis = distX;
|
||||||
|
distanceFastAxis = distY;
|
||||||
|
}
|
||||||
|
|
||||||
|
int error = distanceFastAxis / 2;
|
||||||
|
for (int step = 0; step < distanceFastAxis; step++) {
|
||||||
|
error -= distanceSlowAxis;
|
||||||
|
if (error < 0) {
|
||||||
|
error += distanceFastAxis; // correct error value to be positive again
|
||||||
|
// step into slow direction; diagonal step
|
||||||
|
x += diagonalStepX;
|
||||||
|
y += diagonalStepY;
|
||||||
|
} else {
|
||||||
|
// step into fast direction; parallel step
|
||||||
|
x += parallelStepX;
|
||||||
|
y += parallelStepY;
|
||||||
|
}
|
||||||
|
|
||||||
|
pathList.add(new PositionVector(x, y));
|
||||||
|
}
|
||||||
|
return pathList;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a String representation of the track, including the car locations.
|
* Return a String representation of the track, including the car locations.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue