Surface plot above a non-rectangular region

조회 수: 23 (최근 30일)
Mehdi Y
Mehdi Y 2019년 11월 14일
편집: Mehdi Y 2019년 11월 15일
I'd like to draw the graph of a function above a polygonal region, which is not rectangular. Any idea how to do this? I've seen that there is a file called polygrid
https://uk.mathworks.com/matlabcentral/fileexchange/41454-grid-of-points-within-a-polygon
that extract a list of points from a polygonal region (specified by its list of vertices in the clockwise order). I intended to use the output from polygrid as my mesh for the surf function, but I haven't been able to use them compatibly. Any help will be appreciated.

채택된 답변

Praveen Iyyappan Valsala
Praveen Iyyappan Valsala 2019년 11월 14일
편집: Praveen Iyyappan Valsala 2019년 11월 14일
You can simply create a binary mask of the polygon region and set all Z values to NaN outside the mask. NaN values are not plotted by surface.
%draw poly MASK/get it from your function
imagesc(Z)
Masksize=size(Z);
[x,y]=meshgrid(1:Masksize(2),1:Masksize(1));
poly=drawpolygon;
[in,on] =inpolygon(x(:),y(:),poly.Position(:,1),poly.Position(:,2));
mask=reshape(in|on,Masksize); % all points inside or on the polygon
%set all points outside mask NaN
Z(~mask)=NaN;
clf,surface(X,Y,Z)
  댓글 수: 3
Praveen Iyyappan Valsala
Praveen Iyyappan Valsala 2019년 11월 14일
The above code doesn't need polygrid function, suppose you have a surface Z with x and y grid as shown below.
[x,y] = meshgrid(1:0.1:10,1::0.2:20);
Z = sin(x) + cos(y);
surface(x,y,Z)
then simply append the code above, draw your polygon on the figure and get the surface plotted within the polygon you drew.
if you have vertices of the polygon(xv and yv) already and ppa
% create the mesh like your polygrid function
[x, y] = meshgrid(min(xv):1/sqrt(ppa):max(xv),min(yv):1/sqrt(ppa):max(yv));
%create binary mask with polygon vertices(xv,yv)
Masksize=size(x);
in =inpolygon(x(:),y(:),xv(:),yv(:));
mask=reshape(in,Masksize); % all points inside or on the polygon
%calculate you surface here with x and y
yoursurface=yourfunction(x,y);
%set all points outside mask NaN
yoursurface(~mask)=NaN;
figure,surface(x,y,yoursurface)
Hope it is clear now.
Mehdi Y
Mehdi Y 2019년 11월 15일
편집: Mehdi Y 2019년 11월 15일
I understand it now. :) Thanks.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Computational Geometry에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by