File Exchange

image thumbnail

Grid of points within a polygon

version 1.8 (1.5 KB) by

This function generates an array of points that lie within a given polygon



View License

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');

Comments and Ratings (10)


Zhou (view profile)

Works just as described, great file!

Anton Semechko

Anton Semechko (view profile)

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.

Richard Crozier

There's also John D'errico's inhull

to do the same thing, and it works in any number of dimensions.

Bruno Luong

Bruno Luong (view profile)

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.

Anton Semechko

Anton Semechko (view profile)

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.

Anton Semechko

Anton Semechko (view profile)

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


clarified description

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video