-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
93 lines (79 loc) · 3.03 KB
/
index.html
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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="dist/simplequad.umd.js"></script>
<!-- <script src="dist/simplequad.umd.min.js"></script> -->
<script>
const randomBoundGenerators = [
createRandomCircle,
createRandomPoint,
createRandomBoundingBox,
];
const maxCircleRadius = 15;
const minCircleRadius = 5;
const maxBoundingBoxDimension = 15;
const minBoundingBoxDimension = 5;
function createRandomBound(bounds) {
const randomBoundGeneratorIndex = Math.floor(Math.random() * randomBoundGenerators.length);
return randomBoundGenerators[randomBoundGeneratorIndex](bounds);
}
function createRandomCircle(bounds) {
const r = Math.max(maxCircleRadius * Math.random(), minCircleRadius);
const x = (bounds.x + bounds.width - r) * Math.random() + bounds.x + r;
const y = (bounds.y + bounds.height - r) * Math.random() + bounds.y + r;
return {
x,
y,
r,
};
}
function createRandomPoint(bounds) {
const x = (bounds.x + bounds.width) * Math.random() + bounds.x;
const y = (bounds.y + bounds.height) * Math.random() + bounds.y;
return {
x,
y,
};
}
function createRandomBoundingBox(bounds) {
const width = Math.max(maxBoundingBoxDimension * Math.random(), minBoundingBoxDimension);
const height = Math.max(maxBoundingBoxDimension * Math.random(), minBoundingBoxDimension);
const x = (bounds.x + bounds.width - width) * Math.random() + bounds.x;
const y = (bounds.y + bounds.height - height) * Math.random() + bounds.y;
return {
x,
y,
width,
height,
};
}
function init() {
const numObjectsToAdd = 1000000;
const capacity = 5;
const bounds = {
x: 0,
y: 0,
width: 800,
height: 600,
};
const quadTree = SimpleQuad.createQuadTree(bounds, capacity);
let startTime = performance.now();
let duration;
for (let i = 0; i < numObjectsToAdd; i++) {
quadTree.add(createRandomBound(quadTree.bounds));
}
duration = (performance.now() - startTime) / 1000; // in seconds
console.log(`Took ${duration} seconds to add ${numObjectsToAdd} objects`);
startTime = performance.now();
const results = quadTree.query(quadTree.bounds);
duration = (performance.now() - startTime) / 1000; // in seconds
console.log(`Took ${duration} seconds to query for objects`);
}
init();
</script>
</body>
</html>