how to tell if a point is inside a polygon

Is there a routine in matplotlib for telling whether a point is inside a convex 4 sided polygon?

Mathew

If you can convert your polygon to a path, you can use the "contains_point" method:

>>> from matplotlib import path
>>> p = path.Path([[0,0], [42, 3], [45, 23], [1, 32]])
>>> p.contains_point([5,5])
1
>>> p.contains_point([72, 3])
0

Mike

Mathew Yeates wrote:

···

Is there a routine in matplotlib for telling whether a point is inside a convex 4 sided polygon?

Mathew

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Mathew,
Have you tried the solution that was suggested by Angus yesterday on the numpy
mailing list ?

http://projects.scipy.org/pipermail/scipy-user/2008-February/015418.html

import numpy as np
import matplotlib.nxutils as nxutils
polygon=np.array([(0,0),(0,1),(1,1),(1,0)])
points = np.array([(0.5,0.5),(0.4,1.5)])
nxutils.points_inside_poly(points, polygon)

array([1, 0], dtype=int32)

Meaning that the first point is, the second is not.

Thanks! Thats exactly what I was looking for!

Mathew

Michael Droettboom wrote:

···

If you can convert your polygon to a path, you can use the "contains_point" method:

>>> from matplotlib import path
>>> p = path.Path([[0,0], [42, 3], [45, 23], [1, 32]])
>>> p.contains_point([5,5])
1
>>> p.contains_point([72, 3])
0

Mike

Mathew Yeates wrote:

Is there a routine in matplotlib for telling whether a point is inside a convex 4 sided polygon?

Mathew

-------------------------------------------------------------------------

This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
  

Angus originally suggest matplotlib. The other proposed solutions are overkill, unless it turns out that performance is a problem

Thanks
Mathew

Pierre GM wrote:

···

Mathew,
Have you tried the solution that was suggested by Angus yesterday on the numpy mailing list ?

http://projects.scipy.org/pipermail/scipy-user/2008-February/015418.html

import numpy as np
import matplotlib.nxutils as nxutils
polygon=np.array([(0,0),(0,1),(1,1),(1,0)])
points = np.array([(0.5,0.5),(0.4,1.5)])
nxutils.points_inside_poly(points, polygon)
        

array([1, 0], dtype=int32)

Meaning that the first point is, the second is not.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Pierre GM wrote:

Mathew,
Have you tried the solution that was suggested by Angus yesterday on the numpy mailing list ?

http://projects.scipy.org/pipermail/scipy-user/2008-February/015418.html

import numpy as np
import matplotlib.nxutils as nxutils
polygon=np.array([(0,0),(0,1),(1,1),(1,0)])
points = np.array([(0.5,0.5),(0.4,1.5)])
nxutils.points_inside_poly(points, polygon)

array([1, 0], dtype=int32)

Meaning that the first point is, the second is not.

It seems to me that the output should be boolean, so the line

   mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions,PyArray_INT);

in nxutils.cpp would become

   mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions, NPY_BOOL);

Can anyone think of anything this would break, or any disadvantages?

Eric

This looks better to me too - -I suggest testing it with the lasso
demo which was the example that motivated this code.

JDH

···

On Tue, Oct 14, 2008 at 2:58 PM, Eric Firing <efiring@...202...> wrote:

mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions,PyArray_INT);

in nxutils.cpp would become

mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions, NPY_BOOL);

Can anyone think of anything this would break, or any disadvantages?

John Hunter wrote:

···

On Tue, Oct 14, 2008 at 2:58 PM, Eric Firing <efiring@...202...> wrote:

mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions,PyArray_INT);

in nxutils.cpp would become

mask = (PyArrayObject *)PyArray_SimpleNew(1,dimensions, NPY_BOOL);

Can anyone think of anything this would break, or any disadvantages?

This looks better to me too - -I suggest testing it with the lasso
demo which was the example that motivated this code.

JDH

It worked, so I went ahead and committed the change. I had to change one other line, and while in the neighborhood I replaced the other old-form PyArray_* with NPY_*.

Eric