Skip to main content Link Search Menu Expand Document (external link)

Spiral

Draw a spiral in an s by s grid. Or better, given the coordinates of a grid cell, determine in constant time its color.

Compare with nested squares

Consider a different drawing, where in the grid we draw alternating black and white nested squares. Here the color of a pixel is simply the parity of its distance to the boundary, which is the minimum of its distance to the four borders.

We observe that the differences are in pixels of the form row i and column i+1, for all i between 1 and some value depending on the size s. Let’s call this value f(s). By drawing spirals of different sizes we observe the following pattern.

s 3 4 5 6 7 8 9 10 11 12
f(s) 1 1 2 3 3 3 4 5 5 5

Hence we can guess

\[f(s) = 2 * ((s - 1) / 4) + ( (s \% 4) == 1 ? 0 : 1).\]

Comments