Skip to content

Instantly share code, notes, and snippets.

@nst
Created October 9, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nst/915154a01f5042d86c7bfc33fb0658d8 to your computer and use it in GitHub Desktop.
Save nst/915154a01f5042d86c7bfc33fb0658d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# 2021-10-09
# Emilien & Nicolas Seriot
# https://seriot.ch/visualization/lines_1.png
import cairo
import random
WIDTH = 800
HEIGHT = 600
MARGIN = 20
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
c = cairo.Context(surface)
c.set_source_rgb(0.85,0.85,0.85)
c.paint()
c.set_line_width(2)
c.set_line_join(cairo.LINE_JOIN_ROUND)
c.set_line_cap(cairo.LINE_CAP_ROUND)
x = WIDTH / 2
y = HEIGHT / 2
for i in range(400):
c.move_to(x, y)
n = random.randint(0,3)
l = random.choice([20, 50, 100])
if n == 0:
x = x + l
elif n == 1:
y = y + l
elif n == 2:
y = y - l
elif n == 3:
x = x - l
x = max(min(WIDTH - MARGIN, x), MARGIN)
y = max(min(HEIGHT - MARGIN, y), MARGIN)
c.set_source_rgb(0,0,0)
c.line_to(x, y)
c.stroke()
surface.write_to_png("lines_1.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment