Generating random circles/ellipses inside circular domain

7 views (last 30 days)
I am using this code for microstructure generation. But the issue is that this code generates circle/ellipses inside a square domain.
In my case the domain is circular in shape. I tried many times but I am not able to accurately change that square domain into a circular domain.
I have attached a sample microstructure.

Answers (3)

Torsten on 12 Dec 2022
Edited: Torsten on 12 Dec 2022
Maybe you could generate the circles/ellipses in a square domain with side length R (the radius of your circular shape) and finally cut away those that don't belong to your shape.
Image Analyst
Image Analyst on 13 Dec 2022
@Bruno Luong to be fair, he did not ask us to make changes in the code, though maybe that was the implication or you inferred that. He said it would be great if he, himself, could make the changes (not us) so that's why I referred him to the training web site in my comment.
@Shubham, Bruno is right. If you don't want to learn MATLAB, then you should ask the original author. In general we don't, in this forum, help you modify or debug File Exchange submissions. But the best solution is you become skilled enough yourself so that you can do things yourself.
Torsten on 13 Dec 2022
It is possible but this will affect the area ratio.
Can you elaborate ?

Sign in to comment.

Askic V
Askic V on 12 Dec 2022
Since you provided an example in which all small circles have the same radius, perhaps the following code will suit your needs:
close all
% Main circle
R = 5; % Radius
Xc = 2; % x center coordinate
Yc = 2; % y center coordinate
t = linspace(0,2*pi);
tx = cos(t);
ty = sin(t);
x = Xc + R*tx;
y = Yc + R*ty;
% Plot main circle
plot(x,y, 'LineWidth',2);
axis equal;
grid on;
hold on;
% Random small circles range
rangex = [Xc-R; Xc+R];
rangey = [Yc-R, Yc+R];
% Number of circles
N = 100;
r = 0.2*ones(N,1);
xc = (rangex(2)-rangex(1))*rand(N,1)+rangex(1);
yc = (rangey(2)-rangey(1))*rand(N,1)+rangey(1);
points = [xc, yc, r];
Center = [Xc,Yc];
is_in_circle = sum((Center-points(:,1:2)).^2,2) < R^2;
% Add vector is_incircle to existing points in order to filter it
new_points = [points is_in_circle];
% Remove points that are outside the circle
new_points(new_points(:,4)==0, :) = [];
% %
% Check if circles are intersecting
for ii = 1:size(new_points,1)
xout = circcirc(Xc,Yc,R,new_points(ii,1),new_points(ii,2),...
if ~isnan(xout)
new_points(ii,4) = 0;
% Remove circles that are outside the main circle
new_points(new_points(:,4)==0, :) = [];
% Now check and remove overlapping
% Remove overlapping circles
for ii = 1: size(new_points,1)
for jj = 1:size(new_points,1)
if ii ~= jj
xout = circcirc(new_points(ii,1),new_points(ii,2),new_points(ii,3),...
if ~isnan(xout)
new_points(ii,4) = 0;
% Remove from the matrix
new_points(new_points(:,4)==0, :) = [];
% Now plot small circles
for ii = 1: size(new_points,1)
x = new_points(ii,3)*tx+new_points(ii,1);
y = new_points(ii,3)*ty+new_points(ii,2);
Shubham on 13 Dec 2022
Thanks for the help. But this is not what I am looking for. If I could make changes in the same code that would be great.
Image Analyst
Image Analyst on 13 Dec 2022
That would be great wouldn't it? Here's how you can do it: invest 2 hours of your time here:

Sign in to comment.

Image Analyst
Image Analyst on 12 Dec 2022
% 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 up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% 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);


Find more on Graphics Object Programming 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!