Deciding if a Point is Inside a Polygon python
<p>I am trying to detect if a given point(x,y) is in a polygon of n*2 array. But it seems that some points on the borders of the polygon return that it's not include.</p>

<pre><code>def point_inside_polygon(x,y,poly):

    n = len(poly)
    inside =False

    p1x,p1y = poly[0]
    for i in range(n+1):
        p2x,p2y = poly[i % n]
        if y &gt; min(p1y,p2y):
            if y &lt;= max(p1y,p2y):
                if x &lt;= max(p1x,p2x):
                    if p1y != p2y:
                        xinters = (y-p1y)*(p2x-p1x)/float((p2y-p1y))+p1x
                    if p1x == p2x or x &lt;= xinters:
                        inside = not inside
        p1x,p1y = p2x,p2y

    return inside
</code></pre>

<p>You may use <code>contains_point</code> function from <code>matplotlib.path</code> with small negative and positive radius (small trick). Something like this:</p>

<pre><code>import matplotlib.path as mplPath
import numpy as np

crd = np.array([[0,0], [0,1], [1,1], [1,0]])# poly
bbPath = mplPath.Path(crd)
pnts = [[0.0, 0.0],[1,1],[0.0,0.5],[0.5,0.0]] # points on edges
r = 0.001 # accuracy
isIn = [ bbPath.contains_point(pnt,radius=r) or bbPath.contains_point(pnt,radius=-r) for pnt in pnts]
</code></pre>

<p>The result is</p>

<pre><code>[True, True, True, True]
</code></pre>

<p>By default (or <code>r=0</code>) all the points on borders are not included, and the result is</p>

<pre><code>[False, False, False, False]
</code></pre>

python polygon polygons point-in-polygon