Skip to content

Instantly share code, notes, and snippets.

@joemaddalone
Last active December 26, 2020 16:11
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 joemaddalone/c257cefc5aee64e4c7a55fce51b45f5d to your computer and use it in GitHub Desktop.
Save joemaddalone/c257cefc5aee64e4c7a55fce51b45f5d to your computer and use it in GitHub Desktop.
bounce in a two-dimensional array
export function* bounce(arr) {
let x = 0;
let y = 0;
let mx = 1;
let my = 1;
const w = arr.length - 1;
const h = arr[0].length - 1;
while (true) {
if (mx + x > w || mx + x < 0) {
mx = mx * -1;
}
if (my + y > h || my + y < 0) {
my = my * -1;
}
x = x + mx;
y = y + my;
yield arr[x][y];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment