フィルターのクリア

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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by