Random squares/rectangles in a square
8 views (last 30 days)
Show older comments
Tony Haines
on 24 May 2024
Commented: William Rose
on 28 May 2024
I have a square of size 1, that is (0,1)x(0,1). In this square, I would like to create random squares or rectangles of various sizes where none of them share an edge with each other. Meaning, I want them randomly spaced out. Then, i would like to save the values or coordinates within each squares to compare them with an input (x,y) such that (x,y) has value 1 if it's in the square, and zero if it's not.
I want to repeat this(random sampling of squares) for 5 different samples.
Thanks!
0 Comments
Accepted Answer
William Rose
on 25 May 2024
So you want option B. OK.
Ns=5; % number of squares
P=zeros(4,2,Ns); % vertex coords
s=zeros(Ns,1); % side lengths
anyOverlaps=1; % =0 if no squares overlap
inSquare=zeros(Ns,1); % =1 when point is in square i
j=0; % initialize counter
while anyOverlaps>0
for i=1:Ns
LL=rand(1,2); % lower left coords of square: [x1,y1]
s(i)=(1-max(LL))*rand(1,1); % side length
P(:,:,i)=[LL; LL+[s(i),0]; LL+[s(i),s(i)]; LL+[0,s(i)]]; % vertex coords
pgon(i)=polyshape(P(:,:,i)); % create square polygon
end
tf=overlaps(pgon);
anyOverlaps=sum(tf,'All')-Ns;
j=j+1;
end
fprintf('After %d attempts, we found %d non-overlapping squares.\n',j,Ns)
fprintf('Minimum, maximum side lengths = %.3f, %.3f.\n',min(s),max(s))
fprintf('Total area covered by squares=%.3f.\n',sum(s.^2))
Now we have 5 "random" non-overlapping squares.
Pick a point (x,y) and see if it is in any of the squares.
x=rand(1,1); y=rand(1,1);
for i=1:Ns
inSquare(i)=inpolygon(x,y,P(:,1,i),P(:,2,i));
end
fprintf('Point (x,y) is in %d square(s).\n',sum(inSquare,'all'))
Plot the point and the squares.
figure
for i=1:Ns
plot(pgon(i)); hold on;
end
plot(x,y,'r*')
axis equal; xlim([0,1]); ylim([0,1])
I had to run the script above at least 10 times before I got a case where the point was in one of the squares.
5 Comments
More Answers (1)
William Rose
on 24 May 2024
Do you want
A. One random square inside the unit square, and you want choose that random square 5 times, and each time you want to see if a random point (x,y) falls within the random square?
or
B. Five "random" non-overlapping little squares inside the unit square. If this is the case, then the 5 little squares are not really random, since the location and size of each is constrained by the square(s) already chosen.
or something else? I assume you want A. I will do A, with six examples, instead of five. I will use one random point and compare it to six different squares.
x=rand(1,1); y=rand(1,1); % point of reference
pointInSquare=zeros(1,6); % initialize array for whether point is in the square
figure
for i=0:1
for j=0:2
BL=rand(1,2); %x,y coords of bottom left corner of random square inside the unit square
s=(1-max(BL))*rand(1,1); % side length
P=[BL; BL+[0,s]; BL+[s,s]; BL+[s,0]]; % array of vertices of square
pgon=polyshape(P); % create square polygon
pointInSquare(3*i+j+1)=inpolygon(x,y,P(:,1),P(:,2)); % =1 if x,y in or on square
subplot(2,3,3*i+j+1);
plot(pgon); hold on; plot(x,y,'r*'); axis equal; xlim([0,1]); ylim([0,1])
end
end
fprintf('(x,y) in square for %d square(s).\n',sum(pointInSquare));
Try it.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!

