Plotting Random Points within Boundary

8 Ansichten (letzte 30 Tage)
Ethan Murray
Ethan Murray am 10 Mär. 2017
Beantwortet: Ikram Ashraf am 14 Feb. 2019
I'm trying to set up a simulation of sorts on MatLab that creates a 'map' of the terrain.
The terrain is to be bounded by a 2 meter radius circle with objects (cubes) within the boundary. I can plot this with the code:
% Define the Boundary Area
% Equation of a circle given by: x^2 + y^2 = r^2
r = 2; % Radius of 2metres
k = 0; % Centre of circle is at (0,0)
for theta = 0:pi/300:2*pi % Angle increment of 0.01
k = k+1;
xr(k) = r*cos(theta);
yr(k) = r*sin(theta);
end
figure;
plot(xr,yr); hold on % Plots a circle of radius 'r'
% Create a random set of coordinates in a circle.
% First define parameters that define the number of points and the circle.
n = 20;
R = 2;
x0 = 0; % Center of the circle in the x direction.
y0 = 0; % Center of the circle in the y direction.
% Now create the set of points.
t = 2*pi*rand(n,1);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
% Now display our random set of points in a figure.
plot(x,y, 's', 'MarkerSize', 10); hold off
axis square;
grid on;
However this code doesn't allow for cubes (I suppose on this it will be squares) to be plotted, only points with a certain marker size. Can I get it so that squares can be plotted within the boundary?
I was thinking a point would be created then it would go 0.05 to one side, then 0.05 up, then 0.05 along, then 0.05 back to the start point of each square. And therefore it would be ideal if the squares wouldn't overlap - but stepping stones I guess, that's something to work on.
Thanks in advance.

Antworten (3)

Image Analyst
Image Analyst am 10 Mär. 2017
"Can I get it so that squares can be plotted within the boundary?" Yes you can.
To prevent overlap, simple keep track of the x and y range for each box and if any new box invades that range, don't use it.
  1 Kommentar
Ethan Murray
Ethan Murray am 10 Mär. 2017
Without trying to sound rude and-or lazy, how would I go about coding that in MatLab?
A) How do I plot these seemingly random squares? B) How do I prevent them from intruding on each other's space?

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 11 Mär. 2017
Ethan, try this:
% M-file to place multiple small squares inside a big circle where no squares overlap, though they might touch.
% Clean up
close all;
clc;
fontSize = 15;
% Initialize some parameters.
numberOfsquares = 75; % Number of small squares
squareInsideValue = 0.8;
circleOutsideValue = 0.6;
circleInsideValue = 0.2;
squareWidth = 25;
imageWidth = 500;
imageHeight = 500; % square area 0f 500*500
circleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
ourImage = circleOutsideValue * ones(imageHeight, imageWidth);
[x, y] = meshgrid(1:imageWidth, 1:imageHeight);
ourImage((x - imageWidth/2).^2 + (y - imageHeight/2).^2 <= circleRadius.^2) = circleInsideValue;
% Display it in the upper left plot.
subplot(2, 1, 1);
imshow(ourImage, []);
title('Circle Mask', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Place the small squares inside one by one.
for k = 1 : numberOfsquares
% Get a random row, column location
y1 = randi(imageHeight, 1);
x1 = randi(imageWidth, 1);
y2 = y1 + squareWidth - 1;
x2 = x1 + squareWidth - 1;
% See if any of those corners are outside the circle. Skip this square if so.
if sqrt((x1 - imageWidth/2)^2 + (y1 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x1 - imageWidth/2)^2 + (y2 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x2 - imageWidth/2)^2 + (y1 - imageHeight/2)^2) > circleRadius
continue;
end
if sqrt((x2 - imageWidth/2)^2 + (y2 - imageHeight/2)^2) > circleRadius
continue;
end
% If you get to here it's inside the circle.
% Now make sure no corner is inside any square that has already been placed.
if ourImage(y1, x1) == squareInsideValue
continue;
end
if ourImage(y1, x2) == squareInsideValue
continue;
end
if ourImage(y2, x1) == squareInsideValue
continue;
end
if ourImage(y2, x2) == squareInsideValue
continue;
end
% If we get here, we're free to place the square.
ourImage(y1:y2, x1:x2) = squareInsideValue;
end
% Display it in the lower left plot.
subplot(2, 1, 2);
imshow(ourImage);
caption = sprintf('Now with %d Squares Added', numberOfsquares);
title(caption, 'FontSize', fontSize);
% Note: squares might not be overlapping but they might be touching.

Ikram Ashraf
Ikram Ashraf am 14 Feb. 2019
Nice solution for plotting square inside the circle. I have similar kind of situation. I am trying to plot the square inside bigger square but with the condition that the boxes will not overlap. I tried to test the above example by setting up the:
numberOfsquares = 10000; % Number of small squares
Seems that they are overlapping. Any support will be helpful.
Thank in advance.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by