example1_basic_implementaion.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <vector>
#include <cmath>

class Point {
public:
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) {}
};

class QuadTree {
private:
    static const int MAX_CAPACITY = 4;
    static const int MAX_DEPTH = 6;

    struct Node {
        std::vector<Point> points;
        Node* children[4];
        double x, y, width, height;
        int depth;

        Node(double x, double y, double width, double height, int depth)
            : x(x), y(y), width(width), height(height), depth(depth) {
            for (int i = 0; i < 4; ++i) children[i] = nullptr;
        }

        ~Node() {
            for (int i = 0; i < 4; ++i) delete children[i];
        }
    };

    Node* root;

    bool isLeaf(Node* node) const {
        return node->children[0] == nullptr;
    }

    int getQuadrant(Node* node, const Point& p) const {
        double midX = node->x + node->width / 2;
        double midY = node->y + node->height / 2;
        if (p.x < midX) {
            return (p.y < midY) ? 0 : 2;
        } else {
            return (p.y < midY) ? 1 : 3;
        }
    }

    void split(Node* node) {
        double subWidth = node->width / 2;
        double subHeight = node->height / 2;
        int depth = node->depth + 1;
        node->children[0] = new Node(node->x, node->y, subWidth, subHeight, depth);
        node->children[1] = new Node(node->x + subWidth, node->y, subWidth, subHeight, depth);
        node->children[2] = new Node(node->x, node->y + subHeight, subWidth, subHeight, depth);
        node->children[3] = new Node(node->x + subWidth, node->y + subHeight, subWidth, subHeight, depth);
    }

    void insert(Node* node, const Point& p) {
        if (isLeaf(node)) {
            if (node->points.size() < MAX_CAPACITY || node->depth >= MAX_DEPTH) {
                node->points.push_back(p);
            } else {
                split(node);
                for (const auto& point : node->points) {
                    insert(node->children[getQuadrant(node, point)], point);
                }
                node->points.clear();
                insert(node->children[getQuadrant(node, p)], p);
            }
        } else {
            insert(node->children[getQuadrant(node, p)], p);
        }
    }

public:
    QuadTree(double width, double height) : root(new Node(0, 0, width, height, 0)) {}
    ~QuadTree() { delete root; }

    void insert(const Point& p) {
        insert(root, p);
    }

    void print(Node* node, int level = 0) const {
        if (node == nullptr) return;

        std::string indent(level * 2, ' ');
        std::cout << indent << "Node at (" << node->x << ", " << node->y << ") with size " 
                  << node->width << "x" << node->height << std::endl;

        if (isLeaf(node)) {
            for (const auto& p : node->points) {
                std::cout << indent << "  Point: (" << p.x << ", " << p.y << ")" << std::endl;
            }
        } else {
            for (int i = 0; i < 4; ++i) {
                print(node->children[i], level + 1);
            }
        }
    }

    void print() const {
        print(root);
    }
};

int main() {
    QuadTree qt(100, 100);

    qt.insert(Point(10, 10));
    qt.insert(Point(20, 20));
    qt.insert(Point(30, 30));
    qt.insert(Point(40, 40));
    qt.insert(Point(50, 50));
    qt.insert(Point(60, 60));
    qt.insert(Point(70, 70));
    qt.insert(Point(80, 80));

    qt.print();

    return 0;
}
Back to quadtree