Hi Søren,
I've put this back on the list in case it's useful to anyone else, or
if there are better suggestions or improvements around. Hope you don't
mind.
···
On 22/01/2008, Søren Nielsen <soren.skou.nielsen@...287...> wrote:
Yeah i'd like to see your code if I can..
import numpy as n
def get_poly_pts(x, y, shape):
"""Creates convex polygon mask from list of corners.
Parameters
----------
x : array_like
x co-ordinates of corners
y : array_like
y co-ordinates of corners, in order corresponding to x
shape : array_like
dimension sizes of result
Returns
-------
build : ndarray
2-D array of shape shape with values True inside polygon
Notes
-----
Code is constrained to convex polygons by "inside"
assessment criterion.
"""
x = n.asarray(x)
y = n.asarray(y)
shape = n.asarray(shape)
npts = x.size # should probably assert x.size == y.size
inds = n.indices( shape )
xs = inds[0]
ys = inds[1]
xav = n.round(x.mean()).astype(int)
yav = n.round(y.mean()).astype(int)
for i in xrange(npts): # iterate over pairs of co-ordinates
j = (i + 1) % npts
m = (y[j] - y[i])/(x[j] - x[i])
c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
thisone = ( ys > m * xs + c )
if thisone[xav, yav] == False:
thisone = ~thisone
if i == 0:
build = thisone
else:
build &= thisone
return build
(released under BSD licence)
I just needed the push over the edge to know how to draw on the canvas,
mapping clicks etc. since i'm still fairly new to matplotlib, so I think
your code will be helpfull.
I hope so. As you can see this code doesn't do any of the drawing or
click collecting, but the cookbook page should be able to guide you
there. Ask again on the list if you have any further questions and
we'll see if we can help.
Also, the code assumes that the average co-ordinate is inside the
shape - that's true for convex polygons, but not necessarily for
arbitrary ones. I use if after taking a convex hull of a greater list
of points (using the delaunay module in scipy (now in scikits, I
hear)), which ensures convexity. You just need to be aware of that
limitation.
Cheers,
A.
--
AJC McMorland, PhD candidate
Physiology, University of Auckland