How to create a filled circle?

276 Ansichten (letzte 30 Tage)
Thomas Nell
Thomas Nell am 28 Dez. 2018
Kommentiert: Rik am 2 Okt. 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!

Antworten (2)

Star Strider
Star Strider am 28 Dez. 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 am 2 Okt. 2021
Bearbeitet: Nakarin Ratsuntia am 2 Okt. 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 Kommentar
Rik
Rik am 2 Okt. 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)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by