How to color the space between ellipse?
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to shade the following region :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1169908/image.png)
I know how to plot ellipses using the below code .
But I want to shade the region between two ellipses and I only need x>=0 and y>=0.
In general, if
how do I shade the region when
? Thank you.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1169913/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1169938/image.png)
ezplot('x^2 + y^2/4 - 1');hold on; ezplot('x^2 + y^2/4 - 4'); hold off
댓글 수: 0
답변 (3개)
Torsten
2022년 10월 26일
편집: Torsten
2022년 10월 26일
v = -6:0.001:6; % plotting range from -5 to 5
[x y] = meshgrid(v); % get 2-D mesh for x and y
cond1 = x.^2 + y.^2/4 >= 1; % check conditions for these values
cond2 = x.^2 + y.^2/4 <= 4;
cond3 = x >= 0;
cond4 = y >= 0;
cond1 = double(cond1); % convert to double for plotting
cond2 = double(cond2);
cond3 = double(cond3);
cond4 = double(cond4);
cond1(cond1 == 0) = NaN; % set the 0s to NaN so they are not plotted
cond2(cond2 == 0) = NaN;
cond3(cond3 == 0) = NaN;
cond4(cond4 == 0) = NaN;
cond = cond1.*cond2.*cond3.*cond4; % multiply the conditions to keep only the common points
surf(x,y,cond)
view(0,90) % change to top view
댓글 수: 0
Vasudev Sridhar
2022년 10월 26일
Alternative solution in case you are dealing with radial coordinates.
x1 = linspace(0,1,100); % Generate the first set of x coordinates. Change the first two parameters to get different ranges
y1 = 2*sqrt(1-x1.^2);
x2 = linspace(0,2,100); % Generate the second set of x coordinates. Change the first two parameters to get different ranges
y2 = 2*sqrt(4-x2.^2);
x = [x2, fliplr(x1)]; % Get the set of x coordinates for the resultant polygon. The order of the x-coordinates for the second shape shape need to be reversed for this
yeff = [y2, fliplr(y1)]; % Get the set of y coordinates for the resultant polygon. The order of the y-coordinates for the second shape shape need to be reversed for this
fill(x, yeff, 'g'); % Fill the polygon with color green(g)
댓글 수: 0
Matt J
2022년 10월 26일
편집: Matt J
2022년 10월 26일
fn=@(r) scale( nsidedpoly(1000,'Radius',r) ,[1,2] );
region=subtract(fn(2),fn(1));
plot(region); axis equal; axis([0,4, 0,4])
댓글 수: 1
Matt J
2022년 10월 26일
Or, if you really need the region truncated to the positive quadrant,
fn=@(r) scale( nsidedpoly(1000,'Radius',r) ,[1,2] );
region=subtract(fn(2),fn(1));
region=intersect(region, polyshape([0,0;2,0; 2 4; 0 4]));
plot(region); axis equal;
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!