Help With Getting Positions Around Block

Hello! I need help writing a function that can get positions on a grid.

I need the function to take 2 inputs, the center position and the size of the grid.

The function should then get a grid of positions with the middle of the grid being that center position. Yes this does mean that it can only take odd numbers for grid sizes. Do note the grid is horizontal meaning that the positions will be on x and z.

Example:

function(Vector3.new(0, 0, 0), 3)

should return {(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 0, 1), (-1, 0, 1), (-1, 0, 0), (-1, 0, -1), (0, 0, -1), (1, 0, -1)}

Also I do not care about the order that the positions are returned in.

Maybe I misunderstand the question, but shouldn’t function(Vector3.new(0, 0, 0), 3) return (0, 0, 0), (0, 1.5, 0), (1.5, 0, 1.5), (0, 0, 1.5), (-1.5, 0, 1.5), (-1.5, 0, 0), (-1.5, 0, -1.5), (0, 0, -1.5), (1.5, 0, -1.5)} instead because the size is 3?

image

You should be able to derive all of the corners of an n size cube by just multiplying the coordinates of a unit cube by n.

image

You should be able to do something like this to get the corners of an n sized cube at any given origin.

--// didnt test this code and wrote it pretty quickly but it should work? 
local function getVertices(origin, size)
    local unit = {
        Vector3.new(.5, -.5, .5),
        Vector3.new(.5, .5, .5),
        Vector3.new(.5, .5, -.5),
        Vector3.new(-.5, .5, -.5),
        Vector3.new(-.5, .5, .5),
        Vector3.new(-.5, -.5, .5),
        Vector3.new(.5, -.5, -.5),
        Vector3.new(-.5, -.5, -.5)
    }

    local vertices = {}

    for i, vertice in pairs(unit) do
        vertices[i] = origin + (vertice * size)
    end

    return vertices
end
1 Like

My bad by size of 3 I meant a 3x3 grid.

So the solution you gave me would not work for this purpose.

I managed to work together some hacky code that should be able to do what you want? (Small bug I am not sure how to fix without just checking for a duplicate)

Works by iterating from 0 to 2^size - 1 and converting the current iteration into binary. Each number in the binary sequence is the respective component of a coordinate in the corners of an n-th dimensional cube.

local function toBinary(decimal)
    if (decimal == 0) then
        return { 0 } 
    end

    local binary = {}

    while decimal > 0 do
        local remainder = decimal % 2

        binary[#binary + 1] = remainder
        decimal = (decimal - remainder) / 2
    end

    return binary
end

local function addPadding(array, iterations)
    for i = 1, iterations - #array do
        table.insert(array, 1, "0")
    end
end

local function getVertices(size)
    local vertices = {}

    for i = 0, (2^size) - 1 do
        local vertex = {}
        local binary = toBinary(i)

        addPadding(binary, size)

        for i, component in pairs(binary) do
            vertex[#vertex + 1] = component
        end

        vertices[#vertices + 1] = vertex
    end

    return vertices
end

local vertices = getVertices(4)

for i, vertex in pairs(vertices) do
    print(table.concat(vertex))
end

image