MATLAB Answers

Creating random points in a circle

197 views (last 30 days)
Stephen Johnson
Stephen Johnson on 20 Apr 2013
Commented: Jan on 21 Mar 2018
I am new to MatLab but I am trying to write code for a problem and a couple of things are sticking me up. I am trying to create X number of random points within a circle. The purpose being to model a camp fire and map temperature from the center of the circle at the hottest to the outside. And I am lost. And I can't find an example anywhere I might be able to pull apart and work with. And help would be very much appreciated.
Thanks!

  0 Comments

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 19 May 2013
Let the circle be of radius R and center at (x0,y0). Let n be the desired number of points within.
t = 2*pi*rand(n,1);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
plot(x,y)

  5 Comments

Show 2 older comments
Adrian Blanco
Adrian Blanco on 20 May 2016
Hi,
Thanks for the amazing examples. How would it be to create random points in a square? I am trying to change the math of the example but without success.
Thanks in advance for the advice.
John D'Errico
John D'Errico on 20 May 2016
Uniform random points in a square is easy.
x = rand(n,1);
y = rand(n,1);
Jan
Jan on 21 Mar 2018
rand() replies values from the open interval (0,1), such that t=0 or t=2*pi will never occur. The difference to the correct distribution over [0,1) or (0,1] is tiny, but it is a pity, that Matlab's RNG does not offer half-closed or closed intervals also.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 20 Apr 2013
Here, see my demo:
% M-file to place multiple points inside a big circle.
% Clean up
close all;
clc;
fontSize = 15;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Initialize some parameters.
numberOfPoints = 25; % Number of small circles
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
bigCircleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
clear('x', 'y'); % Release these variables, they're not needed anymore.
% Display it in the upper left plot.
subplot(2,2, 1);
imshow(bigCircleImage, []);
title('Big Circle Mask', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Initialize an output image to hold many small overlapping circles.
pointsImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
% Get linear indexes of 500 randomly located in the rectangle.
numberOfPointsToPlace = 5000;
linearIndexes = randi(numel(pointsImage), numberOfPointsToPlace, 1);
% Set those points in the image
pointsImage(linearIndexes) = 255;
% Get locations in terms of row and columns:
[rows, columns] = ind2sub(size(pointsImage), linearIndexes);
% Display it in the lower left plot.
subplot(2,2, 2);
imshow(pointsImage);
title('Many Points', 'FontSize', fontSize);
% Multiply the big circle mask by the points image to clip
% those points that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* pointsImage;
% Display it in the lower right plot.
subplot(2,2, 3);
imshow(maskedByBigCircle);
title('Many Points Masked by Big Circle', 'FontSize', fontSize);

  1 Comment

Image Analyst
Image Analyst on 19 May 2013
Stephen, what's the status of this? Are you still stuck?

Sign in to comment.


Irfan
Irfan on 31 Jul 2013
Hi, Roger, Why did you use sqrt in r = R*sqrt(rand(n,1)); it should be without sqrt, is'nt ?

  1 Comment

Roger Stafford
Roger Stafford on 31 Jul 2013
That is untrue! Without the square root operation, the distribution of points within the circle will not be uniformly distributed throughout its interior. Just try it out with a large value of n, say, n = 1000 and see.
Think of it this way, Irfan. The area of a circle with the same center and with half the given radius, R, is one-fourth that of the larger circle. Therefore the probability that we get an r value less than or equal to R/2 should be one-fourth. That is, a 'rand' value of 1/4 should give rise to a value r = R/2. This is accomplished by taking the square root of the 'rand' value. A similar reasoning applies to other fractional circles.

Sign in to comment.

Sign in to answer this question.