Created
May 27, 2022 18:28
-
-
Save LIMPIX31/975b8354e71a56cf851ffdb19077496a to your computer and use it in GitHub Desktop.
Square spiral
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 sspiral = (n, step = 1) => { | |
const t = Math.sqrt(n) >> 0 | |
const start = t * t | |
let result = [] | |
if (t % 2) | |
if (n - start <= t) result = [-(t - 1) / 2 + n - start, -(t + 1) / 2] | |
else result = [(t + 1) / 2, -(t + 1) / 2 + n - (start + t)] | |
else if (n - start <= t) result = [t / 2 - (n - start), t / 2] | |
else result = [-t / 2, t / 2 - n + (t + start)] | |
return result.map(r => r * step >> 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment