Skip to content

Instantly share code, notes, and snippets.

@Alfo5123
Last active January 22, 2019 02:07
Show Gist options
  • Save Alfo5123/85ae5ecc6bf7efebb2ec430d95d3ea91 to your computer and use it in GitHub Desktop.
Save Alfo5123/85ae5ecc6bf7efebb2ec430d95d3ea91 to your computer and use it in GitHub Desktop.
import cairocffi as cairo
from math import hypot, pi, sqrt
from random import random, seed
seed(511)
W, H, N, I = 2000, 2000, 15, 2000
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, W, H)
cr = cairo.Context(surface)
cr.set_source_rgb(0,0,0)
cr.paint()
cr.set_line_width(1)
cr.set_source_rgba(0,1.0,0,0.05)
points = [(random()*W , random()*H) for _ in range(N)]
for l in range(I):
p = list(points)
for i, (x0,y0) in enumerate(p):
x1, y1 = p[(i+1)%N]
dx, dy = x1-x0, y1-y0
d = hypot(dx,dy)
points[i] =(x0 + dx/d , y0 + dy/d)
cr.set_source_rgba(0,random(),0,l/I)
cr.arc(x0,y0,sqrt(d),0,2*pi)
cr.stroke()
cr.set_source_rgba(0,0,0,1)
cr.arc(x0,y0,2,0,2*pi)
cr.stroke()
surface.write_to_png('Worms.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment