Skip to content

Commit

Permalink
Refactor + make unavailable previous snakeline
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 committed Dec 11, 2023
1 parent 9c9ddaa commit 2bb8734
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public List<Point> findShortestPath(Grid grid, int startX, int startY, int endX,
Grid.Node currentParent = current.getParent();
// Path can be rebuilt only when goal point is reached
if (currentPoint.equals(goal)) {
return rebuildPath(current);
List<Point> path = rebuildPath(current);
// Make path not available
grid.setAvailability(path, false);
return path;
}
// Store node already visited
visited.add(currentPoint);
Expand All @@ -60,7 +63,8 @@ public List<Point> findShortestPath(Grid grid, int startX, int startY, int endX,
return new ArrayList<>();
}

private List<Point> rebuildPath(Grid.Node current) {
private List<Point> rebuildPath(Grid.Node goal) {
Grid.Node current = goal;
// Reconstruct path
List<Point> path = new ArrayList<>();
while (current != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public void setAvailability(int x, int y, boolean available) {
getNode(x, y).cost = available ? WALKABLE : NOT_WALKABLE;
}

public void setAvailability(Point point, boolean available) {
setAvailability(point.x(), point.y(), available);
}

public void setAvailability(List<com.powsybl.sld.layout.pathfinding.Point> path, boolean available) {
path.forEach(p -> setAvailability(p, available));
}

public boolean isAvailable(Node n) {
return isAvailable(n.point);
}
Expand All @@ -144,21 +152,21 @@ public boolean isAvailable(Point point) {
public List<Node> getNeighbors(Point point) {
// Considering only adjacent points
List<Node> neighbors = new ArrayList<>();
Node n = getNode(point.x() + 1, point.y());
if (isAvailable(n)) {
neighbors.add(n);
Node right = getNode(point.x() + 1, point.y());
Node left = getNode(point.x() - 1, point.y());
Node up = getNode(point.x(), point.y() + 1);
Node down = getNode(point.x(), point.y() - 1);
if (isAvailable(right)) {
neighbors.add(right);
}
n = getNode(point.x() - 1, point.y());
if (isAvailable(n)) {
neighbors.add(n);
if (isAvailable(left)) {
neighbors.add(left);
}
n = getNode(point.x(), point.y() + 1);
if (isAvailable(n)) {
neighbors.add(n);
if (isAvailable(up)) {
neighbors.add(up);
}
n = getNode(point.x(), point.y() - 1);
if (isAvailable(n)) {
neighbors.add(n);
if (isAvailable(down)) {
neighbors.add(down);
}
return neighbors;
}
Expand Down
Loading

0 comments on commit 2bb8734

Please sign in to comment.