### arrowline.py -- arrow-headed lines for reportlab.pdfgen # # This uses getLineWidth(), getFillColor() and getStrokeColor(), # which aren't in the standard pdfgen (yet anyway). The easiest way # to do without would be to # send the line width in as a parameter # set the fill color before calling # # Sorry I'm too lazy to convert this to standalone myself. # # Steve Witham ess doubleyou at tiac remove-this dot net. # http://www.tiac.net/~sw from reportlab.pdfgen import canvas from math import sin, cos, pi, sqrt, atan2 def arrowLine( a_canvas, x0, y0, x1, y1, tail=False, head=True, angle=22.5, width=4 ): w = a_canvas.getLineWidth() dx, dy = x1-x0, y1-y0 d = sqrt( dx*dx + dy*dy ) if d == 0: return dx, dy = dx/d, dy/d sc, fc = a_canvas.getStrokeColor(), a_canvas.getFillColor() if fc != sc: a_canvas.setFillColor( sc ) if head: x1, y1 = arrowHead( a_canvas, x1, y1, dx, dy, angle, width ) if tail: x0, y0 = arrowHead( a_canvas, x0, y0, -dx, -dy, angle, width ) if fc != sc: a_canvas.setFillColor( fc ) a_canvas.line( x0, y0, x1, y1 ) def arrowHead( a_canvas, x, y, dx, dy, wedgeAngle, width ): # lineWidth * width = width across the back end of the arrowhead. wa = wedgeAngle * pi / 180.0 s, c = sin( wa ), cos( wa ) lineAngle = atan2( dy, dx ) * 180 / pi r = a_canvas.getLineWidth() * width / ( 2 * s ) a_canvas.wedge( x-r, y-r, x+r, y+r, lineAngle+180-wedgeAngle, wedgeAngle*2, fill=1, stroke=0 ) return x - dx * r * c, y - dy * r * c