Debian Codegraph @

examples/cairo_snippets/snippets/curve_rectangle.py

raw symbols SCIP
1 def draw(cr, width, height):
2 cr.scale(width, height)
3 cr.set_line_width(0.04)
4
5 # a custom shape, that could be wrapped in a function
6 x0 = 0.1 # parameters like cairo_rectangle
7 y0 = 0.1
8 rect_width = 0.8
9 rect_height = 0.8
10 radius = 0.4 # and an approximate curvature radius
11
12 x1 = x0 + rect_width
13 y1 = y0 + rect_height
14
15 if rect_width / 2 < radius:
16 if rect_height / 2 < radius:
17 cr.move_to(x0, (y0 + y1) / 2)
18 cr.curve_to(x0, y0, x0, y0, (x0 + x1) / 2, y0)
19 cr.curve_to(x1, y0, x1, y0, x1, (y0 + y1) / 2)
20 cr.curve_to(x1, y1, x1, y1, (x1 + x0) / 2, y1)
21 cr.curve_to(x0, y1, x0, y1, x0, (y0 + y1) / 2)
22 else:
23 cr.move_to(x0, y0 + radius)
24 cr.curve_to(x0, y0, x0, y0, (x0 + x1) / 2, y0)
25 cr.curve_to(x1, y0, x1, y0, x1, y0 + radius)
26 cr.line_to(x1, y1 - radius)
27 cr.curve_to(x1, y1, x1, y1, (x1 + x0) / 2, y1)
28 cr.curve_to(x0, y1, x0, y1, x0, y1 - radius)
29 else:
30 if rect_height / 2 < radius:
31 cr.move_to(x0, (y0 + y1) / 2)
32 cr.curve_to(x0, y0, x0, y0, x0 + radius, y0)
33 cr.line_to(x1 - radius, y0)
34 cr.curve_to(x1, y0, x1, y0, x1, (y0 + y1) / 2)
35 cr.curve_to(x1, y1, x1, y1, x1 - radius, y1)
36 cr.line_to(x0 + radius, y1)
37 cr.curve_to(x0, y1, x0, y1, x0, (y0 + y1) / 2)
38 else:
39 cr.move_to(x0, y0 + radius)
40 cr.curve_to(x0, y0, x0, y0, x0 + radius, y0)
41 cr.line_to(x1 - radius, y0)
42 cr.curve_to(x1, y0, x1, y0, x1, y0 + radius)
43 cr.line_to(x1, y1 - radius)
44 cr.curve_to(x1, y1, x1, y1, x1 - radius, y1)
45 cr.line_to(x0 + radius, y1)
46 cr.curve_to(x0, y1, x0, y1, x0, y1 - radius)
47
48 cr.close_path()
49
50 cr.set_source_rgb(0.5, 0.5, 1)
51 cr.fill_preserve()
52 cr.set_source_rgba(0.5, 0, 0, 0.5)
53 cr.stroke()