Skip to content

Instantly share code, notes, and snippets.

@nst
Last active June 14, 2022 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nst/115cdee5f2c7ac88b819edb526dfcb2a to your computer and use it in GitHub Desktop.
Save nst/115cdee5f2c7ac88b819edb526dfcb2a to your computer and use it in GitHub Desktop.
# !/usr/bin/env python3
# Nicolas Seriot
# 2022-06-10
# https://seriot.ch/visualization/truchet_simple.png
import cairo
import math
import random
NB_COLS = 8
NB_ROWS = 8
W = 32
GRID = True
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, W*NB_COLS, W*NB_ROWS)
c = cairo.Context(surface)
c.set_source_rgb(1,1,1)
c.paint()
c.set_source_rgb(0,0,0)
for col in range(NB_COLS):
for row in range(NB_ROWS):
c.save()
c.translate(col*W, row*W)
# rotate if needed
if random.choice([True, False]):
c.translate(W, 0)
c.rotate(math.pi/2.0)
# draw two arc quarters
for o, a in [(0, 0), (W, math.pi)]:
c.arc(o, o, W/2.0, a, a+math.pi/2.0)
c.stroke()
c.restore()
if GRID:
c.rectangle(col*W, row*W, W, W)
c.stroke()
surface.write_to_png("truchet_simple.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment