Skip to content

Commit

Permalink
Update demo to insert random points to the tree
Browse files Browse the repository at this point in the history
  • Loading branch information
coocos committed Nov 20, 2020
1 parent fe5415a commit 5347552
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ const points = pointsWithinArea(tree, {

## Demo

[This demo](https://lamsa.dev/quadtree/) draws the quadtree using canvas and allows you to add new points to the tree by clicking on the canvas.
[This demo](https://lamsa.dev/quadtree/) visualizes the quadtree using canvas and keeps adding new random points to tree, thus branching the tree deeper and deeper.
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { insert, construct, pointsWithinArea } from "./quadtree";
import { insert, construct } from "./quadtree";
import { initializeCanvas } from "./canvas";

const canvas = initializeCanvas();

const points = Array.from({ length: 384 }, () => {
const randomPoint = () => {
return {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
};
});
};

const points = Array.from({ length: 256 }, randomPoint);
let tree = construct(points, {
x: 0,
y: 0,
Expand All @@ -18,11 +19,7 @@ let tree = construct(points, {
});

canvas.draw(tree);
canvas.element.addEventListener("click", (event) => {
const point = {
x: event.clientX,
y: event.clientY,
};
tree = insert(tree, point);
setInterval(() => {
tree = insert(tree, randomPoint());
canvas.draw(tree);
});
}, 250);
1 change: 1 addition & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
}
body {
margin: 0;
overflow: hidden;
}
#canvas {
width: 100vw;
Expand Down

0 comments on commit 5347552

Please sign in to comment.