Skip to content

Commit

Permalink
Added first set of BoundQuadTree classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jotschi committed May 7, 2011
1 parent 3051306 commit 604522f
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 177 deletions.
18 changes: 18 additions & 0 deletions src/main/java/at/jotschi/quadtree/BoundsNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package at.jotschi.quadtree;

import java.awt.Dimension;
import java.awt.Point;

public class BoundsNode<T> extends Node<T> {

public BoundsNode(Point startCoordinates, Dimension bounds, int depth) {
super(startCoordinates, bounds, depth);
}

public BoundsNode(Point startCoordinates, Dimension bounds, int depth,
int maxDepth, int maxChildren) {
super(startCoordinates, bounds, depth, maxDepth, maxChildren);

}

}
24 changes: 24 additions & 0 deletions src/main/java/at/jotschi/quadtree/BoundsNodeElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package at.jotschi.quadtree;

import java.awt.Dimension;
import java.awt.Point;

@SuppressWarnings("serial")
public class BoundsNodeElement<T> extends NodeElement<T> {

protected Dimension bounds;

public BoundsNodeElement(Point coordinates, Dimension bounds, T element) {
super(coordinates, element);
this.bounds = bounds;
}

public int getWidth() {
return this.bounds.width;
}

public int getHeight() {
return this.bounds.height;
}

}
75 changes: 75 additions & 0 deletions src/main/java/at/jotschi/quadtree/BoundsQuadTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package at.jotschi.quadtree;

import java.awt.Dimension;
import java.awt.Point;

/**
* Creates a new QuadTree that can hold the given type of elements. Each element
* can also have a certain dimension.
*
* @author jotschi
*
* @param <T>
*/
public class BoundsQuadTree<T> extends QuadTree<T> {

/**
* Create a new QuadTree with the give start coordinates and size
*
* @param startCorrdinates
* @param size
*/
public BoundsQuadTree(Point startCoordinates, Dimension size) {

super(startCoordinates, size);

this.rootNode = new BoundsNode<T>(startCoordinates, size, 0);
}

/**
* Add a new element to the QuadTree that has a specific dimension/size
*
* @param point
* @param size
* @param element
*/
public void insert(Point point, Dimension elementSize, T element) {

// Check if the element coordinates are within bounds of the quadtree
if (point.x > startCoordinates.x + size.width
|| point.x < startCoordinates.x) {
throw new IndexOutOfBoundsException(
"The x coordinate must be within bounds of ["
+ startCoordinates.x + "] to [" + size.width
+ "] / [" + point.x + "]");
}
if (point.y > startCoordinates.y + size.height
|| point.y < startCoordinates.y) {
throw new IndexOutOfBoundsException(
"The y coordinate must be within bounds of ["
+ startCoordinates.y + "] to [" + size.height
+ "] / [" + point.y + "]");
}

// Check if the right bottom corner is within bounds
if (point.x + elementSize.width > startCoordinates.x + size.width
|| point.x < startCoordinates.x) {
throw new IndexOutOfBoundsException(
"The x coordinate must be within bounds of ["
+ startCoordinates.x + "] to [" + size.width
+ "] / [" + point.x + elementSize.width + "]");
}
if (point.y + elementSize.height > startCoordinates.y + size.height
|| point.y < startCoordinates.y) {
throw new IndexOutOfBoundsException(
"The y coordinate must be within bounds of ["
+ startCoordinates.y + "] to [" + size.height
+ "] / [" + point.x + elementSize.height + "]");
}

this.rootNode.insert(new BoundsNodeElement<T>(point, elementSize, element));

}


}
22 changes: 11 additions & 11 deletions src/main/java/at/jotschi/quadtree/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@
*/
public class Node<T> {

private static Logger log = Logger.getLogger(Node.class);
protected static Logger log = Logger.getLogger(Node.class);

public static enum Cell {
TOP_LEFT, BOTTOM_RIGHT, BOTTOM_LEFT, TOP_RIGHT
}

private Dimension bounds;
private Point startCoordinates;
private int maxDepth;
private int maxElements;
private int depth;
protected Dimension bounds;
protected Point startCoordinates;
protected int maxDepth;
protected int maxElements;
protected int depth;

/**
* Default value for amount of elements
*/
private final int MAX_ELEMENTS = 4;
protected final int MAX_ELEMENTS = 4;

/**
* Default value for max depth
*/
private final int MAX_DEPTH = 4;
protected final int MAX_DEPTH = 4;

private Map<Cell, Node<T>> nodes = new HashMap<Cell, Node<T>>();
protected Map<Cell, Node<T>> nodes = new HashMap<Cell, Node<T>>();

/**
* Holds all elements for this node
Expand Down Expand Up @@ -164,7 +164,7 @@ private Cell findIndex(Point coordinates) {
*
* @return
*/
public Vector<NodeElement<T>> getElements() {
public Vector<? extends NodeElement<T>> getElements() {
return this.elements;
}

Expand All @@ -174,7 +174,7 @@ public Vector<NodeElement<T>> getElements() {
* @param coordinates
* @return
*/
public Vector<NodeElement<T>> getElements(Point coordinates) {
public Vector<? extends NodeElement<T>> getElements(Point coordinates) {

// Check if this node has already been subdivided. Therefor this node
// should contain no elements
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/at/jotschi/quadtree/NodeElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
* @author jotschi
*
*/
@SuppressWarnings("serial")
public class NodeElement<T> extends Point {

private static final long serialVersionUID = -6818452324965717494L;

private T element;

/**
Expand Down
51 changes: 46 additions & 5 deletions src/main/java/at/jotschi/quadtree/QuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
public class QuadTree<T> {

private Node<T> rootNode;
private Dimension size;
private Point startCoordinates;
protected Node<T> rootNode;
protected Dimension size;
protected Point startCoordinates;

/**
* Create a new QuadTree with the give start coordinates and size
Expand Down Expand Up @@ -59,6 +59,48 @@ public void insert(int x, int y, T element) {
insert(new Point(x, y), element);
}

/**
* Add a new element to the QuadTree that has a specific dimension/size
*
* @param point
* @param size
* @param element
*/
public void insert(Point point, Dimension size, T element) {

// Check if the element coordinates are within bounds of the quadtree
if (point.x > startCoordinates.x + size.width
|| point.x < startCoordinates.x) {
throw new IndexOutOfBoundsException(
"The x coordinate must be within bounds of ["
+ startCoordinates.x + "] to [" + size.width + "]");
}
if (point.y > startCoordinates.y + size.height
|| point.y < startCoordinates.y) {
throw new IndexOutOfBoundsException(
"The y coordinate must be within bounds of ["
+ startCoordinates.y + "] to [" + size.height + "]");
}

//Check if the right bottom corner is within bounds
if (point.x+size.width > startCoordinates.x + size.width
|| point.x < startCoordinates.x) {
throw new IndexOutOfBoundsException(
"The x coordinate must be within bounds of ["
+ startCoordinates.x + "] to [" + size.width + "]");
}
if (point.y+size.width > startCoordinates.y + size.height
|| point.y < startCoordinates.y) {
throw new IndexOutOfBoundsException(
"The y coordinate must be within bounds of ["
+ startCoordinates.y + "] to [" + size.height + "]");
}


this.rootNode.insert(new NodeElement<T>(point, element));

}

/**
* Add a new element to the QuadTree
*
Expand Down Expand Up @@ -106,8 +148,7 @@ public Node<T> getRootNode() {
* @param coordinates
* @return
*/
public Vector<NodeElement<T>> getElements(Point coordinates) {
public Vector<? extends NodeElement<T>> getElements(Point coordinates) {
return this.rootNode.getElements(coordinates);

}
}
Loading

0 comments on commit 604522f

Please sign in to comment.