I want to implement insert method for a quadtree. In this quadtree i will insert certain shapes like rectangle, triangle, circle and diamond. All shaoes come with an rectangle border(all shaoes are considered rectangles in this method). My method works for few inputs, but for most of all i get the error mentioned.
void insert(Node parent, GeometricObj shape) {
switch(parent.getType()) {
case EMPTY:
this.setShapeForNode(parent, shape);
break;
case LEAF:
if(needSplit(parent, shape)) {
this.setShapeForNode(parent, shape);
}
else {
this.split(parent);
this.insert(parent, shape);
for (int i = 0; i < parent.Shapes.size(); i++) {
this.insert(parent, parent.Shapes.get(i));
}
}
break;
case PARENT:
for (int i = 0; i < getQuadrantForShape(parent, shape.getX(), shape.getY(),
shape.getWidth(), shape.getHeight()).size(); i++) {
if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(0))) {
this.insert(parent.Quads.get(0), shape);
}
if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(1))) {
this.insert(parent.Quads.get(1), shape);
}
if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(2))) {
this.insert(parent.Quads.get(2), shape);
}
if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(3))) {
this.insert(parent.Quads.get(3), shape);
}
}
break;
}
}
Split method is this:
void split(Node node) {
node.setType(NodeType.PARENT);
double x = node.getX();
double y = node.getY();
double halfwidth = node.getWidth()/ 2;
double halfheight = node.getHeight()/ 2;
node.setQuad1(new Node(x + halfwidth, y + halfheight, halfwidth, halfheight, node));
node.setQuad2(new Node(x, y + halfheight, halfwidth, halfheight, node));
node.setQuad3(new Node(x, y, halfwidth, halfheight, node));
node.setQuad4(new Node(x + halfwidth, y, halfwidth, halfheight, node));
node.Quads.add(node.quad1);
node.Quads.add(node.quad2);
node.Quads.add(node.quad3);
node.Quads.add(node.quad4);
}
GetQuadrantForShape method returns a list of quadrants for the new shape.
void setShapeForNode(Node node, GeometricObj shape) {
if (node.getType() != NodeType.PARENT) {
node.setType(NodeType.LEAF);
node.Shapes.add(shape);
}
}
If someone has a clue of what the problem could be, or what am I doing wrong, or any hint of what can I improve, please tell me. If you need any other class or method I'm gonna attach it here.
UPDATE:
boolean needSplit(Node node, GeometricObj shape) {
boolean result = false;
for (int i = 0; i < node.Shapes.size(); i++) {
GeometricObj o = node.Shapes.get(i);
result = this.overlaps(shape.getX(), shape.getY(), shape.getWidth(), shape.getHeight(), o);
if(result == false) {
break;
}
}
return result;
}
boolean overlaps (double x, double y, double width, double height, GeometricObj r) {
return x < r.getX() + r.getWidth() && x + width > r.getX() && y < r.getY() + r.getHeight() && y + height > r.getY();
}