Surface plot above a non-rectangular region

23 Ansichten (letzte 30 Tage)
Mehdi Y
Mehdi Y am 14 Nov. 2019
Bearbeitet: Mehdi Y am 15 Nov. 2019
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.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Computational Geometry finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by