> from collections import LineCollection
> class Arrow(LineCollection):
> """
> An arrow
> """
> def __init__( self, x, y, dx, dy, width=1.0, arrowstyle='solid',
> **kwargs ):
> """Draws an arrow, starting at (x,y), direction and length
> given by (dx,dy) the width of the arrow is scaled by width.
> arrowstyle may be 'solid' or 'barbed'
> """
> L = math.hypot(dx,dy) or 1 # account for div by zero
> S = 0.1
> arrow = {'barbed': array([[0.,0.], [L,0.], [L-S,S/3],
> [L,0.], [L,-S/3], [L,0.]]),
> 'solid': array([[0.,0.], [L-S,0.], [L-S,S/3],
> [L,0.], [L-S,-S/3], [L-S,0.]])
> }[arrowstyle]
>
> cx = float(dx)/L
> sx = float(dy)/L
> M = array([[cx, sx], [-sx, cx]])
> verts = matrixmultiply(arrow, M) + [x,y]
> LineCollection.__init__(self, [tuple(t) for t in verts],
> **kwargs)
I've found one problem with your Arrow LineCollection; it's not actually
a line collection. It's one line, so some of the LineCollection
functions fail on it. You need to break up the arrow into segments,
like this:
'barbed': array([ [ [0.,0.], [L,0.] ],
[ [L,0.], [L-S,S/3] ],
[ [L,0.], [L-S,-S/3] ] ]
Except just doing this will break the matrixmultiply. Just a heads-up.
Jordan
|