how to plot a circle if the coordinates and the radius are defined in random fashion

2 views (last 30 days)
for k=1:2
x=rand(1,1)*10;
y=rand(1,1)*20;
r=rand(1,1)*10;
end
I have defined the coordinates(Center)and the radius in the given manner, how can i plot a circle using the above code as the radius is defined in the for loop can it be used elsewhere in the code
please help me sort this out and thank you in advance

Answers (2)

Walter Roberson
Walter Roberson on 13 Jun 2015
numcirc = 2;
numsides = 36;
xcrange = [0, 10]; %x center between 0 and 10
ycrange = [5, 20]; %y center between 5 and 20
ang = linspace(0,2*pi, numsides+1);
[xn, yn] = pol2cart(ang(:),1); %create unit circle
r = rand(1, numcirc)*10; %row vector!
xs = xn(:) * r; %scale. Note matrix multiply
ys = yn(:) * r; %scale. Note matrix multiply
xc = rand(1, numcirc) * (xcrange(2)-xcrange(1)) + xcrange(1); %row vector!
yc = rand(1, numcirc) * (ycrange(2)-ycrange(1)) + ycrange(1); %row vector!
x = xs + repmat(xc, length(xn), 1); %translate
y = ys + repmat(yc, length(yn), 1); %translate
plot(x, y);
axis equal
The result of this is two arrays, (numsides+1) x numcirc. One column per circle, and the rows are individual coordinates of points approximating the outside of the circle.

Image Analyst
Image Analyst on 13 Jun 2015
I'm going to assume you know how to program, and use plot() and other simple functions and I'm going to answer your question "how can i plot a circle using the above code as the radius is defined in the for loop can it be used elsewhere in the code ". Yes, it can be used elsewhere. So you have some other function that needs to use the radius. One option is to just put that code you gave into its own function, and then return the radius values in the output argument list. Then just call that functions "elsewhere in the code" whenever you need it. It regenerates it, but it's fast so it's no big deal to do it again.
If you don't want to recalculate it again and want to use the existing arrays like they're global variables, then you can see the FAQ for several ways to use variables in other functions that normally wouldn't be able to see those arrays.

Categories

Find more on 2-D and 3-D Plots 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!