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
- Import turtle.
- Initialise the turtle.
- Change the background color.
- Make a function to draw a star.
- Call the above function recursively inside the for loop to make the entire start pattern.
Below is the implementation of the above approach.
Python3
import turtle
stars = turtle.Turtle()
stars.speed( 10 )
stars.getscreen().bgcolor( "black" )
stars.color( "red" )
stars.penup()
stars.goto(( - 200 , 50 ))
stars.pendown()
def star(turtle, size):
if size < = 10 :
return
else :
for i in range ( 5 ):
turtle.forward(size)
star(turtle, size / 3 )
turtle.left( 216 )
star(stars, 360 )
turtle.done()
|
Output:
Last Updated :
01 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...