Search code examples
algorithmluacell

How to get Manhattan cells' values around another cell by radius?


I'm having some issues finding an algorithm to get the cells' values around a cell.

Here is a picture I modified explaining the situation : Cellules with their respective values

I want to put in a table all the light blue values, that are around the dark blue cell in a radius of x (3 in the picture).

I tried to find some formulas on the Internet and also here but found nothing.


Solution

  • local cell = 369
    local r = 3
    for x = -r, r do 
        for y = -r, r do 
            if math.abs(x) + math.abs(y) <= r then 
                local new_cell = cell + 29*math.floor(x/2) - 27*math.floor(y/2) + (x+y)%2 * (math.floor(cell/14)%2 + 14*(x%2-y%2)) + x*(x+y+1)%2
                print(new_cell)
            end 
        end 
    end