Fill Matrix in Spiral Form from center

I’m working on an island game, the lobby is in the center, but I want the islands that people create to naturally fill up around the lobby, in a grid formation. See the link I posted. Now it was solved, but I don’t know how to translate the c++ code into Roblox, primarily because I don’t understand what they’re doing. How would I fill a matrix in a spiral form from the center?

Honestly, the easiest way would be to put a bunch of parts in your workspace, place them how you want, and just manually name them with numbers in order.

Then you can just grab that list:

local spawnPoints = table.sort(workspace.SpawnPoints:GetChildren(), function(a, b) return a.Name < b.Name end)

…and now you have a list of spawn points you can loop through, fill in old ones, whatever. Dead simple, readable, and extendable to more interesting shapes later.

1 Like

are you sure you need this specific algorithm to place your islands?

Nice, but there’s a possibility that I might need hundreds of these, and it’s going to be a hassle. I need this algorithm for it, I’m more of a math person.

C++
int size = 7;
int matrix[size][size];
int dy[] = { 1,  0, -1, 0 };
int dx[] = { 0, -1,  0, 1 };
int directionCount = 4;

int ringCount = (size - 1) / 2;
int y = ringCount;
int x = ringCount;
int repeatCount = 0;
int value = 1;

matrix[y][x] = value++;
for (int ring = 0; ring < ringCount; ring++)
{
    y--;
    x++;
    repeatCount += 2;
    for (int direction = 0; direction < directionCount; direction++)
        for (int repeat = 0; repeat < repeatCount; repeat++)
        {
            y += dy[direction];
            x += dx[direction];
            matrix[y][x] = value++;
        }
}
Lua code
local size = 7 -- must be an odd integer otherwise there will be no centre in the matrix 
local matrix = {} -- the matrix itself
local delta = {1, 0, -1, 0} -- ive noticed dx is a reverse of dy so we could use one variable for that
local directionCount = 4 -- how many turns to make in one ring

local ringCount = (size - 1) / 2 -- how many rings there are
local y = ringCount
local x = y
local repeatCount = 0 -- how many numbers to add to the matrix on each turn
local value = 0 -- the number that we are going to increment for every position in matrix 

matrix[y] = {}
matrix[y][x] = value
value += 1

for ring = 1, ringCount do
    y += -1
    x += 1
    repeatCount += 2
    for dir = 1, directionCount do
        for rep = 1, repeatCount do
            y = y + delta[dir]
            x = x + delta[directionCount - dir + 1]
            matrix[y] = matrix[y] or {}
            matrix[y][x] = value
            value += 1
        end
    end
end
1 Like

Haha, thanks for this. I was watching you change the script. I’ll test it out, hopefully it’s solid.

found some bugs hold on let me fix
im typing from phone so it might take some time

I’ll fix the bugs for myself, but if someone else needs this code I guess you can go ahead and that.


kind of forgot lua and c++ have different array starting indexes also mistyped some variables
fixed in edit

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.