merging Point and Node classes

master
Peter Babič 9 years ago
parent d94ce414ed
commit 479311bfce
  1. 16
      src/environment/AStar.java
  2. 104
      src/environment/Node.java
  3. 92
      src/environment/Point.java
  4. 2
      src/environment/RouteFinder.java
  5. 29
      src/environment/World.java

@ -23,8 +23,8 @@ public class AStar {
Set<Node> open = new HashSet<Node>();
Set<Node> closed = new HashSet<Node>();
Node start = new Node(world.getStart().getX(), world.getStart().getY());
Node stop = new Node(world.getStop().getX(), world.getStop().getY());
Node start = new Node(world, world.getStart().getX(), world.getStart().getY());
Node stop = new Node(world, world.getStop().getX(), world.getStop().getY());
start.setG(0);
start.setH(manhattanDistance(start, stop));
@ -53,10 +53,10 @@ public class AStar {
closed.add(current);
List<Node> neighbors = new ArrayList<Node>();
neighbors.add(new Node(current.getX() + world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(current.getX() - world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(current.getX(), current.getY() + world.getSquareEdgeLenght()));
neighbors.add(new Node(current.getX(), current.getY() - world.getSquareEdgeLenght()));
neighbors.add(new Node(world, current.getX() + world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(world, current.getX() - world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(world, current.getX(), current.getY() + world.getSquareEdgeLenght()));
neighbors.add(new Node(world, current.getX(), current.getY() - world.getSquareEdgeLenght()));
current.setNeighbors(neighbors);
for (Node neighbor : current.getNeighbors()) {
@ -96,9 +96,7 @@ public class AStar {
}
private boolean isOccupied(Node node) {
// We need to convert since obstacles in in points not nodes
Point point = new Point(world, node);
return world.getObstacles().contains(point);
return world.getObstacles().contains(node);
}
private boolean isOutsideMap(Node node) {

@ -2,6 +2,7 @@ package environment;
import java.util.List;
import java.util.ArrayList;
import environment.World;
public class Node {
@ -14,10 +15,39 @@ public class Node {
private int x;
private int y;
private final int cost = 1;
private int col;
private int row;
private World world;
public Node(int x, int y) {
public Node(World world, int x, int y) {
this.world = world;
this.x = x;
this.y = y;
this.col = xToCol(x);
this.row = yToRow(y);
}
public Node(World world, int col, int row, boolean useRowsCols) {
this.world = world;
if (useRowsCols) {
this.col = col;
this.row = row;
this.x = colToX(col);
this.y = rowToY(row);
}
else {
this.x = col;
this.y = row;
this.col = xToCol(col);
this.row = yToRow(row);
}
}
@ -109,6 +139,7 @@ public class Node {
*/
public void setX(int x) {
this.x = x;
this.col = xToCol(x);
}
/**
@ -124,6 +155,7 @@ public class Node {
*/
public void setY(int y) {
this.y = y;
this.row = yToRow(y);
}
/**
@ -133,12 +165,68 @@ public class Node {
return cost;
}
/**
* @return
*/
public int getCol() {
return col;
}
/**
* @param col
*/
public void setCol(int col) {
this.col = col;
this.x = colToX(col);
}
/**
* @return
*/
public int getRow() {
return row;
}
/**
* @param row
*/
public void setRow(int row) {
this.row = row;
this.y = rowToY(row);
}
public int colToX(int col) {
return col * world.getSquareEdgeLenght() + world.getSquareCornerOffset();
}
public int rowToY(int row) {
return colToX(row);
}
public int xToCol(int x) {
return (x - world.getSquareCornerOffset()) / world.getSquareEdgeLenght();
}
public int yToRow(int y) {
return xToCol(y);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
if (col != node.col) return false;
if (row != node.row) return false;
return true;
}
@Override
public int hashCode() {
return world.getSquareEdgeLenght() * col + row;
}
// /**
// * @param cost
// * the cost to set
// */
// public void setCost(int cost) {
// this.cost = cost;
// }
}

@ -1,92 +0,0 @@
package environment;
import environment.World;
import environment.Node;
public class Point{
private World world;
private int col, row;
/**
* @Point world
* @param col
* @param row
*/
public Point(World world, int col, int row) {
this.world = world;
this.col = col;
this.row = row;
}
// Backward conversion
public Point(World world, Node node) {
this.world = world;
this.col = (node.getX() - world.getSquareCornerOffset()) / world.getSquareEdgeLenght();
this.row = (node.getY() - world.getSquareCornerOffset()) / world.getSquareEdgeLenght();
}
/**
* @return
*/
public int getCol() {
return col;
}
/**
* @param col
*/
public void setCol(int col) {
this.col = col;
}
/**
* @return
*/
public int getRow() {
return row;
}
/**
* @param row
*/
public void setRow(int row) {
this.row = row;
}
/**
* @return true x position in pixels on the world map
*/
public int getX() {
return col * world.getSquareEdgeLenght() + world.getSquareCornerOffset();
}
/**
* @return true y position in pixels on the world map
*/
public int getY() {
return row * world.getSquareEdgeLenght() + world.getSquareCornerOffset();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (col != point.col) return false;
if (row != point.row) return false;
return true;
}
@Override
public int hashCode() {
int result = col;
result = world.getSquareEdgeLenght() * result + row;
return result;
}
}

@ -39,7 +39,7 @@ public class RouteFinder {
RobotSetup[] robotSetups = new RobotSetup[world.getNumObstacles() + 1];
int NdxObstacle = 0;
for (Point point : world.getObstacles()) {
for (Node point : world.getObstacles()) {
// added offset so the tanks are in the middle of the tiles
double InitialObstacleCol = (double) point.getX();
double InitialObstacleRow = (double) point.getY();

@ -3,6 +3,7 @@ package environment;
import java.util.Set;
import java.util.HashSet;
import java.util.Random;
import environment.Node;
public class World {
@ -20,9 +21,9 @@ public class World {
// Obstacle (sitting ducks) count
private final int NUM_OBSTACLES = (int) (NUM_COLS * NUM_ROWS * 0.25);
// Contains poistions of obstacles, start point and destination
private Set<Point> obstacles = new HashSet<>();
private Point start, stop;
// Contains poistions of obstacles, start node and destination
private Set<Node> obstacles = new HashSet<>();
private Node start, stop;
// PRNG instance
private Random rand;
@ -91,21 +92,21 @@ public class World {
/**
* @return the obstacles
*/
public Set<Point> getObstacles() {
public Set<Node> getObstacles() {
return obstacles;
}
/**
* @return the start point
* @return the start node
*/
public Point getStart() {
public Node getStart() {
return start;
}
/**
* @return the stop point
* @return the stop node
*/
public Point getStop() {
public Node getStop() {
return stop;
}
@ -125,19 +126,19 @@ public class World {
}
private Point fillEmptyPosition() {
private Node fillEmptyPosition() {
// try to find free place for an obstacle
while (true) {
int col = randInt(0, getNumCols() - 1);
int row = randInt(0, getNumRows() - 1);
// Generate point in this world
Point point = new Point(this, col, row);
// Generate node in this world
Node node = new Node(this, col, row);
// Check if the point is still free
if (!obstacles.contains(point) && start != point && stop != point) {
return point;
// Check if the node is still free
if (!obstacles.contains(node) && start != node && stop != node) {
return node;
}
}
}

Loading…
Cancel
Save