Skip to content

Commit

Permalink
Added new constructor to set maxChildren and maxElements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jotschi committed May 8, 2011
1 parent 6a8b7d7 commit 76d8a09
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/java/at/jotschi/quadtree/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public AbstractNode(Point startCoordinates, Dimension bounds, int depth) {
}

public AbstractNode(Point startCoordinates, Dimension bounds, int depth,
int maxDepth, int maxChildren) {
int maxDepth, int maxElements) {
this.startCoordinates = startCoordinates;
this.bounds = bounds;
this.maxDepth = maxDepth;
this.maxElements = maxChildren;
this.maxElements = maxElements;
this.depth = depth;
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/at/jotschi/quadtree/point/PointNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void insert(PointNodeElement<T> element) {

// Only subdivide the node if it contain more than MAX_CHILDREN and is
// not the deepest node
if (!(this.depth >= MAX_DEPTH) && this.elements.size() > MAX_ELEMENTS) {
if (!(this.depth >= maxDepth) && this.elements.size() > maxElements) {
this.subdivide();

// Recall insert for each element. This will move all elements of
Expand Down Expand Up @@ -184,22 +184,24 @@ public void subdivide() {
PointNode<T> cellNode = null;

// top left
cellNode = new PointNode<T>(new Point(bx, by), newBounds, depth);
cellNode = new PointNode<T>(new Point(bx, by), newBounds, depth,
this.maxDepth, this.maxElements);
this.nodes.put(Cell.TOP_LEFT, cellNode);

// top right
cellNode = new PointNode<T>(new Point(newXStartCoordinate, by),
newBounds, depth);
newBounds, depth, this.maxDepth, this.maxElements);
this.nodes.put(Cell.TOP_RIGHT, cellNode);

// bottom left
cellNode = new PointNode<T>(new Point(bx, newYStartCoordinate),
newBounds, depth);
newBounds, depth, this.maxDepth, this.maxElements);
this.nodes.put(Cell.BOTTOM_LEFT, cellNode);

// bottom right
cellNode = new PointNode<T>(new Point(newXStartCoordinate,
newYStartCoordinate), newBounds, depth);
newYStartCoordinate), newBounds, depth, this.maxDepth,
this.maxElements);
this.nodes.put(Cell.BOTTOM_RIGHT, cellNode);
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/at/jotschi/quadtree/point/PointQuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public PointQuadTree(Point startCoordinates, Dimension size) {
super(startCoordinates, size);
this.rootNode = new PointNode<T>(startCoordinates, size, 0);
}

public PointQuadTree(Point startCoordinates, Dimension size, int maxDepth, int maxChildren) {
super(startCoordinates, size);
this.rootNode = new PointNode<T>(startCoordinates, size, 0,maxDepth,maxChildren);
}

/**
* Add a new element to the QuadTree
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/at/jotschi/quadtree/RenderPointQuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected RenderPointQuadTree() {
*/
protected PointQuadTree<String> createQuadTree() {
PointQuadTree<String> tree = new PointQuadTree<String>(new Point(100,
100), new Dimension(400, 400));
100), new Dimension(400, 400), 8, 4);

for (int i = 0; i < 200; i++) {
int x = (int) (Math.random() * 1000 * 0.4) + 100;
Expand Down Expand Up @@ -88,7 +88,7 @@ protected void drawCells(PointNode<String> node, Graphics g) {
public void drawSelectedElements(Graphics g) {
g.setColor(Color.RED);
for (AbstractNodeElement<String> element : selectedElements) {
g.drawOval((int) element.getX(), (int) element.getY(), 4, 4);
g.drawOval((int) element.getX()-2, (int) element.getY()-2, 4, 4);
}
g.setColor(Color.BLACK);
}
Expand All @@ -103,7 +103,9 @@ public void drawElements(PointNode node, Graphics g) {
Vector<AbstractNodeElement<String>> elements = (Vector<AbstractNodeElement<String>>) node
.getElements();
for (AbstractNodeElement<String> element : elements) {
g.drawOval((int) element.getX(), (int) element.getY(), 4, 4);
int x = (int) element.getX();
int y = (int) element.getY();
g.drawLine(x, y, x, y);
}
}

Expand Down

0 comments on commit 76d8a09

Please sign in to comment.