from reportlab.lib import colors import math def getAngle(x1, y1, x2, y2): X = 1.0 * ( x2 - x1 ) Y = 1.0 * ( y2 - y1 ) if X==0: angle = 90 + (-1 * (Y<0)) * 180 else: angle = math.atan ( Y / X ) / math.pi * 180 if X<0: angle = angle + 180 return angle class arrow: def __init__(self, x1, y1, x2, y2, width): self.x1 = x1 self.y1 = y1 self.x2 = x2 self.y2 = y2 self.width = width self.arrowpoints = [ ( 0, 2.0 * width ), ( 4.0 * width, 0 ), ( 0, -2.0 * width ) ] def drawOn(self, canvas): canvas.saveState() canvas.setLineWidth(self.width) canvas.setStrokeColor(colors.black) canvas.setFillColor(colors.black) canvas.line(self.x1, self.y1, self.x2, self.y2) # draw the triangle for the arrowhead canvas.translate (self.x2, self.y2) angle = getAngle(self.x1, self.y1, self.x2, self.y2) canvas.rotate(angle) path = canvas.beginPath() (x,y) = self.arrowpoints[0] path.moveTo(x,y) for (x,y) in self.arrowpoints[1:]: path.lineTo(x,y) path.close() canvas.drawPath(path, fill=1) canvas.restoreState()