Surface plot above a non-rectangular region

47 ビュー (過去 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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by