How to color the space between ellipse?

3 次查看(过去 30 天)
I want to shade the following region :
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.
ezplot('x^2 + y^2/4 - 1');hold on; ezplot('x^2 + y^2/4 - 4'); hold off

回答(3 个)

Torsten
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

Vasudev Sridhar
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)

Matt J
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
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 CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by