# How do I index a table in cubes?

I have absolutely no idea what this would be called so I’m just going to call it “indexing with cubes”. Anyways, let’s say I have a table like this:

``````{
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3}
}
``````

I’m trying to index it by 4s in all directions so I could get something like this, ignoring if anything else exists that’s less than 4.

``````1111 2222
1111 2222
1111 2222
1111 2222
``````

Idek if this is coherent, and if it’s not then don’t reply ig? I spent multiple hours trying last night but to no result.

I don’t know if there is a legit way to do this(like a built-in function)
but you can try this:

``````local array = {
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3}
}

function cut(array, amount)
local result = {}
for i = 1, #array, amount do
local last = i+amount-1
if array[last] then
local num = ""
for j = i, last do
num ..= tostring(array[j])
end
num = tonumber(num)
table.insert(result, num)
else
break
end
end
return table.unpack(result)
end

for i, v in pairs(array) do
print(cut(v, 4))
end
``````
``````local arrays = {
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3},
{1,1,1,1,2,2,2,2,3}
}

local newTable = {}

for i, array in pairs(arrays) do
for index, num in pairs(array) do
local newArray = {}
for i = 1, 4 do
if num <= 4 then
local newNum = tonumber(string.rep(tostring(num), 4))
table.insert(newArray, newNum)
end
end
table.insert(newTable, newArray)
end
end

for i, array in pairs(newTable) do
for index, num in pairs(array) do
print(num)
end
end
``````

Output:

Do you mean that you want to find all the 4 by 4 squares consisting of the same number in the table?

1 Like

Yes, is that possible? My brain hurts trying to figure it out on my own. Specifically they’re colors, but I can adjust for that.

Weeelll it makes sense but there are some edge cases. For example:

``````111122
111112
111112
111112
111112
222222
``````

There are three different 4x4 squares of 1s, all of them overlapping. Do you want to find all of them? Could you also explain a bit about what this is for? There might be a simpler way of doing it, and it helps a lot trying to understand what’s going on

Sorry, I’m creating a pixel art importer. I have an external site that handles the pixel data and converts every pixel of the image into an RGB value and returns it in rows. This is very fast despite how resource-intensive it sounds. That’s not the part I’m worried about.

I want to import a pixel art image and have it detect where there’s a consistent amount of pixels in one spot and register that as a part.

I know I can do this by hand, but some programmers are notorious for automating something and spending 50x longer than it’d actually take to do it by hand. It’d also be helpful for any block games I make in the future.

1 Like

No, read the post above that I responded to the guy who asked what I’m doing.

2 Likes

Still not sure what the end goal is. Do you want to recreate the image perfectly but using as few parts as possible? Or do you want to recreate it imperfectly, only in specific spots? If it’s the former, check out greedy meshing

I wanted to see if it was possible to minimize the amount of pixels from like 120,000 to like 500. This’d be specific to pixel art only since it has giant (in the world of pixels) regions of color for each “part/pixel” of the drawing.

1 Like

That’s not really a yes or a no xD

Do you want to turn this

into this?

Ohh, not quite. Let’s say I have an image like this. (not going to actually use it because the stock art logo would mess someth up)

I believe this has a little over 1 million pixels, and I want to convert that into the cubes that are visible to us if that makes any sense. Don’t get me wrong, I can render the 1 million pixels but my computer would not be powerful enough.

I see!

If you can figure out how many pixels are in each “big pixel”, say 24, then you just need to take every (24*24th) pixel, like overlaying a 24x24 grid on the image and only taking the pixels where grid lines intersect. It doesn’t matter if you read from e.g. the upper left corner, the center, or any other point. As long as you then go 24px to the right you will end up at the next pixel.

1 Like

Bruh, I’m so dumb lol tysm

I overanalyze things sometimes, like I was fr trying to index every single pixel and combine groups of them and then compare if they had all the same color and then do something with that

1 Like