How do I plot many circles using a for-loop on a function?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello!
I want to plot 5 circles on one graph with the radius 1 to 5. The conditions are that I need to use a loop of some kind and that it will call the function for the circle 5 times.
I've written a function for plotting a circle:
function [x,y1,y2]=rita_cirkel(r)
x=[-1*r:pi/100:r];
y1=sqrt(r^2-x.^2);
y2=-1*sqrt(r^2-x.^2);
plot(x,y1,x,y2);
end
I tried using a for-loop to plot 5 circles like the following:
for r=1:1:5
rita_cirkel(r)
end
And it didn't work :/ It only game me a graph of a circle with radius=5
I am curious of how do I plot 5 circles in one single graph (like a dart board) while still meeting the conditions of calling a function 5 times and using some kind of loop.
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 18 Dez. 2012
You can easily adapt this demo for your (possible) homework problem. It plots circles in random locations:
% M-file to place multiple small circles inside a big circle.
% Clean up
close all;
clc;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
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);
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
% Display it in the upper left plot.
subplot(3,2,1);
imshow(bigCircleImage, []);
title('Big Circle Mask');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Initialize an image to hold one single small circle.
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y - smallCircleRadius).^2 <= smallCircleRadius.^2) = smallCircleInsideValue;
% Display it in the upper right plot.
subplot(3,2,2);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)');
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(3,2,3);
imshow(manySmallCircles);
title('Many Small Overlapping Circles');
% Multiply the big circle mask by the many small circles image to clip
% those small circles that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* manySmallCircles;
% Display it in the lower right plot.
subplot(3,2,4);
imshow(maskedByBigCircle);
title('Many Small Circles Masked by Big Circle');
% Take the histogram and display it in the bottom row.
subplot(3,2,5);
hist(maskedByBigCircle(:));
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!