Page 1 of 1
Wraparound a Table of Tables [Solved]
Posted: Sun Sep 18, 2022 8:41 am
by NoreoAlles
I am trying to make a wrap around cellular automaton and i am stuck on trying to make it wraparound to count a cells neighboors.
totalN is the amount of neighboors with the have the value of one (alive neighboors)
WIDTH and HEIGHT is reffering to the table, in the example 9*9
and i am checking each cells 8 neighboors in a grid like this:
Code: Select all
grid = {
{1,0,0}
{0,1,1}
{1,1,1}
}
for y, xs in ipairs(grid) do
for x, value in ipairs(xs) do
local totalN = 0
for j=-1,1,1 do
for k=-1, 1, 1 do
if x+k < 1 then
cx = x+k + WIDTH else cx = x+k
end
if x+k > WIDTH then
cx = 0 else cx = x+k
end
if y+j < 1 then
cy = y+j + HEIGHT else cy = y+j
end
if y+j > HEIGHT then
cy = 0 else cx = y+j
end
if grid[cy][cx] == 1 then
totalN = totalN + 1
end
end
end
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 8:51 am
by BrotSagtMist
Thats what modulo is for.
If you say wrap every USE of an objects x position in x%100 it will always act as if its limited to 0-99.
Like pos1%100==pos2%100.
You dont need to do that on assign, so in reality stuff can wander of the screen to near inf but the position check and renderer themself simply wrap them.
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 8:58 am
by NoreoAlles
BrotSagtMist wrote: ↑Sun Sep 18, 2022 8:51 am
Thats what modulo is for.
If you say wrap every USE of an objects x position in x%100 it will always act as if its limited to 0-99.
Like pos1%100==pos2%100.
You dont need to do that on assign, so in reality stuff can wander of the screen to near inf but the position check and renderer themself simply wrap them.
Im sorry, could you give me a example i dont get it.
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 9:02 am
by darkfrei
Just use two functions: getValue and setValue:
(w and h are width and height of the map)
Code: Select all
function getValue (map, x, y, w, h)
x = (x-1)%w+1
y = (y-1)%h+1
return map[y][x]
end
Code: Select all
function setValue (map, x, y, w, h, value)
x = (x-1)%w+1
y = (y-1)%h+1
map[y][x] = value
end
Where x in the map is in range of [1, w] and y in range [1,h]
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 9:11 am
by darkfrei
Also
Code: Select all
dxys = {
{x=0,y=-1},
{x=1,y=-1},
{x=1,y=0},
{x=1,y=1},
{x=0,y=1},
{x=-1,y=1},
{x=-1,y=0},
{x=-1,y=-1},
}
Then
Code: Select all
x0 = 1
y0 = 1
for k, dxy in ipairs(dxys) do
local x, y = x0+dxy.x, y0+dxy.y
-- other code with neighbor x and y
end
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 9:20 am
by NoreoAlles
darkfrei wrote: ↑Sun Sep 18, 2022 9:11 am
Also
Code: Select all
dxys = {
{x=0,y=-1},
{x=1,y=-1},
{x=1,y=0},
{x=1,y=1},
{x=0,y=1},
{x=-1,y=1},
{x=-1,y=0},
{x=-1,y=-1},
}
Then
Code: Select all
x0 = 1
y0 = 1
for k, dxy in ipairs(dxys) do
local x, y = x0+dxy.x, y0+dxy.y
-- other code with neighbor x and y
end
Im still doing soemthing wrong, but cant get what.
Ill just give you the source
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 9:23 am
by darkfrei
Here
Code: Select all
newGen = empty -- not equal, but same table, coping below makes nothing
for y, xs in ipairs(empty) do
for x, value in ipairs(xs) do
newGen[y][x] = empty[y][x]
end
end
Must be:
Code: Select all
newGen = {}
for y, xs in ipairs(empty) do
newGen[y] = {}
for x, value in ipairs(xs) do
newGen[y][x] = value -- except table values
end
end
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 9:26 am
by NoreoAlles
darkfrei wrote: ↑Sun Sep 18, 2022 9:23 am
Here
Code: Select all
newGen = empty -- not equal, but same table, coping below makes nothing
for y, xs in ipairs(empty) do
for x, value in ipairs(xs) do
newGen[y][x] = empty[y][x]
end
end
I know that the code is uneccesary, but it worked when i was only checking each cell if it wasnt on the "edge"
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 12:38 pm
by ReFreezed
What isn't working? The logic seem to wrap around fine. I tried drawing this on the right edge:
Running the simulation shows it wrapping around to the left side. Same happens vertically.
Re: Wraparound a Table of Tables
Posted: Sun Sep 18, 2022 12:58 pm
by NoreoAlles
ReFreezed wrote: ↑Sun Sep 18, 2022 12:38 pm
What isn't working? The logic seem to wrap around fine. I tried drawing this on the right edge:
Running the simulation shows it wrapping around to the left side. Same happens vertically.
Whoops, you´re right, i just messed up when counting the cell as its own neighboor!
Thanks to everbody who helped me with this issue!