On Wednesday, 28 January 2015 02:50:19 UTC, Steven G. Johnson wrote: > > In Julia for this problem, I would just write a nested loop: loop over > points in the mask, and for each point loop over the (ordered) polygon > edges to check whether the point is in the polygon via the usual algorithm > (for an edge v1->v2 and a point p, check whether the cross product > (p-v1)x(v2-v1) has a consistent sign for all the edges). Probably you > should also start by computing the bounding box of the polygon and restrict > yourself to looking at mask points inside this bounding box. Should take > about 30 lines of code, tops. >
Checking each pixel to see whether it is in the polygon is very inefficient, as you basically do a test involving each edge for each pixel. There is a more efficient approach that involves looking at lines of pixels (rows or columns), this only requires a test involving each edge once for each line of pixels. You can see details here: http://alienryderflex.com/polygon_fill/ [Apologies if you see this post twice, it's been a number of hours since my original post and it hasn't appeared.] - Andrew