Created
March 21, 2020 12:12
-
-
Save cheeaun/85274bbea89c06cbdc3eb42732f6d318 to your computer and use it in GitHub Desktop.
Resize (2D but flat) array of values with nearest neighbour algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const width = 300; | |
const height = 200; | |
const newWidth = width * 2; | |
const newHeight = height * 2; | |
const xRatio = width / newWidth; | |
const yRatio = height / newHeight; | |
const resizeValues = (id, values) => { | |
const newValues = new Array(newWidth * newHeight); | |
for (let i = 0; i < newHeight; i++) { | |
for (let j = 0; j < newWidth; j++) { | |
const x = ~~(j * xRatio); // ~~ = Math.floor but faster | |
const y = ~~(i * yRatio); | |
newValues[i * newWidth + j] = values[y * width + x]; | |
} | |
} | |
return newValues; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment