Skip to content

Instantly share code, notes, and snippets.

@typemytype
Last active January 18, 2016 22:10
Show Gist options
  • Select an option

  • Save typemytype/738aebeedeea653ae0e3 to your computer and use it in GitHub Desktop.

Select an option

Save typemytype/738aebeedeea653ae0e3 to your computer and use it in GitHub Desktop.
Fractals are cool!
# create a reusable bezierPath with a triangle
triangle = BezierPath()
triangle.moveTo((0, 0))
triangle.lineTo((width(), 0))
triangle.lineTo((width()/2, height()))
triangle.closePath()
# set the maximum recusions
maxSteps = 7
# recursive drawing
def recursiveDrawTriangle(steps):
# draw the initial triangle
drawPath(triangle)
# check if it ran out of steps
if steps < 0:
return
# save the whole thing
save()
# scale it
scale(.5)
# start a new recursive drawing
recursiveDrawTriangle(steps-1)
# translate to the right
translate(width(), 0)
# start a new recursive drawing
recursiveDrawTriangle(steps-1)
# translate back and up
translate(-width()*.5, height())
# start a new recursive drawing
recursiveDrawTriangle(steps-1)
# restore it back
restore()
# some drawing stuff
stroke(0)
fill(None)
# go
recursiveDrawTriangle(maxSteps)
@thomgb

thomgb commented Jan 18, 2016

Copy link
Copy Markdown

Sweet!
But what!, you call a function within itself... Okay, new things every day eh :)
Thanks Frederik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment