summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-05-25 00:32:15 -0500
committersanine <sanine.not@pm.me>2022-05-25 00:32:15 -0500
commit49b3a5cf1ea2d7ed3f1bf9c1262ee92d66a83a7d (patch)
tree9ff02afce4a73c01ed43b581c780f5715ee05b7d
parent2f2ab5149701243647a5f8d60dc9f771e45b2a5a (diff)
begin KDTree implementation
-rw-r--r--README.md9
-rw-r--r--modules/KDTree.js33
-rw-r--r--modules/KDTree.test.js12
3 files changed, 47 insertions, 7 deletions
diff --git a/README.md b/README.md
index 54bcc0e..808183f 100644
--- a/README.md
+++ b/README.md
@@ -9,12 +9,7 @@ running
bluerose is written in vanilla js and doesn't require any building. Simply spin up a web server (I recommend python3's `http.server`) and load `index.html`!
-To run the tests:
-
-```
-npm
-npm test
-```
+To run the tests, just do `node test.js`.
credits
@@ -22,7 +17,7 @@ credits
bluerose was written by me! (sanine)
-I've used [gorhill's Javascript-Voronoi] algorithm in performing terrain generation.
+I've used [rhill's Javascript-Voronoi] algorithm in performing terrain generation.
The terrain and mapping algorithms were very heavily inspired by mewo2's Uncharted Atlas, which has an [excellent explanatory page].
diff --git a/modules/KDTree.js b/modules/KDTree.js
new file mode 100644
index 0000000..d9133d1
--- /dev/null
+++ b/modules/KDTree.js
@@ -0,0 +1,33 @@
+'use strict';
+
+class NodeType {
+ static Leaf = new NodeType('Leaf');
+ static Branch = new NodeType('Branch');
+
+ constructor(name) { this.name = name; }
+ toString() { return `NodeType.${this.name}`; }
+}
+
+
+class LeafNode {
+ constructor(point) {
+ this.type = NodeType.Leaf;
+ this.point = point;
+ }
+}
+class BranchNode {
+}
+
+
+class KDTree {
+ constructor() {
+ this.root = null;
+ }
+
+ insert(point) {
+ this.root = new LeafNode(point);
+ }
+}
+
+export { KDTree, NodeType, LeafNode, BranchNode };
+export default KDTree;
diff --git a/modules/KDTree.test.js b/modules/KDTree.test.js
new file mode 100644
index 0000000..4e2657b
--- /dev/null
+++ b/modules/KDTree.test.js
@@ -0,0 +1,12 @@
+import { test, assert } from './test-assert.js';
+
+import { KDTree, NodeType } from './KDTree.js';
+
+test('single point does not split tree', () => {
+ let tree = new KDTree();
+ tree.insert({x: 0.5, y: 0.5 });
+ assert.deepEqual(tree.root.type, NodeType.Leaf);
+ assert.ok(tree.root.point);
+ assert(tree.root.point.x = 0.5);
+ assert(tree.root.point.y = 0.5);
+});