MATLAB Answers

Creating random points in a circle

392 views (last 30 days)
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.

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);
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;
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);
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);
title('Many Points Masked by Big Circle', 'FontSize', fontSize);
Image Analyst
Image Analyst on 5 Aug 2021
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
r = 2;
fprintf('Done running %s.m\n', mfilename);
function [missrate, radiusList] = Final_radiationring(r)
missrate = 0;
% Make 360 points around the circle.
numCirclePoints = 360;
theta = linspace(0, 2 * pi, numCirclePoints);
% Get radius, ignoring the one passed in.
q='Enter radius: ';
radiusList = [r, r + r/20, r - r/20];
xcord = xcenter + r * cos(theta);
ycord = ycenter + r * sin(theta);
plot(xcord, ycord, 'b-', 'LineWidth', 2)
hold on
xchigh=xcenter + radiusList(2)*cos(theta);
xclow=xcenter + radiusList(3)*cos(theta);
ychigh=ycenter + radiusList(2)*sin(theta);
yclow=ycenter + radiusList(3)*sin(theta);
plot(xchigh,ychigh, 'm-', 'LineWidth', 2)
plot(xclow,yclow, 'g-', 'LineWidth', 2)
grid on;
numRandomPoints = 300;
x0 = 0; % Center of the circle in the x direction.
y0 = 0; % Center of the circle in the y direction.
% Create a noisy set of points
rng default
t = 2 * pi * rand(numRandomPoints,1);
g = 0.9 * r + 0.2 * r * rand(numRandomPoints,1); %generate random points in boundary
x = x0 + g.*cos(t);
y = y0 + g.*sin(t);
% Now display our random set of points in a figure with circle
radialDistances = sqrt(x.^2 + y.^2);
colors = ['r', 'g', 'b', 'm']
radiusList = [0, sort(radiusList, 'ascend'), inf];
for k = 1 : length(radiusList) - 1
r1 = radiusList(k);
r2 = radiusList(k+1);
logicalIndexes = radialDistances >= r1 & radialDistances < r2;
thisx = x(logicalIndexes);
thisy = y(logicalIndexes);
thisColor = colors(k);
plot(thisx, thisy, '.', 'Color', thisColor, 'MarkerSize', 20)
hold off
axis equal
grid on;

Sign in to comment.

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.

Community Treasure Hunt

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

Start Hunting!