필터 지우기
필터 지우기

How to create a filled circle?

조회 수: 296 (최근 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