turtle_hilbert.py

Created by ferr0fluidmann

Created on April 07, 2019

686 Bytes

Uses the turtle library to draw a Hilbert space-filling curve. This is what I came up with after a little bit of searching on the internet. Run with hilbert_run() and modify that function to modify the output.


from turtle import *

def hilbert(step, rule, angle, depth):
  if depth > 0:
    a = lambda: hilbert(step, "a", angle, depth - 1)
    b = lambda: hilbert(step, "b", angle, depth - 1)
    if rule == "a":
      left(angle)
      b()
      forward(step)
      right(angle)
      a()
      forward(step)
      a()
      right(angle)
      forward(step)
      b()
      left(angle)
    if rule == "b":
      right(angle)
      a()
      forward(step)
      left(angle)
      b()
      forward(step)
      b()
      left(angle)
      forward(step)
      a()
      right(angle)

def hilbert_run():
  reset()
  penup()
  goto(-80,-80)
  pendown()
  hilbert(5, "a", 90, 5)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.