inpolygon function that works using gpuArray
Updated 31 Jan 2015

View License

This is a point-in-polygon function that can run on a gpu using large test point array sizes. It uses a simple ray-casting algorithm without pre-processing or "on" tolerance checks. Therefore it may not give the exact same results as inpolygon. Inputs must all be gpuArray objects in order to run on the gpu. The inputs take the form of MATLAB's inpolygon function. The parallel process occurs once for every vertex of the polygon, so large numbers of vertices will run slow. I had to have around around 1e5-1e7 test points to see speedup on my simple gaming GPU on a polygon with 1e4 vertices. I am hoping that re-implementing this using mex CUDA code will result in faster run-times. For best results, the number of test points should be close to the maximum array size on your GPU.
This implementation of the ray-casting algorithm is based on C code by W. Randolf Franklin, which can be found at

Any advice is welcome on how to speed up this algorithm, or make it more useful. Thanks!


L = linspace(0,2.*pi,6);
xv = gpuArray(cos(L)');
yv = gpuArray(sin(L)');


in=inpolygon_for_gpu(pts_x, pts_y, xv, ,yv);

Cite As

Sulimon Sattari (2024). inpolygon_for_gpu (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on GPU Computing in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

added more description

Clarified description

Clean up description

removed function arguments from title