Points inside multiple polygon for contour

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.

 Akzeptierte Antwort

KSSV
KSSV am 29 Sep. 2020

0 Stimmen

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 Kommentare

Tessa Kol
Tessa Kol am 29 Sep. 2020
I did read about inpolygon and tried the solution with NaN. Didn't work.
Also I used the (x,y) coordinates of the contourlines. I obtained these coordinates with the function getContourLineCoordinates.
Since that didn't work either, what can be the problem?
Tessa Kol
Tessa Kol am 29 Sep. 2020
I think I know where is goes wrong.
Matlab connects two boundary lines that shouldn't be connected. Hence the problem.
KSSV
KSSV am 29 Sep. 2020
You remove the boundary line..
Tessa Kol
Tessa Kol am 29 Sep. 2020
How can you do that?
Adam Danz
Adam Danz am 29 Sep. 2020
Bearbeitet: Adam Danz am 29 Sep. 2020
Tessa, the polygon is defined by the (x,y) values within level 40.94, only.
idx = contourTable.Level==40.94; % or contourTable.Group==1
inpolygon(x,y,contourTable.X(idx), contourTable.Y(idx))
Tessa Kol
Tessa Kol am 29 Sep. 2020
But how does that work if I have the boundary 40.94 and 43, respectively?
Adam Danz
Adam Danz am 29 Sep. 2020
Bearbeitet: Adam Danz am 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.
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:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ameer Hamza
Ameer Hamza am 29 Sep. 2020

0 Stimmen

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 Kommentare

Tessa Kol
Tessa Kol am 29 Sep. 2020
How do you propose to do this since the contourlines are 40.97 and 43, but the (x,y) coordinates of those contourlines are between 0.4 and 0.9?
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.
Tessa Kol
Tessa Kol am 29 Sep. 2020
But that doesn't work here.
Ameer Hamza
Ameer Hamza am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Contour Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 29 Sep. 2020

Kommentiert:

am 29 Sep. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by