Skip to content
Snippets Groups Projects
Commit db0a2852 authored by Yanis O's avatar Yanis O
Browse files

[Ajout] Méthode pour calculer les positions à distance Manhattan

parent 32589d08
No related branches found
No related tags found
No related merge requests found
package util;
import java.util.ArrayList;
import java.util.List;
public class PositionUtil {
public static List<Position> getPositionsAtManhattanDistance(Position fromPos, int distance, int rows, int cols) {
List<Position> positions = new ArrayList<>();
int x0 = fromPos.x();
int y0 = fromPos.y();
// Générer toutes les positions à une distance de Manhattan donnée
for (int dx = -distance; dx <= distance; dx++) {
int dy = distance - Math.abs(dx);
int[] dyOptions = { dy, -dy };
for (int deltaY : dyOptions) {
int x = x0 + dx;
int y = y0 + deltaY;
// Vérifier si la position est dans les limites de la matrice
if (x >= 0 && x < rows && y >= 0 && y < cols) {
positions.add(new Position(x, y));
}
}
}
return positions;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment