Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow in pycairo

I need to draw a directed line or arrow.

context.move_to (10,10) context.line_to(10,100)

this provide an undirected line but I need an arrow. How can I accomplish this in pycairo?

like image 631
softProcess Avatar asked Apr 01 '26 09:04

softProcess


1 Answers

Since there is no builtin arrow, you'll have to define one geometrically. Here's how.

arrow_length = 0.45
arrow_angle = math.pi/4
arrowhead_angle = math.pi/6
arrowhead_length = 0.3

ctx.move_to(0.5, 0.5) # move to center of canvas

ctx.rel_line_to(arrow_length * math.cos(arrow_angle), arrow_length * math.sin(arrow_angle))
ctx.rel_move_to(-arrowhead_length * math.cos(arrow_angle - arrowhead_angle), -arrowhead_length * math.sin(arrow_angle - arrowhead_angle))
ctx.rel_line_to(arrowhead_length * math.cos(arrow_angle - arrowhead_angle), arrowhead_length * math.sin(arrow_angle - arrowhead_angle))
ctx.rel_line_to(-arrowhead_length * math.cos(arrow_angle + arrowhead_angle), -arrowhead_length * math.sin(arrow_angle + arrowhead_angle))

ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.08)
ctx.stroke()

This produces the following image:

enter image description here

Change the parameters as you need.

like image 115
scatter Avatar answered Apr 02 '26 22:04

scatter