aboutsummaryrefslogtreecommitdiff
path: root/src/QuadTree.cpp
blob: d6686ff6b5faffd45ae1cde226ee44035b3762b1 (plain)
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
#include <HyperNeat/QuadTree.hpp>

using namespace std;
using namespace hyperneat;

QuadTree::QuadTree(double segment, double x, double y)
    : _segment(segment), _x(x), _y(y)
{}

double
QuadTree::getSegment() const
{
    return _segment;
}

double
QuadTree::getX() const
{
    return _x;
}

double
QuadTree::getY() const
{
    return _y;
}

void
QuadTree::subdivide(Function<bool(QuadTree*)> subdivider)
{
    if (subdivider(this)) {
        double newSeg = _segment / 2.0;
        _children.resize(4);
        _children[0] = {newSeg, _x - newSeg, _y - newSeg};
        _children[1] = {newSeg, _x + newSeg, _y - newSeg};
        _children[2] = {newSeg, _x - newSeg, _y + newSeg};
        _children[3] = {newSeg, _x + newSeg, _y + newSeg};

        for (auto &i : _children) {
            i.subdivide(subdivider);
        }
    }
}

void
QuadTree::traverse(Function<void(const QuadTree*)> traverser) const
{
    if (!_children.empty()) {
        for (auto &i : _children) {
            i.traverse(traverser);
        }
    } else {
        traverser(this);
    }
}