Re: Is there any way to draw a line with arrow in wx?

Edward Sitarski <[email protected]> Sat, 11 Sep 2021 08:55:12 -0700 (PDT)
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
I realize this question was asked a long time ago.
Sometimes you simply want to draw an arrow in a dc.
I recently ran into this problem.  See below:

from math import atan2, sin, cos, pi
def DrawArrowLine( dc, x0, y0, x1, y1, arrowFrom=True, arrowTo=True, 
arrowLength=16, arrowWidth=8 ):
    '''
        Draws a line with arrows in a regular wxPython DC.
        The line is drawn with the dc's wx.Pen.  The arrows are filled with 
the current Pen's colour.
        Edward Sitarski 2021.
    '''
    dc.DrawLine( x0, y0, x1, y1 )
    if x0 == x1 and y0 == y1:
        return
    
    # Set up the dc for drawing the arrows.
    penSave, brushSave = dc.GetPen(), dc.GetBrush()
    dc.SetPen( wx.TRANSPARENT_PEN )
    dc.SetBrush( wx.Brush(dc.GetPen().GetColour()) )
    
    # Compute the "to" arrow polygon.
    angle = atan2( y1 - y0, x1 - x0 )
    toCosAngle, toSinAngle = -cos(angle), -sin(angle)
    toArrowPoly = [
        (int(xp*toCosAngle - yp*toSinAngle), int(yp*toCosAngle + 
xp*toSinAngle)) for xp, yp in (
            (0,0),
            (arrowLength, arrowWidth/2),
            (arrowLength, -arrowWidth/2),
        )
    ]
    
    # Draw the arrows.
    if arrowTo:
        dc.DrawPolygon( toArrowPoly, x1, y1 )
    if arrowFrom:
        dc.DrawPolygon( [(-x,-y) for x,y in toArrowPoly], x0, y0 )
    
    # Restore the dc.
    dc.SetPen( penSave )
    dc.SetBrush( brushSave )
On Friday, February 22, 2013 at 12:06:08 PM UTC-5 [email protected] wrote:

> On Thu, Feb 21, 2013 at 1:21 PM, Vlastimil Brom
> <[email protected]> wrote:
> > 2013/2/21 Shannon <[email protected]>:
> >> I was wondering is there any way to draw a line with an arrow, which can
> >> indicate the from 'point 1' point to 'point 2'.
>
> > I haven't used any of these myself, but OGL and FloatCanvas come to
> > mind in this context; see the wxpython demo:
> > More windows/controls: FloatCanvas
>
> Indeed -- for FloatCanvas, look at the main Demo, look at the "Arrows"
> demo under the "Tests" Menu - the arrows are not particularly pretty,
> but you could make your own DrawObject with prettier ones if you want.
>
> There are also bunch more little FloatCanvas demos in SVN also:
>
> http://svn.wxwidgets.org/svn/wx/wxPython/3rdParty/FloatCanvas/Demos/
>
> The VectPlot.py demo uses arrows.
>
> -Chris
>
>
> -- 
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R (206) 526-6959 voice
> 7600 Sand Point Way NE (206) 526-6329 fax
> Seattle, WA 98115 (206) 526-6317 main reception
>
> [email protected]
>

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/9d570b92-3409-4c65-b6b9-ab6826c088bdn%40googlegroups.com.