Surface plot above a non-rectangular region

21 次查看(过去 30 天)
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
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
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