Points inside multiple polygon for contour
12 views (last 30 days)
Show older comments
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');
xlabel('\mu_s')
ylabel('\mu_r')
clabel(C,h,'LabelSpacing',500,'FontSize',8);
h.LevelList = round(h.LevelList,2);
set(gca,'XTick',0.1:0.1:0.9,'XTickLabel',0.1:0.1:0.9,'XTickLabelRotation',45);
set(gca,'YTick',0.1:0.1:0.9,'YTickLabel',0.1:0.1:0.9);
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);
plot(R,S,'.r');
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.
0 Comments
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.
8 Comments
Adam Danz
on 29 Sep 2020
Edited: 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.
Example
A = inpolygon(...)
B = inpolygon(...)
A(B) = false;
Use these answers as a stepping stone rather than looking for full solutions.
More Answers (1)
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, '+')
4 Comments
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!