Created
April 10, 2013 10:17
-
-
Save jeffreyiacono/5353437 to your computer and use it in GitHub Desktop.
draw a tree with python (via: http://preshing.com/20110920/the-python-with-statement-by-example)
This file contains hidden or 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
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