How to create a filled circle?

290 次查看(过去 30 天)
Thomas Nell
Thomas Nell 2018-12-28
评论: Rik 2021-10-2
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!

回答(2 个)

Star Strider
Star Strider 2018-12-28
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 2021-10-2
编辑:Nakarin Ratsuntia 2021-10-2
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 个评论
Rik
Rik 2021-10-2
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by