フィルターのクリア

How to color the space between ellipse?

3 ビュー (過去 30 日間)
Vivek  Bharti
Vivek Bharti 2022 年 10 月 26 日
コメント済み: Matt J 2022 年 10 月 26 日
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 Center および File ExchangeElementary Polygons についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by