MATLAB Answers

Points inside multiple polygon for contour

25 views (last 30 days)
Tessa Kol
Tessa Kol on 29 Sep 2020
Commented: Tessa Kol on 29 Sep 2020
I made the following contourplot.
Then, I plot discretized points over this plot.
I wanted to find the (x,y) coordinates of the discretized points within the region between the boundaries 40.94 and 43. This is the way how I approached it. With contourtable I acquired the (x,y) coordinates of the contourlines.
muSP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9];
muRP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9];
[X,Y] = meshgrid(muSP,muRP);
Z = cell2mat(reshape(AoR_mean,[9,9]));
[C,h] = contour(X,Y,Z,(40.9431:2.0538:42.9969));
hold on
title('Angle of Repose - Ledge Test 2D');
h.LevelList = round(h.LevelList,2);
grid on
% Extract x- and y-coordinates of contour lines
contourTable = getContourLineCoordinates(h);
% Define grid for the variable space
N1 = 80;
muRP_grid = linspace(0.1,0.9,N1);
muSP_grid = linspace(0.1,0.9,N1);
[R,S] = meshgrid(muRP_grid,muSP_grid);
hold off
% x- and y-coordinates of polygon vertices, i.e. between AoR of 40.11 degrees
% and AoR of 43.83 degrees
xv = table2array(contourTable(1:31,3));
yv = table2array(contourTable(1:31,4));
inside_variablespace = inpolygon(R,S,xv,yv);
% x- and y-coordinates corresponding to the discretized points in the
% variable spac
xcoord = R(inside_variablespace);
ycoord = S(inside_variablespace);
However, for some reason I don't get de discretized points within the boundaries 40.94 and 43.


Sign in to comment.

Accepted Answer

KSSV on 29 Sep 2020
Read about inpolygon. If you have a polygon and a set of points, you cann extract the points lying inside this polygon using that.


Show 5 older comments
Tessa Kol
Tessa Kol on 29 Sep 2020
But how does that work if I have the boundary 40.94 and 43, respectively?
Adam Danz
Adam Danz on 29 Sep 2020
Check out the examples in the documentation
You can call inpolygon on the outer polygon, then call inpolygon on the inner polygon, then remove selected points that are in both.
A = inpolygon(...)
B = inpolygon(...)
A(B) = false;
Use these answers as a stepping stone rather than looking for full solutions.
Tessa Kol
Tessa Kol on 29 Sep 2020
It worked thank you! I adjusted the code as follow:
xv1 = [0.9;table2array(contourTable(1:15,3));0.9];
yv1 = [0.9;table2array(contourTable(1:15,4));0.9];
xv2 = table2array(contourTable(16:18,3));
yv2 = table2array(contourTable(16:18,4));
xv3 = table2array(contourTable(19:31,3));
yv3 = table2array(contourTable(19:31,4));
inside_variablespace1 = inpolygon(R,S,xv1,yv1);
inside_variablespace2 = inpolygon(R,S,xv2,yv2);
inside_variablespace3 = inpolygon(R,S,xv3,yv3);
% x- and y-coordinates corresponding to the discretized points in the
% variable spac
inside_variablespace1(inside_variablespace2) = false;
inside_variablespace1(inside_variablespace3) = false;
xcoord = R(inside_variablespace1);
ycoord = S(inside_variablespace1);
Resulting in:

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 29 Sep 2020
There is no need to use inpolygon here. You can directly create a mask from the Z matrix. Following shows an example, and find the x and y coordinates from the mesh between contour lines at 0.33 and 0.66
[X, Y] = meshgrid(linspace(-1, 1));
Z = X.^2 + Y.^2;
levels = [0 0.33 0.66 1]; % levels for contour plot
mask = Z < 0.66 & Z > 0.33;
x_coord = X(mask);
y_coord = Y(mask);
contour(X, Y, Z, levels)
hold on
plot(x_coord, y_coord, '+')


Show 1 older comment
Ameer Hamza
Ameer Hamza on 29 Sep 2020
You will write it like this
mask = Z < 43 & Z > 40.97;
x_coord = X(mask);
y_coord = Y(mask);
The x-y coordinates will be extracted from corresponding locations in X and Y matrix.
Ameer Hamza
Ameer Hamza on 29 Sep 2020
What is the error. In the data you shared, AoR_mean is undefined, so I cannot run your code to see the issue.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!