-
Notifications
You must be signed in to change notification settings - Fork 3
/
quadtree.test.js
76 lines (66 loc) · 1.4 KB
/
quadtree.test.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
const { insert, search, nearest } = require('./quadtree');
const assert = require('assert');
{
console.log('Quadtree - searches for points within a boundary');
const quadtree = {
boundary: {
x1: 0,
x2: 8,
y1: 0,
y2: 8,
},
points: [],
};
const p1 = { x: 1, y: 1 };
const p2 = { x: 2, y: 2 };
const p3 = { x: 4, y: 4 };
const p4 = { x: 6, y: 6 };
const p5 = { x: 3, y: 7 };
insert(quadtree, p1);
insert(quadtree, p2);
insert(quadtree, p3);
insert(quadtree, p4);
insert(quadtree, p5);
assert.deepStrictEqual(
search(quadtree, {
x1: 3,
y1: 3,
x2: 5,
y2: 5,
}),
[p3]
);
assert.deepStrictEqual(
search(quadtree, {
x1: 3,
y1: 3,
x2: 7,
y2: 7,
}),
[p3, p5, p4]
);
}
{
console.log('Quadtree - returns the nearest point');
const quadtree = {
boundary: {
x1: 0,
y1: 0,
x2: 8,
y2: 8,
},
points: [],
};
const p1 = { x: 1, y: 1 };
const p2 = { x: 2, y: 2 };
const p3 = { x: 6, y: 6 };
const p4 = { x: 2, y: 7 };
const p5 = { x: 2, y: 3 };
insert(quadtree, p1);
insert(quadtree, p2);
insert(quadtree, p3);
insert(quadtree, p4);
assert.deepStrictEqual(nearest(quadtree, { x: 2, y: 3 }), { point: p2, distance: 1 });
insert(quadtree, p5);
assert.deepStrictEqual(nearest(quadtree, { x: 2, y: 3 }), { point: p5, distance: 0 });
}