Question

So I'm trying to find an algorithm for animating a grid radially from a chosen cell.

For example, let's say I have the following grid and the cell with the "o" is the chosen one to start from:

x | x | x | x | x | 
x | x | x | x | x | 
x | x | x | o | x | 
x | x | x | x | x | 
x | x | x | x | x | 

and this should continue to go on this way:

x | x | x | x | x | 
x | x | x | o | x | 
x | x | o | o | o | 
x | x | x | o | x | 
x | x | x | x | x |

x | x | x | o | x | 
x | x | o | o | o | 
x | o | o | o | o | 
x | x | o | o | o | 
x | x | x | o | x |

x | x | o | o | o | 
x | o | o | o | o | 
o | o | o | o | o | 
x | o | o | o | o | 
x | x | o | o | o |

etc. So I basically need to loop through and every time I do so, I need the coordinates for the new cells that need to be switched in cartesian coordinates (ie x,y)

Was it helpful?

Solution

The following python-like pseudocode will accomplish this, although it will switch some cells multiple times. So let switch be only from x to o, without effect on cells which already are in state o.

for i in 0 .. size: # iteration
  for j in 0 .. i: # both end points are inclusive
    switch(x - i + j, y + j)
    switch(x - i + j, y - j)
    switch(x + i - j, y + j)
    switch(x + i - j, y - j)

OTHER TIPS

You can simply run over the grid (this portion of code can be optimized) and change x to y if it has adjacent o's. Then run again and change all y's to o's.

Another solution will be to save initial o location (x0, y0) and change all x to o if abs(x - x0) + abs(y - y0) = i on ith iteration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top