From e6ab4a8ed100d5d5b7611c74cf3ccd556f1f1d71 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 29 Feb 2024 19:04:34 +0100 Subject: Initial commit --- src/QuadTree.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/QuadTree.cpp (limited to 'src/QuadTree.cpp') diff --git a/src/QuadTree.cpp b/src/QuadTree.cpp new file mode 100644 index 0000000..d6686ff --- /dev/null +++ b/src/QuadTree.cpp @@ -0,0 +1,55 @@ +#include + +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 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 traverser) const +{ + if (!_children.empty()) { + for (auto &i : _children) { + i.traverse(traverser); + } + } else { + traverser(this); + } +} -- cgit v1.2.1