Last active
December 26, 2020 16:11
-
-
Save joemaddalone/c257cefc5aee64e4c7a55fce51b45f5d to your computer and use it in GitHub Desktop.
bounce in a two-dimensional array
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
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