필터 지우기
필터 지우기

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