aboutsummaryrefslogtreecommitdiff
path: root/src/QuadTree.cpp
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2024-02-29 19:04:34 +0100
committerPaul Oliver <contact@pauloliver.dev>2024-02-29 19:16:14 +0100
commite6ab4a8ed100d5d5b7611c74cf3ccd556f1f1d71 (patch)
tree129cf13c2f9b3eae54402300db4570815789a02a /src/QuadTree.cpp
Initial commitHEADmaster
Diffstat (limited to 'src/QuadTree.cpp')
-rw-r--r--src/QuadTree.cpp55
1 files changed, 55 insertions, 0 deletions
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 <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);
+ }
+}