Skip to content
Snippets Groups Projects
Commit c92299d7 authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Added quad tree classes to be used for LOD-system.

parent 1671807b
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,5 @@
</component>
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/idatt2003" vcs="Git" />
</component>
</project>
\ No newline at end of file
package edu.ntnu.stud.chaosgame.controller.game;
import javafx.geometry.Point2D;
import javafx.scene.shape.Rectangle;
public class Quadtree {
private QuadtreeNode root;
public Quadtree(Rectangle bounds) {
root = new QuadtreeNode(bounds, 0);
}
public QuadtreeNode findNodeContainingPoint(Point2D point) {
return findNodeContainingPoint(root, point);
}
private QuadtreeNode findNodeContainingPoint(QuadtreeNode node, Point2D point) {
if (node.isLeaf()) {
return node;
}
for (QuadtreeNode child : node.getChildren()) {
if (child.getBounds().contains(point)) {
return findNodeContainingPoint(child, point);
}
}
return null;
}
// other methods omitted for brevity
}
package edu.ntnu.stud.chaosgame.controller.game;
import javafx.scene.shape.Rectangle;
public class QuadtreeNode {
private QuadtreeNode parent;
private QuadtreeNode[] children;
private boolean isLeaf;
private int levelOfDetail;
private Rectangle bounds;
public QuadtreeNode(Rectangle bounds, int levelOfDetail) {
this.bounds = bounds;
this.levelOfDetail = levelOfDetail;
this.isLeaf = true;
this.children = new QuadtreeNode[4];
}
public void split() {
if (!isLeaf) {
return;
}
double halfWidth = bounds.getWidth() / 2;
double halfHeight = bounds.getHeight() / 2;
children[0] = new QuadtreeNode(new Rectangle(bounds.getX(), bounds.getY(), halfWidth, halfHeight), levelOfDetail + 1);
children[1] = new QuadtreeNode(new Rectangle(bounds.getX() + halfWidth, bounds.getY(), halfWidth, halfHeight), levelOfDetail + 1);
children[2] = new QuadtreeNode(new Rectangle(bounds.getX(), bounds.getY() + halfHeight, halfWidth, halfHeight), levelOfDetail + 1);
children[3] = new QuadtreeNode(new Rectangle(bounds.getX() + halfWidth, bounds.getY() + halfHeight, halfWidth, halfHeight), levelOfDetail + 1);
isLeaf = false;
}
public void merge() {
if (isLeaf) {
return;
}
for (int i = 0; i < 4; i++) {
children[i] = null;
}
isLeaf = true;
}
public Rectangle getBounds() {
return this.bounds;
}
public QuadtreeNode[] getChildren() {
return this.children;
}
public Boolean isLeaf() {
return this.isLeaf;
}
// getters and setters omitted for brevity
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment