File Exchange

image thumbnail

inpoly mex file

version (68.1 KB) by Sebastien PARIS
Find points inside or on the border of a polygon


Updated 12 May 2014

View License

A fast mex version of the modified inpoly function.
please run mexme_inpoly.m to compile mex file on your plateform

please run test_inpoly.m for demo

Comments and Ratings (15)

SOLVED Mex error with R2018a and newer
the inpoly.c code contains errors in lines 400 and 415. The function is not MxErrMsgTxt but mexErrMsgTxt.
Change the function and you should be able to mex again.

Does not work with R2018a.
When creating the mex file, it states an error. I had a previous mex version that no longer works either in version R2018a.
The error when creating the mex file is:
Creating library inpoly.lib and object inpoly.exp
inpoly.obj : error LNK2019: unresolved external symbol mxErrMsgTxt referenced in function mexFunction
inpoly.mexw64 : fatal error LNK1120: 1 unresolved externals

For my use case, this mex file is 1000-1500 times faster than the default inpolygon function that comes with matlab. Nicely done!

great! this was 50 times faster than inpoly.m for my application

Very good function used on my project. Thanks.

You are right, I'll upload a version with argument checking. Thanks

John Kua

Thanks! This has been very useful on a number of projects!

One problem - the function does not handle an empty input array of [2 x 0] and crashes MATLAB. Adding a check of the input dimensionality with a warning if the input is empty and perhaps returning an empty array would be nice.

Please give me an example of this bahaviour.

some points are on the edges of the polygon and the function can not detect that. what is the reason?

it sounds like inpoltgon.

Very fast!!! well done.

I thing I found an error in the demo file:
The second demo model is buggy: the polygon is self-interecant!!

Martin Podt

Very useful. On the examples I have tested, the current mex version of inpoly is about 2 times faster than the *.m file.

Sébastien Paris

sorry for my poor english level skill ...

Sébastien Paris

Yes in fact, I did this mex file a long times ago. I realized that you update the algorithm. Maybe if I had 1 hour, I'll recreate the new version of this mex-file. If not, I'll remove this submission

Darren Engwirda

Well, I guess this is what you get when you "mex" someone else's code...

This mex file is based on an old version of my "inpoly.m" function. Unfortunately for the author I've made significant improvements to the algorithm since then and a different algorithm is used in my new "inpoly.m" function.

This mex file appears to be slower than my new "inpoly.m" function by a factor of between 3 and 4 on the examples I've tested, even though I have still only coded it as a *.m file.

There is also no "help inpoly" available unless you directly open the *.c file and it also seems that the inputs are required to be transposed when compared with the original "inpoly.m" function.

As with all versions/copies of "inpoly" the point of this code is that it's faster than the MATLAB "inpolygon.m" function, especially for large inputs.


- Fix a bug (missing free)

- Add online help
- Cosmetic changes in inpoly.c and demo file

-Inputs arguments checking
-Should compile on Linux

- Inputs arguments checking
- Should compile with Linux/GCC

New algorithm imcorporated. Speed improvement between 50% up to 100%

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux