From f15b1a9990ab0f232d2893ed16f67f9f7010ed23 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Fri, 19 Sep 2025 16:22:44 -0500 Subject: [PATCH] Fixing arrow events to draw head --- src/data/events/brushes/arrow.py | 44 +++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/data/events/brushes/arrow.py b/src/data/events/brushes/arrow.py index 17818ad..578b0ef 100644 --- a/src/data/events/brushes/arrow.py +++ b/src/data/events/brushes/arrow.py @@ -1,4 +1,5 @@ # Python imports +import math # Lib imports import cairo @@ -33,12 +34,49 @@ class Arrow(BrushEvent): def process(self, brush: cairo.Context): - brush.set_line_width(self.size) brush.set_line_cap(1) # 0 = BUTT, 1 = ROUND, 2 = SQUARE brush.set_source_rgba(*self.color) brush.set_operator(cairo.OPERATOR_OVER); - brush.move_to(self.points[0].x, self.points[0].y) - brush.line_to(self.points[1].x, self.points[1].y) + x1, y1 = self.points[0].x, self.points[0].y + x2, y2 = self.points[1].x, self.points[1].y + + # Arrow body + brush.move_to(x1, y1) + brush.line_to(x2, y2) + + # Arrow head + ah1, ay1, \ + ah2, ay2 = self.get_arrow_head_points(x1, y1, x2, y2) + + brush.move_to(x2, y2) + brush.line_to(ah1, ay1) + brush.move_to(x2, y2) + brush.line_to(ah2, ay2) + brush.stroke() + + def get_arrow_head_points(self, x1, y1, x2, y2, arrow_head_length = 25, arrow_angle = 45): + # Convert angle to radians + angle_rad = math.radians(arrow_angle) + + # Calculate the angle of the main line + line_angle = math.atan2(y2 - y1, x2 - x1) + + # Arrowhead angles (one on each side of the line) + angle1 = line_angle + math.pi - angle_rad + angle2 = line_angle + math.pi + angle_rad + + # Calculate the two arrowhead points + p1 = ( + x2 + arrow_head_length * math.cos(angle1), + y2 + arrow_head_length * math.sin(angle1) + ) + + p2 = ( + x2 + arrow_head_length * math.cos(angle2), + y2 + arrow_head_length * math.sin(angle2) + ) + + return *p1, *p2