Changing a 1D random placement code to 2D
Show older comments
I currently have a code that randomly places 2 different size grains/vechicles on a line of 10,000 spaces with no overlaps. I was wondering if there was an easy way to turn this into a 2D design that fills a 10,000x10,000 space with no overlaps. The shapes should be square so the small should be 3x3 and the large 10x10. The shapes also need to fill from bottom up. This should be something similar to tetris but with no rotation or movement.
x = [];
taken_spots = [];
%% Generate Random Numbers
s = 10000; %total number of available parking spots
n = s*5; %n = quantity of random numbers to generate
L = .1; %chance for large grain = L (0-L = Large grain, > L = small grain)
LG = 10; %large grain size
SG = 3; %small grain size
numbers = rand(n,1); %n number of random numbers to be used for vehicle size
spot = round(s*rand(n,1)); %random starting spots for each vehicle between 1 and s spot limit
is_LG = numbers <= L;
Y = SG*ones(1,n);
Y(is_LG) = LG;
nLG = 0;
nSG = 0;
for i = 1:n
x = spot(i)+(0:Y(i)-1);
if ~any(ismember(x, taken_spots))
taken_spots(end+1:end+Y(i)) = x; %add new spots to end of taken spots array
end
if is_LG(i)
nLG = nLG+1; %count number of large grains
else
nSG = nSG+1; %count number of small grains
end
end
density = length(taken_spots)/s*100
bar([nSG nLG])
xticklabels({'# Small' '# Large'})
2 Comments
Walter Roberson
on 17 Nov 2022
This should be something similar to tetris
So, to confirm, the following should be a legal output
..LLLLLLLLLL.....SSS....
..LLLLLLLLLL.....SSS....
..LLLLLLLLLL.....SSS....
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
..LLLLLLLLLL..LLLLLLLLLL
SSS...........LLLLLLLLLL
SSS...........LLLLLLLLLL
SSS...........LLLLLLLLLL
... Or not because there is room for another S block between the L and the S at the top? Or is the number of blocks of each type to be balanced, in which case you would not try to repeatedly drop 3 x 3 into spaces too small to fit 10 x 10, taking into account that due to the differences in sizes that up to nine of the 3 x 3 blocks could fit into a 10 x 10 space ?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!