Search⌘ K
AI Features

Open and Closed Shapes

Explore how to draw open and closed shapes using Pycairo in Python. Learn to manipulate paths with move_to and line_to functions, and use close_path to complete polygons. This lesson helps you understand vector graphics construction for complex shape creation.

We'll cover the following...

In the previous lessons, we used the rectangle function. Generally, though, if we need to create any other type of shape, we construct it with separate lines and curves. Let’s start by drawing a simple straight line. Here is how a line is drawn on a surface:

Lines

Pycairo uses the idea of a current point when drawing shapes. This makes it easy to create common shapes, such as polygons, which are formed through the end-to-end joining of lines or curves.

Python 3.8
import cairo
# Set up surface
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 a line
ctx.move_to(100, 100)
ctx.line_to(500, 300)
ctx.set_source_rgb(1, 0, 0)
ctx.set_line_width(10)
ctx.stroke()
...