Hello! All I’m looking for is an algorithm which will load products of any size onto a shelf of any size perfectly so that the shelf is filled completely with little to no space left.
So I think the parts start from the “Start” and keep generating next to eachother until they reach the opposite corner, then they will generate to the next corner, however I am not sure how to fill in the blank area in the middle once the corners are done.
To recap:
An algorithm which loads a product of ANY SIZE into this shelf making sure as many products as possible can be filled and NO OVERLAPPING PRODUCTS or products hanging off the shelf AT ALL.
The product is called “Cola” in replicatedStorage AND ITS A MODEL but I’d like a function which can host a product in the parameters so I can load any product since there is more than one.
Because for there to be no products hanging of the shelf, the size of each product must be a factor of the total size of shelf i.e size of shelf % size of product = 0
(when I refer to “block” I mean the product that gets loaded)
You can use Area Size ÷ Block Size to get the amounts of blocks that can be fit inside within that area
For example:
Area: 10, 15
Block Size: 2, 3
10 / 2 = 5 blocks can fit on the X axis
15 / 3 = 5 blocks can fit on the Y axis
————————————————————————
Area: 6.5, 37.4
Block size: 4.2, 8.35
6.5 / 4.2 = 1.54761905 ≈ 1 block can fit on the X axis
37.4 / 8.35 = 4.47904192 ≈ 4 blocks can fit on the Y axis
As you see in the examples, to get the amount of blocks that can fit on one axis is to divide the area by the block size (A ÷ S). The result should then be rounded down (floor(A ÷ S)) to not have any hanging blocks
The code would go something like this, but I can’t test it out right now
local function LoadProduct(product: BasePart, area: BasePart)
local size = product.Size
local x = math.floor(area.Size.X/size.X)
local y = math.floor(area.Size.Y/size.Y)
local pos = (area.CFrame*CFrame.new(area.CFrame.UpVector*area.Size.Y-area.CFrame.RightVector*area.Size.X)
for i = 1, x*y do
local clone = product:Clone()
clone.Position = pos
clone.Parent = workspace
pos += Vector3.new(x,0,0)
if i % x == 0 then
pos -= Vector3.new(0,y,0)
end
end
end