-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadtree.ts
143 lines (132 loc) · 4.49 KB
/
quadtree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// must import from .js file, not .ts file
import init, { Vec2, WasmQuadTree, WasmVec2Array } from "./pkg/boids.js";
let canvas = document.getElementById("canvas") as HTMLCanvasElement;
let ctx = canvas.getContext("2d");
let inputText = document.getElementById("input-text") as HTMLParagraphElement;
let inputCache = 0;
let totalPointsText = document.getElementById(
"total-points"
) as HTMLParagraphElement;
let clearPointsButton = document.getElementById(
"clear-points"
) as HTMLButtonElement;
const POINT_RADIUS = 3;
const DRAG_DIST = 20;
function isDigit(key: string) {
return key.length == 1 && "0" <= key && key <= "9";
}
function drawTreeRecur(
tree: WasmQuadTree,
nodeIdx: number,
tlCorner: Vec2,
dims: Vec2
) {
// draw the node's borders
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.rect(tlCorner.x, tlCorner.y, dims.x, dims.y);
ctx.stroke();
// draw the node's points
ctx.fillStyle = "red";
ctx.strokeStyle = "red";
for (let i = 0; i < tree.node_len(nodeIdx); i++) {
let pos = tree.node_item_pos(nodeIdx, i);
ctx.beginPath();
ctx.arc(pos.x, pos.y, POINT_RADIUS, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}
let shift = dims.div_num(2);
let childIdx = tree.node_children(nodeIdx);
let shiftDims = [Vec2.zero(), Vec2.new(1, 0), Vec2.new(0, 1), Vec2.from(1)];
childIdx.forEach((idx, i) => {
if (idx === 0) return;
drawTreeRecur(
tree,
idx,
tlCorner.add_vec(shift.mul_vec(shiftDims[i])),
shift
);
});
}
function drawTree(tree: WasmQuadTree) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawTreeRecur(tree, 0, Vec2.zero(), tree.dims());
}
function drawCirclePoints(center: Vec2, radius: number, points: WasmVec2Array) {
ctx.fillStyle = "green";
ctx.strokeStyle = "green";
for (let i = 0; i < points.len(); i++) {
let pos = points.get(i);
ctx.beginPath();
ctx.arc(pos.x, pos.y, POINT_RADIUS, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}
ctx.fillStyle = "blue";
ctx.strokeStyle = "blue";
ctx.beginPath();
ctx.arc(center.x, center.y, POINT_RADIUS, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.arc(center.x, center.y, radius, 0, Math.PI * 2);
ctx.stroke();
}
init().then(() => {
// wasm initialized
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let dims = Vec2.new(canvas.width, canvas.height);
let tree = WasmQuadTree.new(dims);
let mouseDownPos = Vec2.zero();
function windowClick(pos: Vec2) {
tree.push(pos);
drawTree(tree);
totalPointsText.innerText = `Total points: ${tree.len()}`;
}
function windowDrag(startPos: Vec2, endPos: Vec2) {
let radius = startPos.distance(endPos);
let points = tree.query_circle(startPos, radius);
drawTree(tree);
drawCirclePoints(startPos, radius, points);
}
window.addEventListener("mousedown", (event) => {
mouseDownPos = Vec2.new(event.clientX, event.clientY);
});
window.addEventListener("mouseup", (event) => {
let mouseUpPos = Vec2.new(event.clientX, event.clientY);
if (mouseDownPos.distance(mouseUpPos) < DRAG_DIST) {
windowClick(mouseUpPos);
} else {
windowDrag(mouseDownPos, mouseUpPos);
}
});
window.addEventListener("keydown", (event) => {
if (isDigit(event.key)) {
inputCache = inputCache * 10 + parseInt(event.key);
inputText.innerText = `Add points: ${inputCache}`;
} else if (event.key == "Backspace") {
inputCache = Math.max(0, Math.floor(inputCache / 10));
inputText.innerText = `Add points: ${inputCache}`;
} else if (event.key == "Enter") {
for (let i = 0; i < inputCache; i++) {
tree.push(
Vec2.new(Math.random(), Math.random()).mul_vec(tree.dims())
);
}
drawTree(tree);
inputCache = 0;
inputText.innerText = `Add points: ${inputCache}`;
totalPointsText.innerText = `Total points: ${tree.len()}`;
}
});
clearPointsButton.addEventListener("click", () => {
tree.clear();
totalPointsText.innerText = `Total points: ${tree.len()}`;
inputCache = 0;
inputText.innerText = `Add points: ${inputCache}`;
drawTree(tree);
});
});