Example circles.py

Code: examples/circles.py on github

The image is just a set of intersecting circles, but they are arranged to intersect in a way which creates an interesting pattern.

Code

import sympl.imagefile
import math
 
width = 600
height = 600
gridsize = 100
radius = 117.8
 
def draw(ctx):
    for x in range(gridsize//2, width, gridsize):
        for y in range(gridsize//2, height, gridsize):
            ctx.arc(x, y, radius, 0, 2*math.pi)
            ctx.set_source_rgb(0, 0.5, 0)
            ctx.set_line_width(3)
            ctx.stroke()
 
sympl.imagefile.save(width, height, draw, "circles.png")

We define a few constants - the width and height of the image, the distance between centres of the circles (gridsize) and the radius of each circle. The radius is carefully chosen so that the circles intersect in an interesting way.

Our draw function executes a double loop to draw a grid of circles. Since pycairo only supports drawing arcs, not circles, we must draw a circle by creating an arc with an angle of 2*pi radians (equivalent to 360 degrees), which makes a full circle.