Last active
March 1, 2018 22:05
-
-
Save Yogsther/5934e5b9ae514674341beed4061a9653 to your computer and use it in GitHub Desktop.
Get index from coordinates, or vise versa with this simple trick.
This file contains hidden or 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
let matrix = new Array(width * height); | |
function coordinatesToIndex(x, y) { | |
return x + (width * y); | |
} | |
function indexToCoordinates(index) { | |
let x = index % width; | |
let y = (index - x) / width; | |
return { x: x, y: y }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment