inPoints = polygrid(xv,yv,ppa) generates points that are within a polygon using help from the inpolygon function.
xv and yv are columns representing the vertices of the polygon, as used in the Matlab function inpolygon
ppa refers to the points per unit area you would like inside the polygon. Here unit area refers to a 1.0 X 1.0 square in the axes.
L = linspace(0,2.*pi,6); xv = cos(L)';yv = sin(L)'; %from the inpolygon documentation
inPoints = polygrid(xv, yv, 10^5)
plot(inPoints(:, 1),inPoints(:,2), '.k');
Works just as described, great file!
you should consider incorporating the sampling density parameter (i.e. number of nodes per unit area of the polygon) into your function to actually make it useful
Hi Richard, this program takes in only a list of polygon vertices, not a list of data points that may or may not lie within the polygon. It returns a uniform grid of points that lie within the area specified by the polygon.
There's also John D'errico's inhull
to do the same thing, and it works in any number of dimensions.
This submission shows that the author does not understand the potential of built-in inpolygon() function, that can accomplish the same thing with much shorter syntax and faster runtime.
Thanks, you are right I should get those points as a single list. If you have any other suggestions let me know.
Thanks for clearing that up. Just so you know, your implementation could be substantially improved. For example, you don't have to loop through the individual points to determine if they are inside a polygon. The 'inpolygon' function that you are currently using, will accept a list of points and will return a logical array that could be used to index the points that fall inside the polygon.
I think people who study functions with domain as a polygon in R2 could find this useful. Its currently used to study chaotic maps like the Henon map, with polygons formed by intersections of the stable and unstable manifolds.
just wondering, how would this be useful to anyone?
fixed a typo
Updated text to go with my previous file update
Better syntax and running time