上一篇
在计算机图形学、游戏开发和地理信息系统中,C++四叉树实现是一种非常重要的空间划分技术。本教程将手把手教你如何用C++从零开始构建一个完整的四叉树结构,即使你是编程小白也能轻松上手!
四叉树(Quadtree)是一种树形数据结构,用于对二维空间进行递归划分。每个内部节点都有恰好四个子节点,分别代表父区域的四个象限:西北(NW)、东北(NE)、西南(SW)和东南(SE)。

四叉树常用于:
在实现四叉树数据结构前,我们需要明确几个核心概念:
下面我们一步步用C++实现一个简单的四叉树。
struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {}};class Rectangle {public: double x, y, width, height; Rectangle(double x, double y, double width, double height) : x(x), y(y), width(width), height(height) {} // 判断点是否在矩形内 bool contains(const Point& p) const { return (p.x >= x - width / 2 && p.x <= x + width / 2 && p.y >= y - height / 2 && p.y <= y + height / 2); } // 判断与另一个矩形是否相交 bool intersects(const Rectangle& range) const { return !(range.x - range.width / 2 > x + width / 2 || range.x + range.width / 2 < x - width / 2 || range.y - range.height / 2 > y + height / 2 || range.y + range.height / 2 < y - height / 2); }};class QuadTree {private: static const int CAPACITY = 4; // 每个节点最大点数 Rectangle boundary; std::vector points; QuadTree* northWest = nullptr; QuadTree* northEast = nullptr; QuadTree* southWest = nullptr; QuadTree* southEast = nullptr; bool divided = false;public: QuadTree(Rectangle rect) : boundary(rect) {} ~QuadTree() { delete northWest; delete northEast; delete southWest; delete southEast; } // 插入点 bool insert(Point p) { if (!boundary.contains(p)) return false; if (points.size() < CAPACITY) { points.push_back(p); return true; } if (!divided) { subdivide(); } if (northWest->insert(p)) return true; if (northEast->insert(p)) return true; if (southWest->insert(p)) return true; if (southEast->insert(p)) return true; return false; } // 分裂当前节点 void subdivide() { double x = boundary.x; double y = boundary.y; double w = boundary.width / 2; double h = boundary.height / 2; northWest = new QuadTree(Rectangle(x - w/2, y - h/2, w, h)); northEast = new QuadTree(Rectangle(x + w/2, y - h/2, w, h)); southWest = new QuadTree(Rectangle(x - w/2, y + h/2, w, h)); southEast = new QuadTree(Rectangle(x + w/2, y + h/2, w, h)); divided = true; // 将现有点重新分配到子节点 for (const auto& p : points) { northWest->insert(p) || northEast->insert(p) || southWest->insert(p) || southEast->insert(p); } points.clear(); // 清空当前节点的点 } // 查询范围内的所有点 void query(const Rectangle& range, std::vector& found) const { if (!boundary.intersects(range)) return; for (const auto& p : points) { if (range.contains(p)) found.push_back(p); } if (divided) { northWest->query(range, found); northEast->query(range, found); southWest->query(range, found); southEast->query(range, found); } }}; 下面是一个简单的使用场景,展示如何用C++空间分割来管理2D空间中的点:
#include #include int main() { // 创建一个覆盖 [0,0] 到 [100,100] 的根区域 Rectangle rootRect(50, 50, 100, 100); QuadTree qt(rootRect); // 插入一些随机点 qt.insert(Point(10, 10)); qt.insert(Point(20, 30)); qt.insert(Point(80, 70)); qt.insert(Point(90, 90)); qt.insert(Point(50, 50)); // 查询左下角 30x30 区域内的点 Rectangle queryRange(15, 15, 30, 30); std::vector results; qt.query(queryRange, results); std::cout << "Found " << results.size() << " points in query range:\n"; for (const auto& p : results) { std::cout << "(" << p.x << ", " << p.y << ")\n"; } return 0;} 通过本四叉树教程,你已经掌握了如何用C++实现一个基础但功能完整的四叉树结构。这种C++四叉树实现方法可以显著提升二维空间查询效率,特别适用于需要频繁进行区域查询或碰撞检测的应用场景。
下一步你可以尝试:
希望这篇教程对你有所帮助!如果你有任何问题,欢迎在评论区留言交流。
本文由主机测评网于2025-12-04发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025123030.html