Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tkinter
  • Pygame
  • Matplotlib
  • Python Imaging Library
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Science With Python
  • Machine Learning with Python
  • Django
  • Flask
  • R
Open In App
Next Article:
Python - Hilbert Curve using turtle
Next article icon

Python - Hilbert Curve using turtle

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 that 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

  1. Rotate y degree towards the right
  2. Move step size
  3. Rotate y degree towards the left
  4. Move step size
  5. Rotate y degree towards the left
  6. Move step size
  7. 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:

  1. Rotate 90 degrees towards the right
  2. Create a hilbert curve at level 1 rotated by -y degrees (ie, y degrees in anticlockwise direction)
  3. Move step size
  4. Rotate y degrees towards the right
  5. Create a level 1 hilbert curve rotated by y degrees (ie, y degrees in clockwise direction)
  6. Rotate y degrees towards the left.
  7. Move step size
  8. Create a level 1 hilbert curve rotated by -y degrees
  9. 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 drawing will be made while moving.
  • done(): Used to specify that the turtle work is completed.

Hibert Curve:

It is also called a peano or space-filling curve. It requires successive approximation. In the first approximation the square is divided into 4 quadrants and draw the curve that connects the center points of each. In the second approximation further, every quadrant is divided which cannot be the center of each.

Hibert Curve
  • There is no limit to the subdivision. Ideally length of the curve is infinite. With every subdivision the length Increase by 4
  • The curve is equivalent to line. Its topological dimension is 1.
  • Length of the curve changes by 4.
N = sDf 4 = 2Df Df = 2   DT = 1    Df = 2 Df > DT                                    

So if a line but folded such that it looks like a 2D object.

  • Point sets, curves, and surfaces which has fractal dimension greater than the topological dimension are referred to as fractals.
  • The curve generation starts with a square. In a first approximation, the square is divided into 4 quadrants and the curve joins the center of all quadrants by a straight line.
  • .In the quadrants it is subdivided into 2*2 grids, ending in 16 squares. Curve visits the center of each small square in each quadrant before moving to the next quadrant
  • In 16 squares it is subdivided into 2 * 2 grids, ending in 64 squares. Curve visits the center of each small square before moving to the higher-level quadrant.
  • On applying this process continuously,
  • The curve never crosses itself
  • Curve gets closer to a square containing it
  • With each subdivision, the length of the curve increases four times.
  • There is no limit on depth and hence there is no limit on curve length.

Applications of Hibert Curve:

  • It is used for Modelling natural structures like Geographic terrain, mountain, plant structure, clouds, vegetables etc.
  • Space research.
  • Study of convergence of iterative processes.
  • Engineering and architecture.
  • Medical science.
  • Chemical processes.
  • Medical diagnostic images.
  • Fluid mechanics.
  • Image compression and different Telecommunication purposes.

Code:

Python3
# Code for Hilbert Curve  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() 

Next Article
Python - Hilbert Curve using turtle

K

kartikeysm2001
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2019
  • Python-turtle
Practice Tags :
  • python

Similar Reads

    Draw Circle in Python using Turtle
    Turtle graphics is an engaging way to learn Python programming by visualizing geometric shapes. The turtle module lets you control a turtle to draw lines and shapes on the screen, making it an ideal tool for beginners. Below, we'll explore how to draw circles and create more complex patterns like ta
    2 min read
    Draw Heart Using Turtle Graphics in Python
    Python's Turtle Graphics module provides a simple way to create drawings and shapes using a virtual pen (called a "turtle") that can move across the screen. In this tutorial, we will learn how to draw a heart shape using Turtle Graphics and customize it with colors and text. Before proceeding, you s
    2 min read
    Draw Ellipse Using Turtle in Python
    Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. Approach: The fo
    2 min read
    Create digital clock using Python-Turtle
    Turtle is a special feature of Python. Using Turtle, we can easily draw on a drawing board. First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle methods we can draw in the drawing board. Prerequisites: Turtle Programming in Python Installation
    3 min read
    Draw sun using Turtle module in Python
    Prerequisite: Turtle Programming in Python In this article, let's learn how to draw the Sun using turtle in Python. Turtle is an inbuilt module in Python. It helps draw patterns by providing a screen and turtle (pen) as tools. To move the turtle as desired functions defined within the module like fo
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences