Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created February 12, 2020 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixpickle/e4c23b5fe19132891e69d10310e40608 to your computer and use it in GitHub Desktop.
Save unixpickle/e4c23b5fe19132891e69d10310e40608 to your computer and use it in GitHub Desktop.
Bilinear interp
const canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 400;
const ctx = canvas.getContext('2d');
ctx.font = '350px serif';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillText('Hi', 200, 200);
const data = ctx.getImageData(0, 0, 400, 400);
(x, y, z) => {
if (x < -1 || x > 1 || z < -1 || z > 1 || y > -0.5) {
return false;
}
const x1 = (x + 1) * 199;
const y1 = (z + 1) * 199;
let sum = 0;
for (let i = 0; i < 2; i++) {
const x = Math.floor(x1) + i;
const w1 = 1 - Math.abs(x - x1);
for (let j = 0; j < 2; j++) {
const y = Math.floor(y1) + j;
const w2 = 1 - Math.abs(y - y1);
const idx = 4 * (x + y*400);
sum += w1*w2*data.data[idx+3];
}
}
return sum > 128;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment