Published
Edited
Oct 7, 2019
1 fork
12 stars
Insert cell
Insert cell
Insert cell
bilinearInterpolator = func => (x, y) => {
// "func" is a function that takes 2 integer arguments and returns some value
const x1 = Math.floor(x);
const x2 = Math.ceil(x);
const y1 = Math.floor(y);
const y2 = Math.ceil(y);

if ((x1 === x2) && (y1 === y2)) return func(x1, y1);
if (x1 === x2) {
return (func(x1, y1) * (y2 - y) + func(x1, y2) * (y - y1)) / (y2 - y1);
}
if (y1 === y2) {
return (func(x1, y1) * (x2 - x) + func(x2, y1) * (x - x1)) / (x2 - x1);
}

// else: x1 != x2 and y1 != y2
return (
func(x1, y1) * (x2 - x) * (y2 - y) +
func(x2, y1) * (x - x1) * (y2 - y) +
func(x1, y2) * (x2 - x) * (y - y1) +
func(x2, y2) * (x - x1) * (y - y1)
)
/ ((x2 - x1) * (y2 - y1));
}
Insert cell
Insert cell
sampleFunc = (x, y) => {
if ((x === 0) && (y === 0)) return 0;
if ((x === 0) && (y === 1)) return 1;
if ((x === 1) && (y === 0)) return 1;
if ((x === 1) && (y === 1)) return 0.5;
}
Insert cell
interpolation = {
const interpolate = bilinearInterpolator(sampleFunc)
const n = 100;
return d3.range(0, 1, 1/n).map(y => (
d3.range(0, 1, 1/n).map(x => {
return interpolate(x, y);
})
));
}
Insert cell
imshow(interpolation.reverse(), 2, d3.interpolateTurbo)
Insert cell
Insert cell
tile = {
const dem = await fetchTile({z: 13, x: 7262, y: 3232});
return dem.slice(10, 20).map(row => row.slice(20, 30));
}
Insert cell
Insert cell
Insert cell
interpolatedTile = {
const n = 40;
return d3.range(0, 9, 1/n).map(y => (
d3.range(0, 9, 1/n).map(x => {
const interpolate = bilinearInterpolator((i, j) => tile[j][i])
return interpolate(x, y);
})
));
}
Insert cell
imshow(interpolatedTile, 1, d3.interpolateTurbo)
Insert cell
Insert cell
nearestTile = {
const nearestInterpolator = func => (x, y) => func(Math.round(x), Math.round(y));
const n = 50;
return d3.range(0, 9, 1/n).map(y => (
d3.range(0, 9, 1/n).map(x => {
const interpolate = nearestInterpolator((i, j) => tile[j][i], x, y);
return interpolate(x, y);
})
));
}
Insert cell
imshow(nearestTile, 1, d3.interpolateTurbo)
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more