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])

 채택된 답변

Voss
Voss 2024년 3월 27일
편집: Voss 2024년 3월 27일

0 개 추천

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

gravy
gravy 2024년 3월 27일
Wow, thank you so much
Voss
Voss 2024년 3월 27일
You're welcome!
gravy
gravy 2024년 3월 27일
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
gravy
gravy 2024년 3월 27일
voss you're strong! thx
Voss
Voss 2024년 3월 27일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 3월 27일

댓글:

2024년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by