Fixing arrow events to draw head
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user