refactoring of Method calculatePath from Game.java to Track.java #30

Merged
schrom01 merged 1 commits from Game into main 2022-03-24 13:35:38 +01:00
2 changed files with 62 additions and 56 deletions

View File

@ -271,63 +271,10 @@ public class Game implements GameSpecification {
*/
@Override
public List<PositionVector> calculatePath(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;
return track.calculatePointsOnPath(startPosition, endPosition);
}
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) {
List<PositionVector> path = calculatePath(start, finish);

View File

@ -355,6 +355,65 @@ public class Track implements TrackSpecification {
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.
*