-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
78 lines (59 loc) · 1.58 KB
/
canvas.js
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
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.addEventListener("mousedown", addPoint, false);
canvas.addEventListener("mousemove", getPosition, false);
function getPosition(event) {
mouseX = event.x - canvas.offsetLeft;
mouseY = event.y - canvas.offsetTop;
//console.clear();
//console.log("Hay " + qt.query(range, []).length + " puntos en el área verde.")
}
function addPoint(event) {
let x = event.x - canvas.offsetLeft;
let y = event.y - canvas.offsetTop;
p = new Point(x, y)
qt.insert(p)
}
let mouseX = 0;
let mouseY = 0;
let range;
let qt = new Quadtree(
new Boundary({
x: canvas.width / 2,
y: canvas.height / 2
}, {
w: canvas.width / 2,
h: canvas.height / 2
}))
/*
for (let i = 0; i < 300; i++) {
p = new Point(Math.random() * canvas.width, Math.random() * canvas.height)
qt.insert(p)
}*/
function show() {
requestAnimationFrame(show)
range = new Boundary({
x: mouseX,
y: mouseY
}, {
w: 25,
h: 25
})
qt.draw()
// Rango
context.save()
context.strokeStyle = 'green'
context.strokeRect(range.center.x - range.half.w,
range.center.y - range.half.h,
range.half.w * 2,
range.half.h * 2)
context.restore()
context.beginPath()
context.fillStyle = 'black'
context.fillText(qt.query(range, []).length, range.center.x, range.center.y)
context.fill()
context.closePath()
}
show()