Search⌘ K
AI Features

Circle and Curves

Explore how to draw arcs and complete circles with Pycairo's arc function, using radians for angle measurements. Learn to create versatile Bezier curves with control and anchor points, and combine curves and lines to form complex shapes.

We'll cover the following...

Arcs

An arc is a part of the circumference of a circle. The arc function is called like this: ctx.arc(cx,cy,r,a1,a2)

The following image shows how these parameters are used to draw an arc:

The arc is part of a circle, with radius r and centered at the point (cx, cy). It starts at angle a1 and ends at angle a2. Angles are measured from the x-axis and, by default, increase as we move clockwise. Here is an example of the code used to draw an arc:

Python 3.8
import cairo
import math
# Set up pycairo
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 600, 400)
ctx = cairo.Context(surface)
ctx.set_source_rgb(0.8, 0.8, 0.8)
ctx.paint()
# Draw an arc
ctx.arc(300, 200, 150, 0, math.pi/2)
ctx.set_source_rgb(1, 0, 0)
ctx.set_line_width(10)
ctx.stroke()

Angles in Python, as in most computer languages, are measured in radians rather than ...