Last active
June 15, 2022 04:58
-
-
Save nst/2c5418d289a65a5a2fa7e63a85b7f31b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/usr/bin/env python3 | |
# Nicolas Seriot | |
# 2022-06-14 | |
# https://seriot.ch/visualization/truchet_2.png | |
# loosely inspired by https://www.fxhash.xyz/gentk/slug/dirty-interlaced-truchet-tiles-18 | |
import cairo | |
import math | |
import random | |
NB_COLS = 16 | |
NB_ROWS = 16 | |
W = 32 | |
GRID = False | |
BKG = (241/255., 196./255., 89/255.) | |
EXT = (169/255., 89/255, 55/255.) | |
INT = BKG | |
W_L = 14 | |
W_M = 8 | |
W_S = 4 | |
def draw_arcs(c, rotate): | |
c.save() | |
if rotate: | |
c.translate(W, 0) | |
c.rotate(math.pi/2.) | |
for o, a in [(0, 0), (W, math.pi)]: | |
for (width, color) in [(W_L, EXT), (W_M, INT), (W_S, EXT)]: | |
c.set_line_width(width) | |
c.set_source_rgb(*color) | |
c.arc(o, o, W/2., a, a+math.pi/2.) | |
c.stroke() | |
c.restore() | |
def draw_bridge(c, rotate): | |
c.save() | |
if rotate: | |
c.translate(W, 0) | |
c.rotate(math.pi/2.0) | |
for (x1, y1, x2, y2) in [(W/2., 0, W/2., W), (0, W/2., W, W/2.)]: | |
for (width, color) in [(W_L, EXT), (W_M, INT), (W_S, EXT)]: | |
c.set_line_width(width) | |
c.set_source_rgb(*color) | |
c.move_to(x1, y1) | |
c.line_to(x2, y2) | |
c.stroke() | |
c.restore() | |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, W*NB_COLS, W*NB_ROWS) | |
c = cairo.Context(surface) | |
c.set_source_rgb(*BKG) | |
c.paint() | |
for col in range(NB_COLS): | |
for row in range(NB_ROWS): | |
c.save() | |
c.translate(col*W, row*W) | |
arcs_else_bridge = random.choice([True, False]) | |
rotate = random.choice([True, False]) | |
if arcs_else_bridge: | |
draw_arcs(c, rotate) | |
else: | |
draw_bridge(c, rotate) | |
c.restore() | |
if GRID: | |
c.rectangle(col*W, row*W, W, W) | |
c.stroke() | |
surface.write_to_png("truchet_2.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment