paint the circle in half

I need to draw a circle and color the right side of it. I found how to draw a circle, but I don't know how to color half of the circle.
pos = [2 120 2 2];
rectangle('Position',pos,'Curvature',[1 1],'FaceColor',[0 .5 .5])

 Accepted Answer

Voss
Voss on 27 Mar 2024
Edited: Voss on 27 Mar 2024
pos = [2 120 2 2];
color = [0 0.5 0.5];
r = pos(3)/2;
c = pos([1 2])+r;
th = linspace(-pi/2,3*pi/2,101); % <- use an odd number of points in linspace()
x = c(1)+r*cos(th);
y = c(2)+r*sin(th);
fill(x(1:(end+1)/2),y(1:(end+1)/2),color,'EdgeColor','none')
hold on
plot(x,y,'k')
axis equal

6 Comments

Wow, thank you so much
You're welcome!
Can you do something like this?
Define the circles' coordinates:
r1 = 2/3;
r2 = 1;
c = [0 0];
th = linspace(-pi/2,3*pi/2,101); % <- use an odd number of points in linspace()
x1 = c(1)+r1*cos(th);
y1 = c(2)+r1*sin(th);
x2 = c(1)+r2*cos(th);
y2 = c(2)+r2*sin(th);
Here's one way, where the white parts actually show the underlying axes (made red for illustration):
figure
set(gca(),'Color','r')
hold on
n = numel(th);
n2 = (n+1)/2;
fill([x1(n2:n) x2(n:-1:n2)],[y1(n2:n) y2(n:-1:n2)],'k')
fill(x1(1:n2),y1(1:n2),'k')
plot(x1,y1,'k','LineWidth',2)
plot(x2,y2,'k','LineWidth',2)
axis equal
Here's another way, where the white parts are actually white:
figure()
set(gca(),'Color','r')
xd1 = [x1(n2:n) x2(n:-1:n2); x1(1:n2) x2(n2:-1:1)].';
yd1 = [y1(n2:n) y2(n:-1:n2); y1(1:n2) y2(n2:-1:1)].';
xd2 = [x1(1:n2); x1(n2:n)].';
yd2 = [y1(1:n2); y1(n2:n)].';
patch('XData',xd1,'YData',yd1,'FaceColor','flat','FaceVertexCData',[0 0 0; 1 1 1],'LineWidth',2)
patch('XData',xd2,'YData',yd2,'FaceColor','flat','FaceVertexCData',[0 0 0; 1 1 1],'LineWidth',2)
axis equal
voss you're strong! thx
You're welcome!

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Asked:

on 27 Mar 2024

Commented:

on 27 Mar 2024

Community Treasure Hunt

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

Start Hunting!