On Sunday, January 25, 2015 at 11:03:30 AM UTC-5, Andrei Zh wrote:
>
> In Matlab, `poly2mask` transforms polygon (given by arrays of `x` and `y` 
> coordinates) into a binary mask, where all values inside polygon get value 
> 1 and all others get value 0. I found Octave implementation [1] that I can 
> translate into Julia, but it's GPL-licensed, and I'd like to avoid copyleft 
> to make my library more flexible for users. Are there any better options or 
> components in Julia that I can reuse for this task?
>

You wouldn't want to base a Julia version on an Octave or Matlab version 
anyway.  Those languages force you to jump through lots of hoop in order to 
vectorize your code, resulting in rather convoluted implementations of 
simple algorithms.

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.

Reply via email to