Finding if Path2D self-intersects - java

I need to find if Path2D intersects itself. For now, I do it by simply extracting an array of lines from path, and finding if any of these intersect. But it has O(n^2) complexity, and so it is very slow. Is there a faster way to do it?

You can do this faster using the sweep-line algorithm: http://en.wikipedia.org/wiki/Sweep_line_algorithm
Pseudocode:
Each line has a start point and an end point. Say that `start_x` <= `end_x` for all the lines.
Create an empty bucket of lines.
Sort all the points by their x coordinates, and then iterate through the sorted list.
If the current point is a start point, test its line against all the lines in the bucket, and then add its line to the
bucket.
if the current point is an end point, remove its line from the bucket.
The worst case is still O(N^2), but the average case is O(NlogN)

Here is my Java implementation of this algorithm:
import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.util.*;
/**
* Path2D helper functions.
* <p/>
* #author Gili Tzabari
*/
public class Path2Ds
{
/**
* Indicates if a Path2D intersects itself.
* <p/>
* #return true if a Path2D intersects itself
*/
public static boolean isSelfIntersecting(PathIterator path)
{
SortedSet<Line2D> lines = getLines(path);
if (lines.size() <= 1)
return false;
Set<Line2D> candidates = new HashSet<Line2D>();
for (Line2D line: lines)
{
if (Double.compare(line.getP1().distance(line.getP2()), 0) <= 0)
{
// Lines of length 0 do not cause self-intersection
continue;
}
for (Iterator<Line2D> i = candidates.iterator(); i.hasNext();)
{
Line2D candidate = i.next();
// Logic borrowed from Line2D.intersectsLine()
int lineRelativeToCandidate1 = Line2D.relativeCCW(line.getX1(), line.getY1(), line.
getX2(),
line.getY2(), candidate.getX1(), candidate.getY1());
int lineRelativeToCandidate2 = Line2D.relativeCCW(line.getX1(), line.getY1(), line.
getX2(),
line.getY2(), candidate.getX2(), candidate.getY2());
int candidateRelativeToLine1 = Line2D.relativeCCW(candidate.getX1(),
candidate.getY1(),
candidate.getX2(), candidate.getY2(), line.getX1(), line.getY1());
int candidateRelativeToLine2 = Line2D.relativeCCW(candidate.getX1(),
candidate.getY1(),
candidate.getX2(), candidate.getY2(), line.getX2(), line.getY2());
boolean intersection = (lineRelativeToCandidate1 * lineRelativeToCandidate2 <= 0)
&& (candidateRelativeToLine1 * candidateRelativeToLine2 <= 0);
if (intersection)
{
// Lines may share a point, so long as they extend in different directions
if (lineRelativeToCandidate1 == 0 && lineRelativeToCandidate2 != 0)
{
// candidate.P1 shares a point with line
if (candidateRelativeToLine1 == 0 && candidateRelativeToLine2 != 0)
{
// line.P1 == candidate.P1
continue;
}
if (candidateRelativeToLine1 != 0 && candidateRelativeToLine2 == 0)
{
// line.P2 == candidate.P1
continue;
}
// else candidate.P1 intersects line
}
else if (lineRelativeToCandidate1 != 0 && lineRelativeToCandidate2 == 0)
{
// candidate.P2 shares a point with line
if (candidateRelativeToLine1 == 0 && candidateRelativeToLine2 != 0)
{
// line.P1 == candidate.P2
continue;
}
if (candidateRelativeToLine1 != 0 && candidateRelativeToLine2 == 0)
{
// line.P2 == candidate.P2
continue;
}
// else candidate.P2 intersects line
}
else
{
// line and candidate overlap
}
return true;
}
if (candidate.getX2() < line.getX1())
i.remove();
}
candidates.add(line);
}
return false;
}
/**
* Returns all lines in a path. The lines are constructed such that the starting point is found
* on the left (or same x-coordinate) of the ending point.
* <p/>
* #param path the path
* #return the lines, sorted in ascending order of the x-coordinate of the starting point and
* ending point, respectively
*/
private static SortedSet<Line2D> getLines(PathIterator path)
{
double[] coords = new double[6];
SortedSet<Line2D> result = new TreeSet<Line2D>(new Comparator<Line2D>()
{
#Override
public int compare(Line2D o1, Line2D o2)
{
int result = Double.compare(o1.getX1(), o2.getX1());
if (result == 0)
{
// Ensure we are consistent with equals()
return Double.compare(o1.getX2(), o2.getX2());
}
return result;
}
});
if (path.isDone())
return result;
int type = path.currentSegment(coords);
assert (type == PathIterator.SEG_MOVETO): type;
Point.Double startPoint = new Point.Double(coords[0], coords[1]);
Point.Double openPoint = startPoint;
path.next();
while (!path.isDone())
{
type = path.currentSegment(coords);
assert (type != PathIterator.SEG_CUBICTO && type != PathIterator.SEG_QUADTO): type;
switch (type)
{
case PathIterator.SEG_MOVETO:
{
openPoint = startPoint;
break;
}
case PathIterator.SEG_CLOSE:
{
coords[0] = openPoint.x;
coords[1] = openPoint.y;
break;
}
}
Point.Double endPoint = new Point.Double(coords[0], coords[1]);
if (Double.compare(startPoint.getX(), endPoint.getX()) < 0)
result.add(new Line2D.Double(startPoint, endPoint));
else
result.add(new Line2D.Double(endPoint, startPoint));
path.next();
startPoint = endPoint;
}
return result;
}
}

Related

Node problems with stack implementation of depth first search

If anyone could help me find out what I did wrong, it would be really appreciated. I have taken a few shots at this and have gotten closer with each version of my code. I started by having a linked list within the SearchPath class itself but now I moved it to the Node class. For some reason the code works and finds the end, but it does not properly remove some stuff from the stack. Feel free to give it a try. When the code finishes elements from the stack are supposed to be removed and it does but some extra coordinates end up in there. Not sure if it is because of the way I handled the processing of the already checked nodes or if it the recursion itself. This could also just be a horrible implementation of the problem that needs to be solved and maybe I need to think of another way to solve it, but I know the answer is right there. Possibly also take a look at how I included the linked list and stack as static variables within the node class. Should they have maybe been added elsewhere or could that have been causing some of the issues. Thanks a lot. Also the way it works is by inputting a grid of the same dimensions. Finish is indicated with F and start is indicated with S. The walls are X and the path is O to find the way throught path.
/* This program will implement a stack to try and traverse
a grid from a text file that contains a path. It will try
and find a path to the end and when it does it succeeds.
If it does not find the end that means that there was
not path that exists to it. Reads a file as input.
*/
import java.util.Stack;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.lang.Exception;
import java.util.LinkedList;
// This class will represent information about a part in the board and will be used by the
// algorithim to determine what to do next.
class Node {
private static Stack<Node> pathLocation = new Stack<>(); // Create a stack that will hold nodes for the algorithm
// to traverse the area.
private static LinkedList<int[]> checkedLocations = new LinkedList<int[]>();
private static int totalMoves = 0;
private int rowNumber, columnNumber;
private boolean checked;
public Node() { // Constructor for a default node.
}
// Gets the current node in the stack.
public Node getLocaiton() {
return pathLocation.peek();
}
// Removes a node from the stack.
public void removeLocation() {
pathLocation.pop();
}
// Adds a new node to the stack.
public void newLocation(Node nextPath) {
pathLocation.push(nextPath);
}
public Stack<Node> getStack() {
return pathLocation;
}
public int getRowLocation() {
return rowNumber;
}
public int getColumnLocation() {
return columnNumber;
}
public void setRowLocation(int inputRow) {
rowNumber = inputRow;
}
public void setColumnLocation(int inputColumn) {
columnNumber = inputColumn;
}
public void setChecked(boolean input) {
checked = input;
}
public boolean getChecked() {
return checked;
}
public int getTotalMoves() {
return totalMoves;
}
public void setTotalMoves(int input) {
totalMoves = input;
}
public boolean getChecked(int inputColumn, int inputRow) {
for (int[] i : checkedLocations) {
if (inputColumn == i[0] && inputRow == i[1]) {
return true;
}
}
return false;
}
public boolean addChecked(int inputColumn, int inputRow) {
this.setColumnLocation(inputColumn);
this.setRowLocation(inputRow);
for (int[] i : checkedLocations) {
if (this.getColumnLocation() == i[0] && this.getRowLocation() == i[1]) {
--totalMoves;
this.checked = true;
return this.checked;
}
}
int[] temp = { this.getColumnLocation(), this.getRowLocation() };
checkedLocations.push(temp);
this.checked = false;
++totalMoves;
return this.checked;
}
}
class Board {
private int size; // This will determine the size of the board which is obtained from the
// BoardFile class.
private char[][] boardArea; // The current location on the board. This current program supports double
// array. Could later be upgraded to support more dimensions.
private int boardColumn;
private int boardRow;
Board(BoardFile inputBoard) throws Exception { // This constructor could throw and exception.
Scanner boardRead = inputBoard.processedBoard(); // Obtained the scanner from the boardFile class.
String temp; // Strings that will be processed and saved by the scanner.
while (boardRead.hasNext()) { // Processes infromation from the board as long as there is information to read.
temp = boardRead.nextLine(); // Sets a temp string equal to what was next in the file.
int tempSize; // Remembers the size of the temp string.
size = temp.length(); // Sets the total size of game area.
boardColumn = size;
boardArea = new char[size][size];
int j = 0;
boolean startFound = false, endFound = false; // Makes sure there is a finish and a start.
while (temp.length() == size) {
for (int i = 0; i < temp.length(); ++i) { // This loop will construct the game board and
// and makes sure everything is correct.
if (temp.charAt(i) == 'X' || temp.charAt(i) == 'O' || temp.charAt(i) == 'F' || temp.charAt(i) == 'S') {
boardArea[i][j] = temp.charAt(i);
// The following if statements check to make sure there is only one instance of
// finish and start.
if (temp.charAt(i) == 'F' && !endFound) {
endFound = true;
}
else if (temp.charAt(i) == 'F' && endFound) {
throw new Exception("The finish was already found. Please check and try again.");
}
else if (temp.charAt(i) == 'S' && !startFound) {
startFound = true;
}
else if (temp.charAt(i) == 'S' && startFound) {
throw new Exception("The start was already found. Please check and try again.");
}
}
else {
// throws an exception in case one of the characters was not excpected.
throw new Exception("There was a incorrect character within the board. Please check and try again.");
}
}
if (boardRead.hasNext()) {
tempSize = temp.length(); // Assign the previous line size.
temp = boardRead.nextLine(); // Assign the next line.
if (tempSize != temp.length()) { // Check to make sure both lines are the same size.
throw new Exception("The size of the board is not correct. Please check and try again.");
}
}
else {
break;
}
++j; // Increment the row.
if (j > size - 1) { // Makes sure the area in the map is correct.
throw new Exception("The area in the map is not the same. Please check and try again.");
}
boardRow = j; // Sets the row number.
}
if (j != size - 1) {
throw new Exception("The area in the map is not the same. Please check and try again.");
}
if (!endFound) {
throw new Exception("The end was not found. Please check and try again.");
}
if (!startFound) {
throw new Exception("The start was not found. Please check and try again.");
}
}
}
public char[][] getBoardArea() { // Returns the array.
return boardArea;
}
public char getBoardLocation(int inputColumn, int inputRow) { // Gets a character a certain location
return boardArea[inputColumn][inputRow]; // within the array.
}
public int getBoardColumn() { // Gets the column total.
return boardColumn - 1;
}
public int getBoardRow() { // Gets the row total.
return boardRow;
}
}
// The class used to read a file.
class BoardFile {
private File file;
private FileReader createdBoard;
private Scanner readBoard;
BoardFile(String fileName) throws Exception { // Creates a file reader object.
file = new File(fileName);
if (!file.exists()) { // Makes sure the file is found.
throw new Exception("The file could not be found.");
}
createdBoard = new FileReader(file); // Creates the file reader.
readBoard = new Scanner(createdBoard); // Creates a new scanner object to read the board.
}
public Scanner processedBoard() { // Gets the scanner object from the read board.
return this.readBoard;
}
}
// This class is the actual searching and will be used to traverse the map.
public class PathSearch {
private Node node = new Node(); // Creates the node object to traverse the map area.
private Board board; // Creates the map.
private Integer[] saveLocation = new Integer[2]; // Create an array that saves a certain location.
PathSearch() throws Exception { // Accepts nothing but asks for a string that searchs for a file.
Scanner input = new Scanner(System.in);
String userFile;
System.out.println("Please enter the name of the file.");
userFile = input.nextLine();
input.close();
board = new Board(new BoardFile(userFile)); // This creates the board from a file that was input.
for (int i = 0; i < board.getBoardColumn(); ++i) { // Searches the board for the starting point.
for (int j = 0; j < board.getBoardRow(); ++j) {
if (board.getBoardLocation(i, j) == 'S') {
node.newLocation(new Node()); // Creates a new element in the stack.
saveLocation[0] = i; // Saves the column in which the node was found.
saveLocation[1] = j; // Saves the row in which the node was found.
System.out.println("Start found at " + (saveLocation[0]) + " " + (saveLocation[1]));
break; // Exits the loop since the information that was needed was processed.
}
}
}
if (saveLocation == null) {
throw new Exception("No starting point was found. Please check and try again.");
}
checkPath(saveLocation[0], saveLocation[1]);
}
// This method accepts two seperate inputs because it needs to remember if it
// checked the position it is currently at.
public Stack<Node> checkPath(int inputColumn, int inputRow) throws Exception {
node.addChecked(inputColumn, inputRow); // Get the current node and check if has been used already.
if (node.getChecked() && !node.getStack().empty()) { // Sees if it was checked and if the stack is not empty.
node.removeLocation(); // Removes the current node from the stack.
checkPath(node.getLocaiton()); // Uses the previous node to keep checking,
}
else {
Node tempNode = new Node(); // Creates a new temp node.
tempNode.setColumnLocation(inputColumn);
tempNode.setRowLocation(inputRow);
node.newLocation(tempNode); // Adds the temp node into the stack.
checkPath(node.getLocaiton()); // Starts checking at the new location.
}
return null; // Path was not found.
}
// This is a overloaded version of checkPath that accepts the current node
// and begins looking at the current node.
public Stack<Node> checkPath(Node node) throws Exception {
int inputColumn = node.getLocaiton().getColumnLocation();
int inputRow = node.getLocaiton().getRowLocation();
if (board.getBoardLocation(inputColumn, inputRow) == 'F') {
System.out.println("The end was found at " + node.getLocaiton().getColumnLocation()
+ " " + node.getLocaiton().getRowLocation());
System.out.println("Here is the path.");
while (!node.getStack().empty()) { // While the stack has information.
System.out.println(node.getLocaiton().getColumnLocation() + " " + node.getLocaiton().getRowLocation());
node.removeLocation(); // Remove the node to process the next.
}
System.out.println("The total moves were " + node.getTotalMoves());
return node.getStack();
}
// All statements to determine where to move next within the board.
if (inputColumn < board.getBoardColumn() && (board.getBoardLocation(inputColumn + 1, inputRow) == 'O'
|| board.getBoardLocation(inputColumn + 1, inputRow) == 'F')
&& !node.getChecked(inputColumn + 1, inputRow)) {
checkPath(inputColumn + 1, inputRow);
}
if (inputColumn > 0 && (board.getBoardLocation(inputColumn - 1, inputRow) == 'O'
|| board.getBoardLocation(inputColumn - 1, inputRow) == 'F')
&& !node.getChecked(inputColumn - 1, inputRow)) {
checkPath(inputColumn - 1, inputRow);
}
if (inputRow < board.getBoardRow() && (board.getBoardLocation(inputColumn, inputRow + 1) == 'O'
|| board.getBoardLocation(inputColumn, inputRow + 1) == 'F')
&& !node.getChecked(inputColumn, inputRow + 1)) {
checkPath(inputColumn, inputRow + 1);
}
if (inputRow > 0 && (board.getBoardLocation(inputColumn, inputRow - 1) == 'O'
|| board.getBoardLocation(inputColumn, inputRow - 1) == 'F')
&& !node.getChecked(inputColumn, inputRow - 1)) {
checkPath(inputColumn, inputRow - 1);
}
return null; // No path was found at that location.
}
}

Why am I Getting Null Pointer Exceptions in My Linked Lists?

Could someone check my code for why I'm getting NullPointerExceptions in various methods? I've been racking my brain and can't figure out what is wrong.
I'm attempting to create a polynomial class that stores polynomials in a linked list using an inner class Terms. If you have any questions on the methods, please let me know!
public class Polynomial {
// Create the "Term" Inner Class for Handling the Respective Nodes for this Class
protected class Term {
int coefficient; // Coefficient of the Current Term
int exponent; // Exponent of the Current Term
Term next; // Reference to the Next Term in the Linked List (if Possible)
public Term (int coefficient, int exponent, Term next) { // General Constructor Method
this.coefficient = coefficient;
this.exponent = exponent;
this.next = next;
}
public Term (int coefficient, int exponent) { // No "Term next" (The Next Term Needs to be Null!)
this (coefficient, exponent, null);
}
public Term () { // Basic Constructor Method (No Input Necessary)
this (0, 0, null);
}
}
protected Term poly; // Starting Point (Dummy Header Node)
public Polynomial() { // General Constructor Method
poly = new Term();
clear();
}
public Polynomial(int [] terms) { // Constructor Method (Using an Integer Array of Coefficients and Exponents)
if (!(terms.length % 2 == 0)) { // This Array MUST Possess an Even, Positive Number of Elements (Alternating Coefficients and Corresponding Exponents)
throw new IllegalArgumentException("Your array must have an even, positive number of elements for use in our program. Thanks!");
}
else { // Proceed Normally...
poly = new Term(); // Dummy Header Node
clear();
setTerms(terms);
}
}
// Create the clear() Method for Promptly Resetting the Polynomial Back to 0
public void clear() {
poly.next = null;
}
// Create the setTerms(int [] terms) Method for Setting the Polynomial to the Appropriate Set of Terms
public void setTerms(int [] terms) {
Term temp = poly;
for (int i = 0; i < terms.length; i = i + 2) { // Incrementing By 2 (Each Polynomial Necessitates a Coefficient & Exponent, or Lack Thereof)
if (terms[i + 1] < 0) { // Polynomials CANNOT Have Coefficients with Negative Exponents
throw new IllegalArgumentException("Coefficients in polynomials must have positive exponents for use in our program. Thanks!");
}
temp.next = new Term(terms[i], terms[i + 1]);
temp = temp.next;
}
}
// Create the toString() for Outputting the Polynomial to the User's Screen
public String toString() {
String polynomialString = "";
boolean firstTerm = true;
if (poly.next == null) {
return "0"; // The ONLY Time 0 is Outputted as a Coefficient is if the Polynomial = 0
}
while (poly.next != null) { // Reached the Conclusion of the Linked List?
if (poly.next.coefficient > 0) { // Checking for Positivity
if (!firstTerm) { // First Term DOESN'T Need a "+" Sign
polynomialString = polynomialString + "+";
}
}
if (poly.next.coefficient == 1) {
if (poly.next.exponent == 1) {
polynomialString = polynomialString + "x"; // Print ONLY the Variable (x)
}
else {
polynomialString = polynomialString + "x^" + poly.next.exponent; // Otherwise, Print "x^exponent"
}
}
else if (poly.next.coefficient == -1) {
if (poly.next.exponent == 1) {
polynomialString = polynomialString + "-x"; // Print ONLY the Variable (x)
}
else {
polynomialString = polynomialString + "-x^" + poly.next.exponent; // Otherwise, Print "x^exponent"
}
}
else if (poly.next.exponent == 1) {
polynomialString = polynomialString + poly.next.coefficient + "x"; // Print "coefficientx"
}
else if (poly.next.exponent == 0) { // Print the Coefficient Alone...
polynomialString = polynomialString + poly.next.coefficient;
}
else { // Proceed Normally (WITH Coefficient & Exponent)
polynomialString = polynomialString + poly.next.coefficient + "x^" + poly.next.exponent;
}
poly = poly.next;
firstTerm = false;
}
return polynomialString;
}
// Create the addTerm() Method that Incorporates the Term "Coefficient * x^Exponent" to the Respective Polynomial (Needs Help)
public void addTerm(int coefficient, int exponent) {
if (exponent < 0) { // Polynomials CANNOT Have Coefficients with Negative Exponents
throw new IllegalArgumentException("Coefficients in polynomials must have positive exponents for use in our program. Thanks!");
}
Term newTerm = new Term(coefficient, exponent);
Term p = poly;
while (p.next != null && p.next.exponent > exponent) {
p = p.next;
}
newTerm.next = p.next;
p.next = newTerm;
}
// Create the eval() Method that Returns the Result of Evaluating the Polynomial at x = val (USE Horner's Method for this Method)
public double eval(double val) {
double result = 0;
Term p = poly;
p = p.next;
result += p.coefficient * val + p.next.coefficient;
p = p.next;
while (p.next != null) {
result = result * val + p.next.coefficient;
p = p.next;
}
return result;
}
// Create the multiply() Method that Returns a NEW Polynomial After Multiplying Each Term by the scalar Variable
public Polynomial multiply(int scalar) {
Polynomial newPoly = new Polynomial();
Term p = poly.next;
while (p.next != null) {
newPoly.addTerm(scalar * p.coefficient, p.exponent); // Multiplying the Coefficients and Scalar Together
p = p.next;
}
return newPoly;
}
// Create the add(Polynomial rhs) Method that Returns the Result of ADDING the "rhs" polynomial to "this" polynomial
public Polynomial add(Polynomial rhs) {
Polynomial newPoly = new Polynomial();
Term p1 = poly.next; // this polynomial
Term p2 = rhs.poly.next; // rhs polynomial
while (p1.next != null && p2.next != null) {
if (p1.exponent == p2.exponent) { // Same Exponents?
newPoly.addTerm(p1.coefficient + p2.coefficient, p1.exponent);
p1 = p1.next;
p2 = p2.next;
}
else if (p1.exponent > p2.exponent) { // Greater than rhs Polynomial?
newPoly.addTerm(p1.coefficient, p1.exponent);
p1 = p1.next;
}
else { // Less than rhs Polynomial?
newPoly.addTerm(p2.coefficient, p2.exponent);
p2 = p2.next;
}
}
if (p1.next == null) {
while (p2.next != null) { // Does the rhs Polynomial have Extra Terms?
newPoly.addTerm(p2.coefficient, p2.exponent);
p2 = p2.next;
}
}
if (p2.next == null) {
while (p1.next != null) { // Does the this Polynomial have Extra Terms?
newPoly.addTerm(p1.coefficient, p1.exponent);
p1 = p1.next;
}
}
return newPoly;
}
// Create the multiply(Polynomial rhs) Method that Returns the Result of MULTIPLYING the "rhs" polynomial to "this" polynomial
public Polynomial multiply(Polynomial rhs) {
Polynomial newPoly = new Polynomial();
Term p1 = poly.next; // this polynomial
Term p2 = rhs.poly.next; // rhs polynomial
while (p1.next != null) {
while (p2.next != null) {
newPoly.addTerm(p1.coefficient * p2.coefficient, p1.exponent + p2.exponent);
p2 = p2.next;
}
p1 = p1.next;
}
return newPoly;
}

Java - Can't break out of this recursive method

I am trying to implement a depht first search alogrithm (my code is probably horrible, I'm sorry). Now I wanted to make this a recursive method but I just can't seem to be able to break out of it once the end condition is met. The first if-conditional you see in the method should break out of the method. When I was debugging the project it reached the return statement and then immediately jumped to the end of the method. But instead of stopping the whole thing it went back to the while(!allNeighboursVisited) loop and went on in an infinite loop.
I was trying to solve this by myself which did not work and started searching in the web, but I just could not find any solution to my problem.
EDIT: Decided to share the link to the project on my github for you guys to try it out: https://github.com/Equiphract/Maze
EDIT 2: Updated the code; I hacked it together so please don't expect anything that is pleasant to look at :)
Here is the recursive method:
public void depthFirstSearch(int x, int y, Tile[][] maze) {
// Return method after every Tile is visited.
if (this.visitedCounter == maze.length * maze[0].length) {
this.stack.clear();
return;
}
Tile currentTile = maze[x][y];
Random r = new Random();
int neighbourAmount = currentTile.getNeighbourAmount();
boolean allNeighboursVisited = false;
int stopCounter = 0;
// If it is a new Tile, mark it as visited
if (!currentTile.isVisited()) {
currentTile.setVisited(true);
this.visitedCounter++;
stack.add(currentTile);
}
// Check if neighbours are not yet visited and "visit" one of them.
while (!allNeighboursVisited) {
int random;
do {
random = r.nextInt(neighbourAmount);
} while (this.excludeList.contains(random));
Tile neighbour = currentTile.getNeighbours().get(random);
if (!neighbour.isVisited()) {
if (neighbour.getX() == currentTile.getX() - 1) {
currentTile.getWall(4).setOpen(true);
neighbour.getWall(2).setOpen(true);
} else if (neighbour.getX() == currentTile.getX() + 1) {
currentTile.getWall(2).setOpen(true);
neighbour.getWall(4).setOpen(true);
} else if (neighbour.getY() == currentTile.getY() - 1) {
currentTile.getWall(1).setOpen(true);
neighbour.getWall(3).setOpen(true);
} else if (neighbour.getY() == currentTile.getY() + 1) {
currentTile.getWall(3).setOpen(true);
neighbour.getWall(1).setOpen(true);
}
this.excludeList.clear();
depthFirstSearch(neighbour.getX(), neighbour.getY(), maze);
if (this.visitedCounter == maze.length * maze[0].length) {
this.stack.clear();
return;
}
} else {
this.excludeList.add(random);
stopCounter++;
}
if (stopCounter == neighbourAmount) {
allNeighboursVisited = true;
}
}
// If every neighbour has already been visited, go back one Tile.
if (!this.stack.isEmpty()) {
this.stack.remove(this.stack.size() - 1);
if (!this.stack.isEmpty()) {
Tile backtrackTile = this.stack.get(this.stack.size() - 1);
this.excludeList.clear();
depthFirstSearch(backtrackTile.getX(), backtrackTile.getY(), maze);
if (this.visitedCounter == maze.length * 3) {
this.stack.clear();
return;
}
}
this.excludeList.clear();
}
}
You know what, here is the Tile-Object (sorry for the high amount of edits in this short period):
public class Tile {
private ArrayList<Wall> walls;
private ArrayList<Tile> neighbours;
private int x;
private int y;
private boolean visited;
/*
* Constructor of the Tile class.
*/
public Tile(int x, int y) {
this.walls = new ArrayList<Wall>();
this.neighbours = new ArrayList<Tile>();
this.walls.add(new Wall(1));
this.walls.add(new Wall(2));
this.walls.add(new Wall(3));
this.walls.add(new Wall(4));
this.x = x;
this.y = y;
this.visited = false;
}
/*
* Returns the ArrayList walls.
*/
public ArrayList<Wall> getWalls() {
return walls;
}
/*
* Returns the value of visited.
*/
public boolean isVisited() {
return visited;
}
/*
* Sets the value of visited to a specified value.
*
* #param visited a boolean value
*/
public void setVisited(boolean visited) {
this.visited = visited;
}
/*
* Returns a wall with the specified position.
*
* #param position the position of the wall
*/
public Wall getWall(int position) {
for(Wall w : this.walls) {
if(w.getPosition() == position) {
return w;
}
}
return null;
}
public int getNeighbourAmount() {
return this.neighbours.size();
}
public ArrayList<Tile> getNeighbours(){
return this.neighbours;
}
/*
* Adds a Tile to the ArrayList neighbours-
*
* #param t a Tile
*/
public void addNeighbour(Tile t) {
this.neighbours.add(t);
}
/**
* #return the x
*/
public int getX() {
return x;
}
/**
* #return the y
*/
public int getY() {
return y;
}
}
Ok I think I found a solution to my question. It is far from perfect and needs a lot of optimisation, maybe one of you guys want to do that and post it here^^.
My main mistake was not adding returns after each time I invoked the method recursively, which resulted in an endless-loop.
Here is my solution:
public void depthFirstSearch(int x, int y, Tile[][] maze) {
// Return method after every Tile is visited.
if (this.visitedCounter == maze.length * maze[0].length) {
this.stack.clear();
return;
}
Tile currentTile = maze[x][y];
Random r = new Random();
int neighbourAmount = currentTile.getNeighbourAmount();
boolean allNeighboursVisited = false;
int stopCounter = 0;
// If it is a new Tile, mark it as visited
if (!currentTile.isVisited()) {
currentTile.setVisited(true);
this.visitedCounter++;
stack.add(currentTile);
}
// Check if neighbours are not yet visited and "visit" one of them.
while (!allNeighboursVisited) {
int random;
do {
random = r.nextInt(neighbourAmount);
} while (this.excludeList.contains(random));
Tile neighbour = currentTile.getNeighbours().get(random);
if (!neighbour.isVisited()) {
if (neighbour.getX() == currentTile.getX() - 1) {
currentTile.getWall(4).setOpen(true);
neighbour.getWall(2).setOpen(true);
} else if (neighbour.getX() == currentTile.getX() + 1) {
currentTile.getWall(2).setOpen(true);
neighbour.getWall(4).setOpen(true);
} else if (neighbour.getY() == currentTile.getY() - 1) {
currentTile.getWall(1).setOpen(true);
neighbour.getWall(3).setOpen(true);
} else if (neighbour.getY() == currentTile.getY() + 1) {
currentTile.getWall(3).setOpen(true);
neighbour.getWall(1).setOpen(true);
}
this.excludeList.clear();
depthFirstSearch(neighbour.getX(), neighbour.getY(), maze);
return;
} else {
this.excludeList.add(random);
stopCounter++;
}
if (stopCounter == neighbourAmount) {
allNeighboursVisited = true;
}
}
// If every neighbour has already been visited, go back one Tile.
if (!this.stack.isEmpty()) {
this.stack.remove(this.stack.size() - 1);
if (!this.stack.isEmpty()) {
Tile backtrackTile = this.stack.get(this.stack.size() - 1);
this.excludeList.clear();
depthFirstSearch(backtrackTile.getX(), backtrackTile.getY(), maze);
return;
}
this.excludeList.clear();
}
}

Multi-2D Array Breadth First Search Java

I'm trying to create a method in a class for Java for a game called Quoridor in which a Pawn has to reach the other side of the board. The Pawn class (one coordinate) traverses a 9x9 2D array whereas the Wall classes (2 coordinates) are placed on a 10x10 2D array. The Walls are basically placed between the Pawn squares. Pawns cant cross Walls or other Pawns, I'm not sure how to implement the BFS with two 2D arrays. I'm new to programming and was wondering if someone could give me a step by step on how to create such a method. Currently have a Pawn and Wall class with necessary get and set methods.enter code here
package Players.HaydenLindquist;
import java.util.*;
import Engine.Logger;
import Interface.Coordinate;
import Interface.PlayerModule;
import Interface.PlayerMove;
public class HaydenLindquist implements PlayerModule {
Coordinate newCoords;
Wall theWall;
private Logger logOut;
Pawn player;
Pawn opponent;
List<Wall> wallList;
List<Pawn> pawnList;
public int getID()
{
return player.getId();
}
public Set<Coordinate> getNeighbors(Coordinate c) {
// Creates HashSet we will use to store neighbor tiles
Set<Coordinate> neighbor = new HashSet<Coordinate>();
int x = c.getRow();
int y = c.getCol();
// Coordinates for the 4 adjacent spaces
Coordinate top = new Coordinate(x,y-1);
Coordinate bottom = new Coordinate(x,y+1);
Coordinate left = new Coordinate(x-1,y);
Coordinate right = new Coordinate(x+1,y);
if(x == 0) {
if(y == 0) {
if(! wallCheck(right))
neighbor.add(right);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
else if(y == 8) {
if(! wallCheck(top))
neighbor.add(top);
if(! wallCheck(right))
neighbor.add(right);
}
else {
if(! wallCheck(top))
neighbor.add(top);
if(! wallCheck(right))
neighbor.add(right);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
}
else if(x == 8) {
if(y == 0) {
if(! wallCheck(left))
neighbor.add(left);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
else if(y == 8) {
if(! wallCheck(top))
neighbor.add(top);
if(! wallCheck(left))
neighbor.add(left);
}
else {
if(! wallCheck(top))
neighbor.add(top);
if(! wallCheck(left))
neighbor.add(left);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
}
else if(y == 0) {
if(! wallCheck(right))
neighbor.add(right);
if(! wallCheck(left))
neighbor.add(left);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
else if(y == 8) {
if(! wallCheck(right))
neighbor.add(right);
if(! wallCheck(left))
neighbor.add(left);
if(! wallCheck(top))
neighbor.add(top);
}
else {
if(! wallCheck(right))
neighbor.add(right);
if(! wallCheck(left))
neighbor.add(left);
if(! wallCheck(top))
neighbor.add(top);
if(! wallCheck(bottom))
neighbor.add(bottom);
}
return neighbor;
}
/**
*
*/
public Coordinate getPlayerLocation(int playerID)
{
if(playerID == player.getId())
{
return(player.getLocation());
}
else return(opponent.getLocation());
}
/**
*
*/
public Map<Integer, Coordinate> getPlayerLocations() {
// Creates HashMap of Integer, Coordinate type
HashMap<Integer, Coordinate> locations = new HashMap<Integer, Coordinate>();
// Adds the ID and locations of the 2 players to the HashMap
locations.put(player.getId(), player.getLocation());
locations.put(opponent.getId(), opponent.getLocation());
return locations;
}
/**
*
*/
public List<Coordinate> getShortestPath(Coordinate start, Coordinate end)
{
List<Coordinate> path = new ArrayList<Coordinate>();
return null;
}
/**
*
*/
public int getWallsRemaining(int playerID)
{
if(playerID == player.getId())
{
return(player.getWalls());
}
else return(opponent.getWalls());
}
/**
*
*/
public void init(Logger logger, int playerID, int numWalls, Map<Integer, Coordinate> playerHomes)
{
logOut = logger;
// Creates ArrayList used to store wall objects
wallList = new ArrayList<Wall>();
// Creates our two players and initializes them with data from engine
for ( Integer i : (Set<Integer>) playerHomes.keySet() )
{
if ( i == playerID )
player = new Pawn(playerID,numWalls,playerHomes.get(i));
else
{
opponent = new Pawn(2,numWalls,playerHomes.get(i));
}
}
}
public void lastMove(PlayerMove m)
{
// Check if m is a player move or wall placement
if(m.isMove())
{
// Switch to differentiate between player 1 and 2.
// then updates the appropriate players location
switch(m.getPlayerId())
{
case 1:
player.setLocation(m.getEnd());
break;
case 2:
opponent.setLocation(m.getEnd());
break;
}
}
else
{
switch(m.getPlayerId())
{
case 1:
addWall(m.getStart(), m.getEnd());
player.setWalls(player.getWalls() - 1);
break;
case 2:
addWall(m.getStart(), m.getEnd());
opponent.setWalls(player.getWalls() - 1);
break;
}
}
}
/**
*
*/
public Set<PlayerMove> allPossibleMoves()
{
return null;
}
/**
*
*/
public PlayerMove move()
{
return null;
}
/**
*
* #param player
* #return
*/
/**
*
*
*/
public void playerInvalidated(int playerID)
{
}
/**
* Method that creates a new wall object and adds it to the wallList ArrayList
*
* #param start
* #param end
*/
public void addWall(Coordinate start, Coordinate end)
{
Wall w = new Wall(start,end);
wallList.add(w);
}
/**
* A check method to see if entered coordinate contains a section of a wall
*
* #param c
* #return
*/
public boolean wallCheck(Coordinate c)
{
// Iterates through wall objects in wallList
for(int i = 0; i < wallList.size(); i++)
{
// Check if any adjacent squares contain a section of a wall
if(wallList.get(i).isWall(c))
{
return true;
}
}
return false;
}
}
Since you're starting with the idea of a BFS, and you've decided to represent your board with multidimensional arrays, why don't you start by thinking about how BFS maps to the representation of your board?
For example, can you write up the code to list all adjacent cells of a given cell? If you can do that, it should be easier to see how to implement the rest of BFS.

KDTree Implementation in Java

I'm looking for a KDTree implementation in Java.
I've done a google search and the results seem pretty haphazard. There are actually lots of results, but they're mostly just little one-off implementations, and I'd rather find something with a little more "production value". Something like apache collections or the excellent C5 collection library for .NET. Something where I can see the public bug tracker and check to see when the last SVN commit happened. Also, in an ideal world, I'd find a nice well-designed API for spatial data structures, and the KDTree would be just one class in that library.
For this project, I'll only be working in either 2 or 3 dimensions, and I'm mostly just interested in a good nearest-neighbors implementation.
In the book Algorithms in a Nutshell there is a kd tree implementation in java along with a few variations. All of the code is on oreilly.com and the book itself also walk you through the algorithm so you could build one yourself.
for future seekers. Java-ml library has a kd-tree implementation that work fine.
http://java-ml.sourceforge.net/
I've had success with Professor Levy's implementation found here. I realize you're looking for a more production-certified implementation so this is probably not a good fit.
However note to any passers-by, I've been using it for a while now in my photomosaic project with no issues. No guarantee but better than nothing :)
I created a KD-Tree implementation as part of an offline reverse geocoding library
https://github.com/AReallyGoodName/OfflineReverseGeocode
Maybe Nearest Neighbor Search and KD-trees from the Stony-Brook algorithm repository can help.
This is a full implementation for KD-Tree, I have used some libraries to store point and rectangle. These libraries are freely available. It is possible to do with these classes my making your own classes to store point and rectangle. Please share your feedback.
import java.util.ArrayList;
import java.util.List;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private static class Node {
public Point2D point; // the point
public RectHV rect; // the axis-aligned rectangle corresponding to this
public Node lb; // the left/bottom subtree
public Node rt; // the right/top subtree
public int size;
public double x = 0;
public double y = 0;
public Node(Point2D p, RectHV rect, Node lb, Node rt) {
super();
this.point = p;
this.rect = rect;
this.lb = lb;
this.rt = rt;
x = p.x();
y = p.y();
}
}
private Node root = null;;
public KdTree() {
}
public boolean isEmpty() {
return root == null;
}
public int size() {
return rechnenSize(root);
}
private int rechnenSize(Node node) {
if (node == null) {
return 0;
} else {
return node.size;
}
}
public void insert(Point2D p) {
if (p == null) {
throw new NullPointerException();
}
if (isEmpty()) {
root = insertInternal(p, root, 0);
root.rect = new RectHV(0, 0, 1, 1);
} else {
root = insertInternal(p, root, 1);
}
}
// at odd level we will compare x coordinate, and at even level we will
// compare y coordinate
private Node insertInternal(Point2D pointToInsert, Node node, int level) {
if (node == null) {
Node newNode = new Node(pointToInsert, null, null, null);
newNode.size = 1;
return newNode;
}
if (level % 2 == 0) {//Horizontal partition line
if (pointToInsert.y() < node.y) {//Traverse in bottom area of partition
node.lb = insertInternal(pointToInsert, node.lb, level + 1);
if(node.lb.rect == null){
node.lb.rect = new RectHV(node.rect.xmin(), node.rect.ymin(),
node.rect.xmax(), node.y);
}
} else {//Traverse in top area of partition
if (!node.point.equals(pointToInsert)) {
node.rt = insertInternal(pointToInsert, node.rt, level + 1);
if(node.rt.rect == null){
node.rt.rect = new RectHV(node.rect.xmin(), node.y,
node.rect.xmax(), node.rect.ymax());
}
}
}
} else if (level % 2 != 0) {//Vertical partition line
if (pointToInsert.x() < node.x) {//Traverse in left area of partition
node.lb = insertInternal(pointToInsert, node.lb, level + 1);
if(node.lb.rect == null){
node.lb.rect = new RectHV(node.rect.xmin(), node.rect.ymin(),
node.x, node.rect.ymax());
}
} else {//Traverse in right area of partition
if (!node.point.equals(pointToInsert)) {
node.rt = insertInternal(pointToInsert, node.rt, level + 1);
if(node.rt.rect == null){
node.rt.rect = new RectHV(node.x, node.rect.ymin(),
node.rect.xmax(), node.rect.ymax());
}
}
}
}
node.size = 1 + rechnenSize(node.lb) + rechnenSize(node.rt);
return node;
}
public boolean contains(Point2D p) {
return containsInternal(p, root, 1);
}
private boolean containsInternal(Point2D pointToSearch, Node node, int level) {
if (node == null) {
return false;
}
if (level % 2 == 0) {//Horizontal partition line
if (pointToSearch.y() < node.y) {
return containsInternal(pointToSearch, node.lb, level + 1);
} else {
if (node.point.equals(pointToSearch)) {
return true;
}
return containsInternal(pointToSearch, node.rt, level + 1);
}
} else {//Vertical partition line
if (pointToSearch.x() < node.x) {
return containsInternal(pointToSearch, node.lb, level + 1);
} else {
if (node.point.equals(pointToSearch)) {
return true;
}
return containsInternal(pointToSearch, node.rt, level + 1);
}
}
}
public void draw() {
StdDraw.clear();
drawInternal(root, 1);
}
private void drawInternal(Node node, int level) {
if (node == null) {
return;
}
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.02);
node.point.draw();
double sx = node.rect.xmin();
double ex = node.rect.xmax();
double sy = node.rect.ymin();
double ey = node.rect.ymax();
StdDraw.setPenRadius(0.01);
if (level % 2 == 0) {
StdDraw.setPenColor(StdDraw.BLUE);
sy = ey = node.y;
} else {
StdDraw.setPenColor(StdDraw.RED);
sx = ex = node.x;
}
StdDraw.line(sx, sy, ex, ey);
drawInternal(node.lb, level + 1);
drawInternal(node.rt, level + 1);
}
/**
* Find the points which lies in the rectangle as parameter
* #param rect
* #return
*/
public Iterable<Point2D> range(RectHV rect) {
List<Point2D> resultList = new ArrayList<Point2D>();
rangeInternal(root, rect, resultList);
return resultList;
}
private void rangeInternal(Node node, RectHV rect, List<Point2D> resultList) {
if (node == null) {
return;
}
if (node.rect.intersects(rect)) {
if (rect.contains(node.point)) {
resultList.add(node.point);
}
rangeInternal(node.lb, rect, resultList);
rangeInternal(node.rt, rect, resultList);
}
}
public Point2D nearest(Point2D p) {
if(root == null){
return null;
}
Champion champion = new Champion(root.point,Double.MAX_VALUE);
return nearestInternal(p, root, champion, 1).champion;
}
private Champion nearestInternal(Point2D targetPoint, Node node,
Champion champion, int level) {
if (node == null) {
return champion;
}
double dist = targetPoint.distanceSquaredTo(node.point);
int newLevel = level + 1;
if (dist < champion.championDist) {
champion.champion = node.point;
champion.championDist = dist;
}
boolean goLeftOrBottom = false;
//We will decide which part to be visited first, based upon in which part point lies.
//If point is towards left or bottom part, we traverse in that area first, and later on decide
//if we need to search in other part too.
if(level % 2 == 0){
if(targetPoint.y() < node.y){
goLeftOrBottom = true;
}
} else {
if(targetPoint.x() < node.x){
goLeftOrBottom = true;
}
}
if(goLeftOrBottom){
nearestInternal(targetPoint, node.lb, champion, newLevel);
Point2D orientationPoint = createOrientationPoint(node.x,node.y,targetPoint,level);
double orientationDist = orientationPoint.distanceSquaredTo(targetPoint);
//We will search on the other part only, if the point is very near to partitioned line
//and champion point found so far is far away from the partitioned line.
if(orientationDist < champion.championDist){
nearestInternal(targetPoint, node.rt, champion, newLevel);
}
} else {
nearestInternal(targetPoint, node.rt, champion, newLevel);
Point2D orientationPoint = createOrientationPoint(node.x,node.y,targetPoint,level);
//We will search on the other part only, if the point is very near to partitioned line
//and champion point found so far is far away from the partitioned line.
double orientationDist = orientationPoint.distanceSquaredTo(targetPoint);
if(orientationDist < champion.championDist){
nearestInternal(targetPoint, node.lb, champion, newLevel);
}
}
return champion;
}
/**
* Returns the point from a partitioned line, which can be directly used to calculate
* distance between partitioned line and the target point for which neighbours are to be searched.
* #param linePointX
* #param linePointY
* #param targetPoint
* #param level
* #return
*/
private Point2D createOrientationPoint(double linePointX, double linePointY, Point2D targetPoint, int level){
if(level % 2 == 0){
return new Point2D(targetPoint.x(),linePointY);
} else {
return new Point2D(linePointX,targetPoint.y());
}
}
private static class Champion{
public Point2D champion;
public double championDist;
public Champion(Point2D c, double d){
champion = c;
championDist = d;
}
}
public static void main(String[] args) {
String filename = "/home/raman/Downloads/kdtree/circle100.txt";
In in = new In(filename);
KdTree kdTree = new KdTree();
while (!in.isEmpty()) {
double x = in.readDouble();
double y = in.readDouble();
Point2D p = new Point2D(x, y);
kdTree.insert(p);
}
// kdTree.print();
System.out.println(kdTree.size());
kdTree.draw();
System.out.println(kdTree.nearest(new Point2D(0.4, 0.5)));
System.out.println(new Point2D(0.7, 0.4).distanceSquaredTo(new Point2D(0.9,0.5)));
System.out.println(new Point2D(0.7, 0.4).distanceSquaredTo(new Point2D(0.9,0.4)));
}
}
There is also JTS Topology Suite
The KdTree implementation only provides range search (no nearest-neighbors).
If nearest-neighbor is your thing look at STRtree
You are correct, there are not that many sites with kd implementation for java! anyways, kd tree is basically a binary search tree which a median value typically is calculated each time for that dimension. Here is simple KDNode and in terms of nearest neighbor method or full implementation take a look at this github project. It was the best one I could find for you. Hope this helps you.
private class KDNode {
KDNode left;
KDNode right;
E val;
int depth;
private KDNode(E e, int depth){
this.left = null;
this.right = null;
this.val = e;
this.depth = depth;
}
May be it will be interest for someone. Please see my nearest() (and KD Tree class) implementation for 2D tree in java:
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
import java.util.ArrayList;
import java.util.List;
public class KdTree {
private Node root;
private int size;
private static class Node {
private Point2D p; // the point
private RectHV rect; // the axis-aligned rectangle corresponding to this node
private Node lb; // the left/bottom subtree
private Node rt; // the right/top subtree
public Node(Point2D p, RectHV rect) {
this.p = p;
this.rect = rect;
}
}
public KdTree() {
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public boolean contains(Point2D p) {
if (p == null) throw new IllegalArgumentException("argument to contains() is null");
return contains(root, p, 1);
}
private boolean contains(Node node, Point2D p, int level) {
if (node == null) return false; // a base case for recursive call
if (node.p.equals(p)) return true;
if (level % 2 == 0) { // search by y coordinate (node with horizontal partition line)
if (p.y() < node.p.y())
return contains(node.lb, p, level + 1);
else
return contains(node.rt, p, level + 1);
}
else { // search by x coordinate (node with vertical partition line)
if (p.x() < node.p.x())
return contains(node.lb, p, level + 1);
else
return contains(node.rt, p, level + 1);
}
}
public void insert(Point2D p) {
if (p == null) throw new IllegalArgumentException("calls insert() with a null point");
root = insert(root, p, 1);
}
private Node insert(Node x, Point2D p, int level) {
if (x == null) {
size++;
return new Node(p, new RectHV(0, 0, 1, 1));
}
if (x.p.equals(p)) return x; // if we try to insert existed point just return its node
if (level % 2 == 0) { // search by y coordinate (node with horizontal partition line)
if (p.y() < x.p.y()) {
x.lb = insert(x.lb, p, level + 1);
if (x.lb.rect.equals(root.rect))
x.lb.rect = new RectHV(x.rect.xmin(), x.rect.ymin(), x.rect.xmax(), x.p.y());
}
else {
x.rt = insert(x.rt, p, level + 1);
if (x.rt.rect.equals(root.rect))
x.rt.rect = new RectHV(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.rect.ymax());
}
}
else { // search by x coordinate (node with vertical partition line)
if (p.x() < x.p.x()) {
x.lb = insert(x.lb, p, level + 1);
if (x.lb.rect.equals(root.rect))
x.lb.rect = new RectHV(x.rect.xmin(), x.rect.ymin(), x.p.x(), x.rect.ymax());
}
else {
x.rt = insert(x.rt, p, level + 1);
if (x.rt.rect.equals(root.rect))
x.rt.rect = new RectHV(x.p.x(), x.rect.ymin(), x.rect.xmax(), x.rect.ymax());
}
}
return x;
}
public void draw() {
draw(root, 1);
}
private void draw(Node node, int level) {
if (node == null) return;
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
node.p.draw();
StdDraw.setPenRadius();
if (level % 2 == 0) {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(node.rect.xmin(), node.p.y(), node.rect.xmax(), node.p.y());
}
else {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.line(node.p.x(), node.rect.ymin(), node.p.x(), node.rect.ymax());
}
draw(node.lb, level + 1);
draw(node.rt, level + 1);
}
public Iterable<Point2D> range(RectHV rect) {
if (rect == null) throw new IllegalArgumentException("calls range() with a null rect");
List<Point2D> points = new ArrayList<>(); // create an Iterable object with all points we found
range(root, rect, points); // call helper method with rects intersects comparing
return points; // return an Iterable object (It could be any type - Queue, LinkedList etc)
}
private void range(Node node, RectHV rect, List<Point2D> points) {
if (node == null || !node.rect.intersects(rect)) return; // a base case for recursive call
if (rect.contains(node.p))
points.add(node.p);
range(node.lb, rect, points);
range(node.rt, rect, points);
}
public Point2D nearest(Point2D query) {
if (isEmpty()) return null;
if (query == null) throw new IllegalArgumentException("calls nearest() with a null point");
// set the start distance from root to query point
double best = root.p.distanceSquaredTo(query);
// StdDraw.setPenColor(StdDraw.BLACK); // just for debugging
// StdDraw.setPenRadius(0.01);
// query.draw();
return nearest(root, query, root.p, best, 1); // call a helper method
}
private Point2D nearest(Node node, Point2D query, Point2D champ, double best, int level) {
// a base case for the recursive call
if (node == null || best < node.rect.distanceSquaredTo(query)) return champ;
// we'll need to set an actual best distance when we recur
best = champ.distanceSquaredTo(query);
// check whether a distance from query point to the traversed node less than
// distance from current champion to query point
double temp = node.p.distanceSquaredTo(query);
if (temp < best) {
best = temp;
champ = node.p;
}
if (level % 2 == 0) { // search by y coordinate (node with horizontal partition line)
// we compare y coordinate and decide go up or down
if (node.p.y() < query.y()) { // if true go up
champ = nearest(node.rt, query, champ, best, level + 1);
// important case - when we traverse node and go back up through the tree
// we need to decide whether we need to go down(left) in this node or not
// we just check our bottom (left) node on null && compare distance
// from query point to the nearest point of the node's rectangle and
// the distance from current champ point to thr query point
if (node.lb != null && node.lb.rect.distanceSquaredTo(query) < champ.distanceSquaredTo(query)) {
champ = nearest(node.lb, query, champ, best, level + 1);
}
}
else { // if false go down
champ = nearest(node.lb, query, champ, best, level + 1);
if (node.rt != null && node.rt.rect.distanceSquaredTo(query) < champ.distanceSquaredTo(query))
// when we traverse node and go back up through the tree
// we need to decide whether we need to go up(right) in this node or not
// we just check our top (right) node on null && compare distance
// from query point to the nearest point of the node's rectangle and
// the distance from current champ point to thr query point
champ = nearest(node.rt, query, champ, best, level + 1);
}
}
else {
// search by x coordinate (node with vertical partition line)
if (node.p.x() < query.x()) { // if true go right
champ = nearest(node.rt, query, champ, best, level + 1);
// the same check as mentioned above when we search by y coordinate
if (node.lb != null && node.lb.rect.distanceSquaredTo(query) < champ.distanceSquaredTo(query))
champ = nearest(node.lb, query, champ, best, level + 1);
}
else { // if false go left
champ = nearest(node.lb, query, champ, best, level + 1);
if (node.rt != null && node.rt.rect.distanceSquaredTo(query) < champ.distanceSquaredTo(query))
champ = nearest(node.rt, query, champ, best, level + 1);
}
}
return champ;
}
public static void main(String[] args) {
// unit tests
KdTree kd = new KdTree();
Point2D p1 = new Point2D(0.7, 0.2);
Point2D p2 = new Point2D(0.5, 0.4);
Point2D p3 = new Point2D(0.2, 0.3);
Point2D p4 = new Point2D(0.4, 0.7);
Point2D p5 = new Point2D(0.9, 0.6);
// Point2D query = new Point2D(0.676, 0.736);
Point2D query1 = new Point2D(0.972, 0.887);
// RectHV test = new RectHV(0, 0, 0.7, 0.4);
// Point2D query = new Point2D(0.331, 0.762);
// Point2D p6 = new Point2D(0.4, 0.4);
// Point2D p7 = new Point2D(0.1, 0.6);
// RectHV rect = new RectHV(0.05, 0.1, 0.15, 0.6);
kd.insert(p1);
kd.insert(p2);
kd.insert(p3);
kd.insert(p4);
kd.insert(p5);
System.out.println(kd.nearest(query1));
// System.out.println("Dist query to 0.4,0.7= " + query.distanceSquaredTo(p4));
// System.out.println("Dist query to RectHV 0.2,0,3= " + test.distanceSquaredTo(p4));
// kd.insert(p6);
// kd.insert(p7);
// System.out.println(kd.size);
// System.out.println(kd.contains(p3));
// // System.out.println(kd.range(rect));
kd.draw();
}
}
Thanks to theosem, really!
Based on his posted library (http://java-ml.sourceforge.net/) I made this code example:
package kdtreeexample; //place your package name here
import net.sf.javaml.core.kdtree.KDTree; //import library
public class KDTreeExample {
public static void main(String[] args) {
KDTree kdTree = new KDTree(2); //2 dimensions (x, y)
// point insertion:
kdTree.insert(new double[]{4, 3}, 0); //insert points (x=4,y=3), index = 0
kdTree.insert(new double[]{1, 10}, 1); //insert points (x=1,y=10), index = 1
kdTree.insert(new double[]{10, 10}, 2); //insert points (x=10,y=10), index = 2
kdTree.insert(new double[]{5, 1}, 3); //insert points (x=5,y=1), index = 3
// nearest index to point in coordinates x, y:
int x = 0; //x coordinate for target point
int y = 11; //y coordinate for target point
int nearestIndex = (int) kdTree.nearest(new double[]{x, y}); //doing calculation here
// result:
System.out.println("Nearest point value index to point(" + x + ", " + y + ") = " + nearestIndex);
System.out.println(kdTree.toString()); //check the data
}
}
package kdtree;
class KDNode{
KDNode left;
KDNode right;
int []data;
public KDNode(){
left=null;
right=null;
}
public KDNode(int []x){
left=null;
right=null;
data = new int[2];
for (int k = 0; k < 2; k++)
data[k]=x[k];
}
}
class KDTreeImpl{
KDNode root;
int cd=0;
int DIM=2;
public KDTreeImpl() {
root=null;
}
public boolean isEmpty(){
return root == null;
}
public void insert(int []x){
root = insert(x,root,cd);
}
private KDNode insert(int []x,KDNode t,int cd){
if (t == null)
t = new KDNode(x);
else if (x[cd] < t.data[cd])
t.left = insert(x, t.left, (cd+1)%DIM);
else
t.right = insert(x, t.right, (cd+1)%DIM);
return t;
}
public boolean search(int []data){
return search(data,root,0);
}
private boolean search(int []x,KDNode t,int cd){
boolean found=false;
if(t==null){
return false;
}
else {
if(x[cd]==t.data[cd]){
if(x[0]==t.data[0] && x[1]==t.data[1])
return true;
}else if(x[cd]<t.data[cd]){
found = search(x,t.left,(cd+1)%DIM);
}else if(x[cd]>t.data[cd]){
found = search(x,t.right,(cd+1)%DIM);
}
return found;
}
}
public void inorder(){
inorder(root);
}
private void inorder(KDNode r){
if (r != null){
inorder(r.left);
System.out.print("("+r.data[0]+","+r.data[1] +") ");
inorder(r.right);
}
}
public void preorder() {
preorder(root);
}
private void preorder(KDNode r){
if (r != null){
System.out.print("("+r.data[0]+","+r.data[1] +") ");
preorder(r.left);
preorder(r.right);
}
}
/* Function for postorder traversal */
public void postorder() {
postorder(root);
}
private void postorder(KDNode r) {
if (r != null){
postorder(r.left);
postorder(r.right);
System.out.print("("+r.data[0]+","+r.data[1] +") ");
}
}
}
public class KDTree {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
KDTreeImpl kdt = new KDTreeImpl();
int x[] = new int[2];
x[0] = 30;
x[1] = 40;
kdt.insert(x);
x[0] = 5;
x[1] = 25;
kdt.insert(x);
x[0] = 10;
x[1] = 12;
kdt.insert(x);
x[0] = 70;
x[1] = 70;
kdt.insert(x);
x[0] = 50;
x[1] = 30;
kdt.insert(x);
System.out.println("Input Elements");
System.out.println("(30,40) (5,25) (10,12) (70,70) (50,30)\n\n");
System.out.println("Printing KD Tree in Inorder");
kdt.inorder();
System.out.println("\nPrinting KD Tree in PreOder");
kdt.preorder();
System.out.println("\nPrinting KD Tree in PostOrder");
kdt.postorder();
System.out.println("\nsearching...............");
x[0]=40;x[1]=40;
System.out.println(kdt.search(x));
}
}

Categories