MATLAB Answers


Surface bounded by a polygon

Asked by Sarah Andrade on 26 Sep 2017
Latest activity Commented on by Sarah Andrade on 27 Sep 2017

I have a polygon and a surface that covers only part of this polygon (this is in 2-D x and y axis). Is there any way to delimit the surface by the polygon? I want only the part that is inside the polygon.



Log in to comment.

1 Answer

Answer by Chad Greene
on 26 Sep 2017
 Accepted Answer

Without any sample data or illustrations it's mighty difficult to know exactly what you mean, but it sounds like inpolygon is the function you're looking for. Just feed it the x,y locations of your surface (generated by meshgrid) and it will tell you which corresponding surface locations are inside the polygon.


Hi Chad! This is an example of what I'm trying to say (my code is too big):

lat = [-22.738889 
long = [-45.590833
variavel = [5793.4
rangeZ = floor(min(long)):.01:ceil(max(long));
rangeX = floor(min(lat)):0.01:ceil(max(lat));
[X,Z] = meshgrid(rangeX,rangeZ);
Y = griddata(lat,long,variavel,X,Z,'cubic');
lat2 = [-23.4
long2 = [-47
axis tight
shading interp
hold on

I want to cut the surface inside the triangle and stay with it only. Using 'inpolygon' is there any way to erase the surface outside the polygon? I didn't find it before.

Thank you!

Hi Sarah,

Thanks for providing a minimal working example. That helps tremendously. Before we go any further, I want to make a fundamental change to your code. For this kind of stuff we often think of longitudes as being like x values and latitudes as similar to y values. However, the use of variable names X and Y can imply that the spatial dimensions have been projected into meters, so it gets confusing to mix lat, long, X, and Y all on the same map with the same units. If I were faced with your problem I'd start by plotting the raw scattered data in unprojected coordinates, but letting long and lat be the horizontal and vertical axes of a plot, respectively (note, this is a change from your method):

hold on
xlabel 'longitude'
ylabel 'latitude'

I've made a habit of typically using lowercase variable names (lat, long, x, y) for scattered data, and uppwercase (LAT, LONG, X, Y) for gridded data. That's a bit more intuitive for me than letting gridded longitudes become Z and latitudes X. So I'd rewrite your gridding section like this:

% No need to change variable names, just change their cases: 
[LONG,LAT] = meshgrid(floor(min(long)):.01:ceil(max(long)),...
VARIAVEL = griddata(long,lat,variavel,LONG,LAT,'cubic');
s = surface(LONG,LAT,VARIAVEL); 
shading interp 

Now use inpolygon to find which grid cells are inside the triangle:

% These points are inside the polygon: 
in = inpolygon(LONG,LAT,long2,lat2); 
% Set points not inside (~in) the polygon to NaN: 
VARIAVEL(~in) = NaN; 
% delete the surface we just plotted: 
% Plot the new surface: 
s = surface(LONG,LAT,VARIAVEL); 
shading interp 

Thank you so much, Chad!!

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today