Sometimes you simply want to draw an arrow in a dc.
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 )