Skip to content

Instantly share code, notes, and snippets.

@Yogsther
Last active March 1, 2018 22:05
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 Yogsther/5934e5b9ae514674341beed4061a9653 to your computer and use it in GitHub Desktop.
Save Yogsther/5934e5b9ae514674341beed4061a9653 to your computer and use it in GitHub Desktop.
Get index from coordinates, or vise versa with this simple trick.
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