How can I find all points inside a polygon?

25 visualizaciones (últimos 30 días)
Tyler
Tyler el 27 de Ag. de 2016
Respondida: Star Strider el 27 de Ag. de 2016
I have the bounds of a polygon in x- and y-coordinates. My end goal is to get a matrix of all x-values and a corresponding matrix of all y-values for the points in/on that polygon. If possible, I would also like to color in the specified polygon on a 2d plot. Does anyone know which commands/functions I can use to do this? Most of what I have seen simply confirms whether input values lie in/on a polygon. Thank you!

Respuestas (2)

Star Strider
Star Strider el 27 de Ag. de 2016
The inpolygon function returns a logical matrix of points that are in (or on if you request the second output) the polygon you give to it as an input. To get the points inside the polygon as row-column coordinates, you need to get their indices.
See if this does something similar to what you want:
xq = rand(1,100); % Random X-Coordinates
yq = rand(1,100); % Random Y-Coordinates
xv = [0.3, 0.3, 0.7, 0.7, 0.3]; % Polygon X-Coordinates
yv = [0.3, 0.7, 0.7, 0.3, 0.3]; % Polygon Y-Coordinates
[in,on] = inpolygon(xq,yq, xv,yv); % Logical Matrix
inon = in | on; % Combine ‘in’ And ‘on’
idx = find(inon(:)); % Linear Indices Of ‘inon’ Points
xcoord = xq(idx); % X-Coordinates Of ‘inon’ Points
ycoord = yq(idx); % Y-Coordinates Of ‘inon’ Points
figure(1)
plot(xq, yq, 'bp') % Plot All Points
hold on
plot(xv, yv, '-r') % Plot Polygon
plot(xcoord, ycoord, 'gp') % Overplot ‘inon’ Points
hold off

Image Analyst
Image Analyst el 27 de Ag. de 2016
Editada: Image Analyst el 27 de Ag. de 2016
There is an inpolygon() function. Give it arrays of the x and y bounding vertices, and a test point, and it will tell you if the test point is inside the polygon defined by the vertices you gave.
You can also try patch() or fill() to make colored polygons on an axes.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by