Python - Hilbert Curve using turtle - GeeksforGeeks

27 Feb, 2020
Fractal is a curve or a figure which repeats itself. It comprises a recursive pattern that repeats itself up to a desired level of nesting. Turtle graphics are provided in the turtle module which is used for drawing various shapes and patterns in Python.
A Hilbert curve is a curve which is formed by connecting a sequence of U-shaped curves arranged and oriented in different directions. These U-shaped curves are placed at a certain step size distance apart.
Let us examine a Level-1 Hilbert Curve. The following steps will draw a simple U curve.
Let y = 90 degree
Rotate y degree towards the rightMove step sizeRotate y degree towards the leftMove step sizeRotate y degree towards the leftMove step sizeRotate y degree towards the right
Rotate y degree towards the right
Move step size
Rotate y degree towards the left
Move step size
Rotate y degree towards the left
Move step size
Rotate y degree towards the right
Let us examine and try to understand the level-2 Hilbert Curve.
Again, we assume that the turtle pointer points towards right initially. The following steps may be used to draw the curve:
Rotate 90 degrees towards the rightCreate a hilbert curve at level 1 rotated by -y degrees (ie, y degrees in anticlockwise direction)Move step sizeRotate y degrees towards the rightCreate a level 1 hilbert curve rotated by y degrees (ie, y degrees in clockwise direction)Rotate y degrees towards the left.Move step sizeCreate a level 1 hilbert curve rotated by -y degreesRotate y degrees towards the right
Rotate 90 degrees towards the right
Create a hilbert curve at level 1 rotated by -y degrees (ie, y degrees in anticlockwise direction)
Move step size
Rotate y degrees towards the right
Create a level 1 hilbert curve rotated by y degrees (ie, y degrees in clockwise direction)
Rotate y degrees towards the left.
Move step size
Create a level 1 hilbert curve rotated by -y degrees
Rotate y degrees towards the right
turtle methods used in this section are as follows :
forward(): Used for moving the turtle forward by a given distance in the direction of the turtle.
backward(): Used for moving the turtle backward by a given distance in the direction of the turtle.
left(): Used for rotating the turtle in the left direction by a specified angle.
right(): Used for rotating the turtle in the right direction by a specified angle.
goto(): Used for moving the turtle to the location specified ((x, y) coordinates).
penup(): Used for specifying that no drawing will be made while moving.
pendown(): Used for specifying that that drawing will be made while moving.
done(): Used to specify that the turtle work is completed.
from turtle import *   def hilbert(level, angle, step):      # Input Parameters are numeric    # Return Value: None    if level == 0:        return      right(angle)    hilbert(level-1, -angle, step)      forward(step)    left(angle)    hilbert(level-1, angle, step)      forward(step)    hilbert(level-1, angle, step)      left(angle)    forward(step)    hilbert(level-1, -angle, step)    right(angle)  def main():    level = int(input())    size = 200    penup()    goto(-size / 2.0, size / 2.0)    pendown()         # For positioning turtle    hilbert(level, 90, size/(2**level-1))           done()  if __name__=='__main__':    main()
Python-turtle
Technical Scripter 2019
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python