Effacer les filtres
Effacer les filtres

How to create a filled circle?

177 vues (au cours des 30 derniers jours)
Thomas Nell
Thomas Nell le 28 Déc 2018
Commenté : Rik le 2 Oct 2021
Hey guys,
I have this function here for drawing circles:
function circles = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
x_circle = r * cos(th) + x;
y_circle = r * sin(th) + y;
circles = plot(x_circle, y_circle);
hold off
this, however, only draws line circles. What I want to do is draw a filled circle. How do I go about doing this?
Thanks so much!

Réponses (2)

Star Strider
Star Strider le 28 Déc 2018
One option is to use the fill (link) function:
function circles = circle(x,y,r,c)
hold on
th = 0:pi/50:2*pi;
x_circle = r * cos(th) + x;
y_circle = r * sin(th) + y;
circles = plot(x_circle, y_circle);
fill(x_circle, y_circle, c)
hold off
axis equal
end
circleout = circle(3, 4, 2, 'g') % Call ‘circle’ To Create Green Circle
I added a color argument ‘c’ at the end of your current argument list.

Nakarin Ratsuntia
Nakarin Ratsuntia le 2 Oct 2021
Modifié(e) : Nakarin Ratsuntia le 2 Oct 2021
This's some simple way to create filled circle with circle equation.
paper = zeros(360,360); %create empty array
[y x] = size(paper); %define y,x as size of array
r = 40; %define radius of a circle
for i=1:y
for j=1:x
if ((i-y/2)^2)+((j-x/2)^2)<(r^2); %define origin is at the center
paper(i,j) = 1; %define array inside the circle eq. = 1
end
end
end
imshow(paper); %show image
  1 commentaire
Rik
Rik le 2 Oct 2021
Why the double loop? You can also do this with an array operation:
%define radius and center coordinates
r=20;x_c=0;y_c=0;
%generate a coordinate grid
[y,x]=ndgrid(-20:20,-50:50);
%perform calculation
paper= (x-x_c).^2+(y-y_c).^2 <= r^2;
%show result
imshow(paper)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by