0

I'm trying to find a formula for spiral filling of an array.
Example:
0 1 2 3 4
11 12 13 14 5
10 9 8 7 6

My formula is based on "corners" of a spiral. The "corners" are bold in the example. With coordinates of the cell, we can find coordinates of biggest "corner" less than the number we want to find. With coordinates of "corner", we can find the index of this "corner". With the index of corner number, we can find the number. How to find the formula for corner number by its index?

Nishant Gupta
  • 3,533
  • 1
  • 11
  • 18
  • Possible duplicate of [Print 2-D Array in clockwise expanding spiral from center](https://stackoverflow.com/questions/33684970/print-2-d-array-in-clockwise-expanding-spiral-from-center) – Spektre Mar 26 '18 at 13:24
  • see the duplicate and apply it in reverse order ... – Spektre Mar 26 '18 at 13:25
  • I'm voting to close this question as off-topic because it's not about programming –  Mar 27 '18 at 00:31

1 Answers1

0

Strong clue:
Let's width of array is W and height is H. We can see the next sequence:

0
(W-1)
(W-1) + (H-1) 
(W-1) + (H-1) + (W-1) 
(W-1) + (H-1) + (W-1) + (H-2)  //full round, increment shifts
(W-1) + (H-1) + (W-1) + (H-2) + (W-2)
...

If you want to make a formula, note multiplicity of W and H, arithmetic progressions, and remainders by modulo 2 and 4

MBo
  • 77,366
  • 5
  • 53
  • 86