File Exchange

image thumbnail

2D polygon interior detection

version (24.2 KB) by Bruno Luong
Detect a whereas a point is interior or exterior to a 2D polygon


Updated 07 Jun 2010

View Version History

View License

This function tries to improve the slow Matlab INPOLYGON function.

There are few other fast codes on FEX (e.g., by Darren Engwirda). After exploring many different ways, I come to the conclusion that Darren Engwirda's approach is close to optimal. So here you'll find a general method that share many common aspects, but implemented in C-Mex. I have makes few contributions: (1) a lean C-implementation, (2) Using Matlab SORT and HISTC for binning, (3) Derive an empirical rule when to apply the sorting, (4) a more rigorous on-boundary detection.

Cite As

Bruno Luong (2021). 2D polygon interior detection (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)


A good order of magnitude quicker than inbuilt polyshape isinterior function especially when multiple points (literally 10's of 1000) are queried at time.

Shakir Hussain

Why it is giving me blank output? My data and code is ok

Shakir Hussain

Reema Alhassan

Bruno Luong

Reema, you need

1. Having the C compiler installed
2. to compile the C-mex file by runing insidepoly_install.m

Reema Alhassan

I'm getting this error where is insidepoly_dblengine?
Undefined function or variable 'insidepoly_dblengine'.

Error in insidepoly>insidepoly1 (line 371)
[in on] = insidepoly_dblengine(x, y, Px1, Py1, Px2, Py2, ontol, ...

Error in insidepoly (line 17)
mask =insidepoly1(X,Y,rx,ry);


Really fast and gives same results as inpolygon Matlab version.

Alex Barnett

This code is great. 100x faster on large-scale problems than even the latest version of MATLAB I have (R2015b), amazingly! You can run my tests (within mpspack) at:




Bruno Luong

To Matt: this is algorithm designed for for generic polygons (as with INPOLYGON)

Matt J

Does this submission apply to convex polygons or general ones? If the latter, there should be a way to make it faster if the polygon is known a priori to be convex. Perhaps an input parameter to enable this...

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

Community Treasure Hunt

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

Start Hunting!