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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #include <iostream> #include <string>
class Point { public: int x, y; Point(int _x, int _y): x(_x), y(_y) {} };
class Object { public: Point position; std::string name; Object(Point _position, std::string _name): position(_position), name(_name) {} };
class Quadtree { private: Point top_left, bottom_right; Quadtree *top_left_child, *top_right_child, *bottom_left_child, *bottom_right_child; Object *object;
bool hasChildren(); public: Quadtree(Point _top_left, Point _bottom_right):\ top_left(_top_left), bottom_right(_bottom_right),\ object(NULL), top_left_child(NULL), top_right_child(NULL),\ bottom_left_child(NULL), bottom_right_child(NULL) {}
~Quadtree();
void insert(Object *object); Object* search(Point point);
bool in_top_left_child(Point point); bool in_top_right_child(Point point); bool in_bottom_left_child(Point point); bool in_bottom_right_child(Point point);
bool in_boundary(Point point); };
bool Quadtree::hasChildren() { return top_left_child != NULL; }
bool Quadtree::in_top_left_child(Point point) { return point.x <= top_left_child->bottom_right.x && point.y <= top_left_child->bottom_right.y; }
bool Quadtree::in_top_right_child(Point point) { return point.x > top_left_child->bottom_right.x && point.x <= top_right_child->bottom_right.x && point.y <= top_right_child->bottom_right.y; }
bool Quadtree::in_bottom_left_child(Point point) { return point.x <= bottom_left_child->bottom_right.x && point.y > top_left_child->bottom_right.y && point.y <= bottom_left_child->bottom_right.y; }
bool Quadtree::in_bottom_right_child(Point point) { return point.x > top_left_child->bottom_right.x && point.y > top_left_child->bottom_right.y && point.x <= bottom_right_child->bottom_right.x && point.y <= bottom_right_child->bottom_right.y; }
bool Quadtree::in_boundary(Point point) { return point.x >= top_left.x && point.x <= bottom_right.x && point.y >= top_left.y && point.y <= bottom_right.y; }
void Quadtree::insert(Object* object) { if (!in_boundary(object->position)) return;
if (top_left.x == bottom_right.x && top_left.y == bottom_right.y) { if (this->object == NULL) this->object = object; return; }
if (!hasChildren()) { int x = (top_left.x + bottom_right.x) / 2; int y = (top_left.y + bottom_right.y) / 2; top_left_child = new Quadtree(top_left, Point(x, y)); top_right_child = new Quadtree(Point(x + 1, top_left.y), Point(bottom_right.x, y)); bottom_left_child = new Quadtree(Point(top_left.x, y + 1), Point(x, bottom_right.y)); bottom_right_child = new Quadtree(Point(x + 1, y + 1), bottom_right); }
if (in_top_left_child(object->position)) top_left_child->insert(object); else if (in_top_right_child(object->position)) top_right_child->insert(object); else if (in_bottom_left_child(object->position)) bottom_left_child->insert(object); else bottom_right_child->insert(object); }
Object* Quadtree::search(Point point) { if (!in_boundary(point)) return NULL;
if (top_left.x == bottom_right.x && top_left.y == bottom_right.y) return object;
if (hasChildren()) { if (in_top_left_child(point)) return top_left_child->search(point); else if (in_top_right_child(point)) return top_right_child->search(point); else if (in_bottom_left_child(point)) return bottom_left_child->search(point); else return bottom_right_child->search(point); }
return NULL; }
Quadtree::~Quadtree() { delete top_left_child; delete top_right_child; delete bottom_left_child; delete bottom_right_child; delete object; }
int main() { Quadtree qt(Point(0, 0), Point(8, 8));
Object obj1(Point(1, 1), "Object 1"); Object obj2(Point(2, 5), "Object 2"); Object obj3(Point(7, 7), "Object 3");
qt.insert(&obj1); qt.insert(&obj2); qt.insert(&obj3);
Object* result1 = qt.search(Point(1, 1)); Object* result2 = qt.search(Point(2, 5)); Object* result3 = qt.search(Point(7, 7)); Object* result4 = qt.search(Point(3, 3));
std::cout << "Test Insert and Search:" << std::endl; std::cout << "Search (1, 1): " << (result1 != NULL ? result1->name : "NULL") << std::endl; std::cout << "Search (2, 5): " << (result2 != NULL ? result2->name : "NULL") << std::endl; std::cout << "Search (7, 7): " << (result3 != NULL ? result3->name : "NULL") << std::endl; std::cout << "Search (3, 3): " << (result4 != NULL ? result4->name : "NULL") << std::endl; return 0; }
|