-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
228 lines (216 loc) · 6.08 KB
/
index.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const BRUSH_MAX = 10;
const TEXT_SIZE = 12;
const CANVAS_SCALE = 0.8;
const DRAW_SELECTION = [{
label: 'Paint Points',
draw(sketch) {
if (sketch.mouseIsPressed) {
doPaintPoints(sketch, sketch.mouseX, sketch.mouseY);
}
}
}, {
label: 'Select Points',
draw(sketch, startX, startY) {
if (sketch.mouseIsPressed) {
doSelectPoints(sketch, startX, startY, sketch.mouseX, sketch.mouseY);
}
}
}];
var canvas;
var sketch;
var quadTree;
var w;
var h;
var treeCapacity;
var drawSwitch;
var curDraw;
var brush;
var p5;
onload = () => {
initP5();
brush = 1;
curDraw = DRAW_SELECTION[0];
document.getElementById('clear').addEventListener('click', () => {
quadTree.clear();
});
const content = document.getElementById('dropdown-content');
const dropdownButton = document.getElementById('dropbtn');
dropdownButton.textContent = curDraw.label;
for (let i = 0; i < DRAW_SELECTION.length; i++) {
let a = document.createElement('a');
a.onclick = () => {
curDraw = DRAW_SELECTION[i];
dropdownButton.textContent = curDraw.label;
content.classList.toggle('show');
};
a.href = '#';
a.textContent = DRAW_SELECTION[i].label;
a.classList = ['dropdown-content-a'];
content.appendChild(a);
}
dropdownButton.addEventListener('click', () => content.classList.toggle('show'));
const b = document.getElementById('brush');
b.value = brush;
b.oninput = () => {
brush = parseInt(b.value);
}
const t = document.getElementById('tree-capacity');
const tv = document.getElementById('tree-capacity-value');
t.value = treeCapacity;
tv.innerText = 'Capacity: ' + treeCapacity;
t.oninput = () => {
treeCapacity = parseInt(t.value);
tv.innerText = 'Capacity: ' + treeCapacity;
quadTree.clear()
quadTree = new QuadTree(new Quadrant(w, h, h, w), treeCapacity);
}
}
function initP5() {
var startX, startY;
p5 = new p5(
(sketch) => {
sketch.mousePressed = (e) => {
startX = sketch.mouseX;
startY = sketch.mouseY;
return e.target.id !== canvas.canvas.id;
}
sketch.touchStarted = (e) => {
if (e.target.id === canvas.canvas.id) {
startX = sketch.mouseX;
startY = sketch.mouseY;
return false;
}
return true;
};
sketch.touchMoved = (e) => {
return e.target.id !== canvas.canvas.id;
};
sketch.setup = () => {
treeCapacity = 4;
h = ((window.innerHeight > 0) ? window.innerHeight : screen.height) * CANVAS_SCALE;
w = ((window.innerWidth > 0) ? window.innerWidth : screen.width) * CANVAS_SCALE;
canvas = sketch.createCanvas(w, h);
quadTree = new QuadTree(new Quadrant(w, h, h, w), treeCapacity);
};
sketch.draw = () => {
let visitor = new Visitor(sketch)
sketch.background(0);
quadTree.accept(visitor);
sketch.strokeWeight(1);
sketch.fill(0, 0, 255);
sketch.stroke(0, 0, 255);
sketch.textSize(TEXT_SIZE);
sketch.text(`Total sub-trees: ${visitor.subTrees}`, w * 0.005, h * 0.03);
sketch.text(`Total points: ${quadTree.size()}`, w * 0.005, h * 0.07);
curDraw.draw(sketch, startX, startY);
};
},
'sketch-holder');
}
function doPaintPoints(sketch, x, y) {
if (x <= w && y <= h) {
for (let i = 0; i < brush; ++i) {
quadTree.insert(new Point(Math.fround(x + sketch.random(-brush, brush)), Math.fround(y + sketch.random(-brush, brush))))
}
}
}
function doSelectPoints(sketch, startX, startY, x, y) {
sketch.stroke(0, 255, 0);
sketch.noFill();
const cq = new CountingQuery();
const selection = getSelection(startX, startY, x, y);
const result = cq.query(quadTree, selection);
sketch.rect(selection.x, selection.y, selection.w * 2, selection.h * 2);
sketch.stroke(255, 0, 0);
for (let p of result.resList) {
sketch.strokeWeight(3);
sketch.point(p.x, p.y);
}
sketch.strokeWeight(1);
sketch.textSize(TEXT_SIZE);
sketch.fill(255, 0, 0);
let c = result.resList.length;
let s = result.steps;
sketch.text(`Found ${c} points in ${s} steps`, selection.x - selection.w, (selection.y - selection.h) * .97);
}
function getSelection(startX, startY, x, y) {
let x2 = (x + startX) / 2;
let y2 = (y + startY) / 2
if (startX > x) {
// left half
if (startY > y) {
// lower right
let y1 = startY - y2;
let x1 = startX - x2;
return new Quadrant(x2, y2, y1, x1);
} else {
// upper right
let y1 = y2 - startY;
let x1 = startX - x2;
return new Quadrant(x2, y2, y1, x1);
}
} else {
// right half
if (startY > y) {
// lower left
let y1 = startY - y2;
let x1 = x2 - startX;
return new Quadrant(x2, y2, y1, x1);
} else {
// upper left
let y1 = y2 - startY;
let x1 = x2 - startX;
return new Quadrant(x2, y2, y1, x1);
}
}
}
class Visitor {
constructor(sketch) {
this.sketch = sketch;
this.subTrees = -1; // skip root
}
visit(qt) {
this.subTrees++;
this.sketch.stroke(255);
this.sketch.strokeWeight(1);
this.sketch.noFill();
this.sketch.rectMode(this.sketch.CENTER);
this.sketch.rect(qt.quadrant.x, qt.quadrant.y, qt.quadrant.w * 2, qt.quadrant.h * 2);
if (qt.divided) {
qt.upLeft.accept(this);
qt.upRight.accept(this);
qt.downLeft.accept(this);
qt.downRight.accept(this);
}
for (let p of qt.points) {
this.sketch.strokeWeight(3);
this.sketch.point(p.x, p.y);
}
}
}
class CountingQuery {
query(qt, quadrant, result) {
if (!result) {
result = {
resList: [],
steps: 0
};
}
if (qt.quadrant.intersetcs(quadrant)) {
for (let p of qt.points) {
if (quadrant.contains(p)) {
result.resList.push(p);
result.steps++;
}
}
if (qt.divided) {
result.steps += 4;
this.query(qt.upLeft, quadrant, result);
this.query(qt.upRight, quadrant, result);
this.query(qt.downLeft, quadrant, result);
this.query(qt.downRight, quadrant, result);
}
}
return result;
}
}