Friday 23 September 2016

How to find if point is in or out of polygon? (in IDL)

It's a common problem in visualization and many other applications. There are many ways to solve it, but not all the ways are equally efficient. Baard Krane wrote a very elegant solution in IDL that was later vectorized by William Connelly and popularized in the community - of course - by David Fanning (see his article here and get the source of the inside.pro).

Example:
IDL> px = [0.3, 0.3, 0.7, 0.7]
IDL> py = [0.3, 0.7, 0.7, 0.3]
IDL> x = RANDOMU(seed1, 5000) 
IDL> y = RANDOMU(seed2, 5000) 
IDL> id = INSIDE(x, y, px, py, /ind)          
IDL> PLOT, x, y, psym = 3                     
IDL> OPLOT, [px, 0.3], [py, 0.3]              
IDL> OPLOT, x[id], y[id], psym = 3, col = 255