-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtree.cpp
73 lines (65 loc) · 1.34 KB
/
quadtree.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
#include "quadtree.h"
#include <random>
#include <iostream>
/*
* Rough test of quadtree
* Brock Jackman
*/
using namespace tree;
struct Point
{
double x, y, z;
};
struct Triangle
{
Point points[3];
};
bool within(const Triangle& shape, const Rect<double>& r)
{
for(auto& p : shape.points) {
if(p.x < r.xMin || p.x > r.xMax || p.y < r.yMin || p.y > r.yMax) {
return false;
}
}
return true;
}
std::ostream& operator<<(std::ostream& os, const Triangle& t)
{
for(auto& p : t.points) {
os << '[' << p.x << ", " << p.y << ", " << p.z << ']';
os << '\t';
}
return os;
}
int main()
{
std::default_random_engine rng;
std::uniform_real_distribution<double> dist1(0, 10);
std::uniform_real_distribution<double> dist2(-0.1, 0.1);
QuadTree<double, Triangle> qTree(0, 10, 0, 10);
for(int i = 0; i < 10000; ++i) {
Triangle tri;
double x = dist1(rng);
double y = dist1(rng);
for(auto& p : tri.points) {
p.x = x + dist2(rng);
p.y = y + dist2(rng);
p.z = 0;
}
qTree.insert(tri);
}
auto tree2 = qTree;
auto it = tree2.beginAt(1,1);
auto end = tree2.end();
while(it != end) {
std::cout << *it << std::endl;
++it;
}
qTree.clear();
it = qTree.beginAt(1,1);
std::cout << "Printing cleared tree:";
while(it != end) {
std::cout << *it << std::endl;
++it;
}
}