public class QuadTree { private static final int MAX_OBJECTS = 4; // 最大对象数 private static final int MAX_LEVELS = 5; // 最大深度 private Node root; private int levels; public QuadTree() { this.root = new Node(null, 0, 0, 100, 100); this.levels = 0; } private class Node { Rectangle bounds; List
八叉树实现
public class Octree { private static final int MAX_OBJECTS = 8; private static final int MAX_LEVELS = 5; private Node root; private int levels; public Octree() { this.root = new Node(null, new Vector3f(0, 0, 0), 100); this.levels = 0; } private class Node { Box bounds; List
public class QuadTree { private static final int MAX_OBJECTS = 4; // 最大对象数 private static final int MAX_LEVELS = 5; // 最大深度 private Node root; private int levels; public QuadTree() { this.root = new Node(null, 0, 0, 100, 100); this.levels = 0; } private class Node { Rectangle bounds; List
八叉树实现
public class Octree { private static final int MAX_OBJECTS = 8; private static final int MAX_LEVELS = 5; private Node root; private int levels; public Octree() { this.root = new Node(null, new Vector3f(0, 0, 0), 100); this.levels = 0; } private class Node { Box bounds; List
import java.awt.Rectangle; import java.util.ArrayList; import java.util.List; public class QuadTree { private static final int MAX_OBJECTS = 4; private static final int MAX_LEVELS = 5; private Node root; private int levels; public QuadTree(int x, int y, int width, int height) { root = new Node(null, x, y, width, height); levels = 0; } private class Node { Rectangle bounds; List
八叉树完整实现
import org.joml.Vector3f; // 假设Box是一个三维矩形类 class Box { public Vector3f center; public float size; public Box(Vector3f center, float size) { this.center = center; this.size = size; } } public class Octree { private static final int MAX_OBJECTS = 8; private static final int MAX_LEVELS = 5; private Node root; private int levels; public Octree(Vector3f center, float size) { root = new Node(null, center, size); levels = 0; } private class Node { Box bounds; List
import java.awt.Rectangle; import java.util.ArrayList; import java.util.List; public class QuadTree { private static final int MAX_OBJECTS = 4; private static final int MAX_LEVELS = 5; private Node root; private int levels; public QuadTree(int x, int y, int width, int height) { root = new Node(null, x, y, width, height); levels = 0; } private class Node { Rectangle bounds; List
完善八叉树
import org.joml.Vector3f; public class Octree { private static final int MAX_OBJECTS = 8; private static final int MAX_LEVELS = 5; private Node root; private int levels; public Octree(Vector3f center, float size) { root = new Node(null, center, size); levels = 0; } private class Node { Box bounds; List objects; Node[] nodes; public Node(Node parent, Vector3f center, float size) { bounds = new Box(center, size); objects = new ArrayList<>(); nodes = new Node[8]; } public boolean insert(Object obj) { if (nodes[0] != null) { for (int i = 0; i < 8; i++) { if (nodes[i].bounds.contains(obj.getPosition())) { return nodes[i].insert(obj); } } } objects.add(obj); if (objects.size() > MAX_OBJECTS && levels < MAX_LEVELS) { subdivide(); List tempObjects = new ArrayList<>(objects); objects.clear(); for (Object o : tempObjects) { insert(o); } } return true; } private void subdivide() { float subSize = bounds.size / 2.0f; Vector3f subCenter = new Vector3f(); for (int i = 0; i < 8; i++) { subCenter.set(bounds.center).add((i & 1) * subSize, ((i >> 1) & 1) * subSize, ((i >> 2) & 1) * subSize); nodes[i] = new Node(this, subCenter, subSize); } levels++; } } public boolean insert(Point3D obj) { return root.insert(obj); } }