Surface plot above a non-rectangular region

23 views (last 30 days)
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.

Accepted Answer

Praveen Iyyappan Valsala
Praveen Iyyappan Valsala on 14 Nov 2019
Edited: Praveen Iyyappan Valsala on 14 Nov 2019
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 Comments
Praveen Iyyappan Valsala
Praveen Iyyappan Valsala on 14 Nov 2019
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 on 15 Nov 2019
Edited: Mehdi Y on 15 Nov 2019
I understand it now. :) Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!