Skip to content

Commit

Permalink
apply linear binning
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
Fil authored and mbostock committed Jun 5, 2021
1 parent 29d1447 commit 392b887
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/density.js
Expand Up @@ -32,13 +32,21 @@ export default function() {
function density(data) {
var values0 = new Float32Array(n * m),
values1 = new Float32Array(n * m);
var pow2k = Math.pow(2, -k);

data.forEach(function(d, i, data) {
var xi = (+x(d, i, data) + o) >> k,
yi = (+y(d, i, data) + o) >> k,
var xi = (x(d, i, data) + o) * pow2k,
yi = (y(d, i, data) + o) * pow2k,
wi = +weight(d, i, data);
if (xi >= 0 && xi < n && yi >= 0 && yi < m) {
values0[xi + yi * n] += wi;
var x0 = Math.floor(xi),
y0 = Math.floor(yi),
xt = xi - x0 - 0.5,
yt = yi - y0 - 0.5;
values0[x0 + y0 * n] += (1 - xt) * (1 - yt) * wi;
values0[x0 + 1 + y0 * n] += xt * (1 - yt) * wi;
values0[x0 + 1 + (y0 + 1) * n] += xt * yt * wi;
values0[x0 + (y0 + 1) * n] += (1 - xt) * yt * wi;
}
});

Expand Down

0 comments on commit 392b887

Please sign in to comment.