Star fractal printing using Turtle in Python

Prerequisite: Turtle Programming Basics

Fractals are objects that tend to have self-similar structures repeated a finite number of times. The objective of this article is to draw a star fractal where a star structure is drawn on each corner of the star and this process is repeated until the input size reduces to a value of 10. For achieving this star fractal pattern the turtle module is used.

Methods used 

  • forward(value): It moves the turtle in forward direction.
  • speed(value): It changes the speed of the turtle.
  • penup(): Stop drawing.
  • pendown(): Start drawing.
  • left(value): It moves the turtle towards the left.

Approach

  1. Import turtle.
  2. Initialise the turtle.
  3. Change the background color.
  4. Make a function to draw a star.
  5. Call the above function recursively inside the for loop to make the entire start pattern.

Below is the implementation of the above approach.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import turtle
import turtle
  
# initialise turtle instance
stars = turtle.Turtle()
  
# increases the speed of turtle
stars.speed(10)
  
# to change the background color
stars.getscreen().bgcolor("black")
stars.color("red")
  
# stop drawing
stars.penup()
  
# move the turtle
stars.goto((-200, 50))
  
# start drawing
stars.pendown()
  
# function to draw stars
def star(turtle, size):
    if size <= 10:
        return
    else:
        for i in range(5):
            
            # moving turtle forward
            turtle.forward(size)
            star(turtle, size/3)
  
            # moving turtle left
            turtle.left(216)
  
  
# calling the star function
star(stars, 360)
turtle.done()

chevron_right


Output:




Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.