-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhilbert2.js
59 lines (49 loc) · 1.5 KB
/
hilbert2.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
// http://en.wikipedia.org/wiki/Hilbert_curve
var Point2 = require("./point2");
module.exports = Hilbert2;
function Hilbert2(resolution) {
this.resolution = resolution;
}
Hilbert2.prototype.encode = function (point) {
return encode(point, this.resolution);
};
Hilbert2.prototype.decode = function (scalar) {
return decodeInto(new Point2(), scalar, this.resolution);
};
Hilbert2.prototype.decodeInto = function (point, scalar) {
return decodeInto(point, scalar, this.resolution);
};
function encode(point, resolution) {
var rotation = point.constructor.zero.clone();
var scalar = 0;
for (var scale = resolution / 2; scale > 0; scale /= 2) {
rotation.x = point.x & scale;
rotation.y = point.y & scale;
scalar += scale * ((3 * rotation.x) ^ rotation.y);
rotate(scale, point, rotation);
}
return scalar;
}
function decodeInto(point, scalar, resolution) {
var rotation = point.constructor.zero.clone();
point.x = 0;
point.y = 0;
for (var scale = 1; scale < resolution; scale *= 2) {
rotation.x = 1 & (scalar / 2);
rotation.y = 1 & (scalar ^ rotation.x);
rotate(scale, point, rotation);
rotation.scaleThis(scale);
point.addThis(rotation);
scalar /= 4;
}
return point;
}
function rotate(scale, point, rotation) {
if (!rotation.y) {
if (rotation.x) {
point.x = scale - 1 - point.x;
point.y = scale - 1 - point.y;
}
point.transpose();
}
}