Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created April 10, 2013 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffreyiacono/5353437 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/5353437 to your computer and use it in GitHub Desktop.
import cairo
from contextlib import contextmanager
@contextmanager
def saved(cr):
cr.save()
try:
yield cr
finally:
cr.restore()
def Tree(angle):
cr.move_to(0, 0)
cr.translate(0, -65)
cr.line_to(0, 0)
cr.stroke()
cr.scale(0.72, 0.72)
if angle > 0.12:
for a in [-angle, angle]:
with saved(cr):
cr.rotate(a)
Tree(angle * 0.75)
width = 280
height = 204
surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surf)
cr.set_source_rgb(1, 1, 1)
cr.rectangle(0, 0, width, height)
cr.fill()
cr.set_source_rgb(0, 0, 0)
cr.translate(140, 203)
cr.set_line_width(5)
Tree(0.75)
surf.write_to_png('fractal-tree.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment