quadtree-drawer/js/tree.js
2024-06-04 15:15:43 +02:00

42 lines
832 B
JavaScript

// QUADTREE
import {None, Some} from "./option.js";
export function Tree() {
let parent = None();
let children = None();
let color = "#000000";
const node = {
set_parent: p => { parent = Some(p); },
get_parent: () => parent,
create_children: () => {
children.none(() => {
let _children = Array(4);
for (let i = 0; i < 4; i++) {
_children[i] = Tree();
_children[i].set_color(color);
_children[i].set_parent(node);
}
children = Some(_children);
});
},
get_children: () => children,
set_color: c => { color = c; },
get_color: () => color,
to_string: () => {
return children
.some(cs => {
return Some(`{color: "${color}", children: [${
cs.map(c => c.to_string()).join(", ")
}]}`);
})
.unwrap(`{color: "${color}"}`);
},
};
return node;
}