Last active
March 21, 2023 09:39
-
-
Save reveriejake/8ca31f3da702d548646f70ed27b6bc91 to your computer and use it in GitHub Desktop.
An example of a simplified spiral loop algorithm for iterating in a square.
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
public static class SpiralLoop | |
{ | |
public static void ExecuteCenterToOutter(int width, System.Action<int, int> callback) | |
{ | |
// Run the loop from the center outwards | |
for (int r = 0; r < width; r++) | |
{ | |
// If we are on center, do not run the edge loops | |
if (r == 0) | |
{ | |
callback(0, 0); | |
continue; | |
} | |
// For each edge of the square, run a loop from 0-size | |
// Call one each of the edges using simple transformations | |
for (int i = -r; i < r; i++) | |
{ | |
callback(i, r); | |
callback(r, -i); | |
callback(-r, i); | |
callback(-i, -r); | |
} | |
} | |
} | |
public static void ExecuteOutterToCenter(int width, System.Action<int, int> callback) | |
{ | |
// Run the loop from the outside edges to the center | |
for (int r = width-1; r >= 0; r--) | |
{ | |
// If we are on center, do not run the edge loops | |
if (r == 0) | |
{ | |
callback(0, 0); | |
continue; | |
} | |
// For each edge of the square, run a loop from 0-size | |
// Call one each of the edges using simple transformations | |
for (int i = -r; i < r; i++) | |
{ | |
callback(i, r); | |
callback(r, -i); | |
callback(-r, i); | |
callback(-i, -r); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment