I need to plot a number of circles with the same radius. Number of circles and the radius are input parameters. Also circles are randomly positioned. For example: The program ask user to input the number of the circles, then ask to input the radius of the circle. Here, the radius is the same for all circles. Then by using input parameters program randomly put the circles.
MATLAB: How to plot a number of circles with the same radius, but the position of the circles is randomly put
circleMATLABrandom positions
Related Solutions
Try this:
% Create sample data.
numCircles = 10;xy = rand(numCircles, 2);radii = 0.1 * rand(numCircles, 1) + 0.1;viscircles(xy, radii);% Label them so we know what circle is what.
for k = 1 : numCircles text(xy(k, 1), xy(k, 2), num2str(k));endaxis square;% See if any intersect
for k = 1 : numCircles % Compute the distance between centers.
distances = sqrt((xy(k, 1) - xy(:, 1)).^2 + (xy(k, 2) - xy(:, 2)) .^2); sumOfRadii = radii(k) + radii(:); intersectingIndexes = distances < sumOfRadii; % Don't compare circle to itself. Of course that one intersects!
intersectingIndexes(k) = false; % Convert to linear indexes
intersectingIndexes = find(intersectingIndexes); for k2 = 1 : length(intersectingIndexes) fprintf('Circle %d intersects with circle #%d.\n', k, intersectingIndexes(k2)); endend
You see
Circle 1 intersects with circle #2.
Circle 1 intersects with circle #5.
Circle 1 intersects with circle #7.
Circle 2 intersects with circle #1.
Circle 2 intersects with circle #5.
Circle 3 intersects with circle #7.
Circle 3 intersects with circle #9.
Circle 5 intersects with circle #1.
Circle 5 intersects with circle #2.
Circle 6 intersects with circle #10.
Circle 7 intersects with circle #1.
Circle 7 intersects with circle #3.
Circle 8 intersects with circle #9.
Circle 9 intersects with circle #3.
Circle 9 intersects with circle #8.
Circle 10 intersects with circle #6.
and it's trivial to find out if one circle is completely contained within another. See if you can do it.
Here is a little demo you might find useful. It determines which, if any, circles overlap/intersect:
% Demo to create some circles with random radii and center locations and look for overlaps.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;format compact;fontSize = 20;% Create some circles randomly.
% Some might overlap, or possibly none overlap (since it's random).
numCircles = 7;xy = rand(numCircles, 2); % Randomly located centers.
radii = 0.1 * rand(1, numCircles) + 0.05; % Random radii in the range 0.05 to 0.2
% Draw circles.
viscircles(xy, radii);xlabel('X', 'FontSize', fontSize);ylabel('Y', 'FontSize', fontSize);% Label them so we know which circle is which.
% Put the circle number at the center.
for k = 1 : numCircles text(xy(k, 1), xy(k, 2), num2str(k), 'FontWeight', 'bold');endgrid on;axis('equal');% Get a list of separations center-to-center.
distances = pdist2(xy, xy)% Determine the sum of the radii.
[r1, r2] = meshgrid(radii, radii)sumOfRadii = r1 + r2% If there is an overlap, the sum of the radii will be bigger than the distance between them.
overlapping = sumOfRadii > distances;% Zero out the diagonal because we don't care that
% circle 1 overlaps circle 1, circle 2 overlaps circle 2, etc.
overlapping(logical(eye(numCircles))) = false% Make up information message for plot title and command window:
numberOfOverlaps = sum(any(overlapping, 2));overlappedIndexes = find(any(overlapping, 2));if numberOfOverlaps > 0 caption = sprintf('%d circles are overlapped:', numberOfOverlaps); for k = 1 : length(overlappedIndexes) caption = sprintf('%s %d', caption, overlappedIndexes(k)); endelse caption = sprintf('%d circles are overlapped', numberOfOverlaps);endfprintf('%s.\n', caption); % Print to command line.
title(caption, 'FontSize', fontSize); % Title over the plot.
% Report which circles overlap by determining how many rows have at least one 1 in them.
for row = 1 : numCircles % See which other circles overlap with the kth circle.
overlappedCircleIndexes = find(overlapping(row, :)); if ~isempty(overlappedCircleIndexes) % If any overlap with it, print them out.
for k2 = 1 : length(overlappedCircleIndexes) fprintf('Circle %d overlaps with circle %d.\n', ... row, overlappedCircleIndexes(k2)); end endend
For that random set, I see:
distances =
0 0.30561 0.64619 0.32995 0.25392 0.45259 0.70397
0.30561 0 0.54554 0.19313 0.35021 0.29865 0.39959
0.64619 0.54554 0 0.36275 0.41575 0.24748 0.68878
0.32995 0.19313 0.36275 0 0.21992 0.12872 0.49327
0.25392 0.35021 0.41575 0.21992 0 0.2886 0.70892
0.45259 0.29865 0.24748 0.12872 0.2886 0 0.4981
0.70397 0.39959 0.68878 0.49327 0.70892 0.4981 0
sumOfRadii =
0.2034 0.20737 0.16735 0.2079 0.22118 0.19434 0.23533
0.20737 0.21134 0.17132 0.21188 0.22515 0.19832 0.2393
0.16735 0.17132 0.1313 0.17186 0.18513 0.1583 0.19928
0.2079 0.21188 0.17186 0.21241 0.22569 0.19885 0.23983
0.22118 0.22515 0.18513 0.22569 0.23896 0.21213 0.25311
0.19434 0.19832 0.1583 0.19885 0.21213 0.18529 0.22627
0.23533 0.2393 0.19928 0.23983 0.25311 0.22627 0.26725
overlapping =
7×7 logical array
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 1 0 0 1 1 0
0 0 0 1 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
4 Circles overlap.
Circle 2 overlaps with circle 4.
Circle 4 overlaps with circle 2.
Circle 4 overlaps with circle 5.
Circle 4 overlaps with circle 6.
Circle 5 overlaps with circle 4.
Circle 6 overlaps with circle 4.
Best Answer