Open In App

Snowflakes Fractal using Python

Improve
Improve
Like Article
Like
Save
Share
Report

To create Snowflake fractals using Python programming
What are fractals
 

A fractal is a never-ending pattern. Fractals are infinitely complex patterns that are self-similar across different scales. They are created by repeating a simple process over and over in an ongoing feedback loop. Driven by recursion, fractals are images of dynamic systems – the pictures of Chaos.

What is turtle programming in python?? 
 

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.
By combining together these and similar commands, intricate shapes and pictures can easily be drawn.
The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5.
It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n switch.
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

 

This Python Code allow you to create Snowflakes design by using its standard library Turtle for GUI designing. This code creates 20 (you can change it in the source code) snowflakes randomly of random size and color in random position of the screen. 
 

 

 

Python




# Python code to draw snowflakes fractal.
import turtle
import random
 
# setup the window with a background color
wn = turtle.Screen()
wn.bgcolor("cyan")
 
# assign a name to your turtle
elsa = turtle.Turtle()
elsa.speed(15)
 
# create a list of colours
sfcolor = ["white", "blue", "purple", "grey", "magenta"]
 
# create a function to create different size snowflakes
def snowflake(size):
 
    # move the pen into starting position
    elsa.penup()
    elsa.forward(10*size)
    elsa.left(45)
    elsa.pendown()
    elsa.color(random.choice(sfcolor))
 
    # draw branch 8 times to make a snowflake
    for i in range(8):
        branch(size)  
        elsa.left(45)
     
 
# create one branch of the snowflake
def branch(size):
    for i in range(3):
        for i in range(3):
            elsa.forward(10.0*size/3)
            elsa.backward(10.0*size/3)
            elsa.right(45)
        elsa.left(90)
        elsa.backward(10.0*size/3)
        elsa.left(45)
    elsa.right(90)
    elsa.forward(10.0*size)
 
# loop to create 20 different sized snowflakes
# with different starting co-ordinates
for i in range(20):
    x = random.randint(-200, 200)
    y = random.randint(-200, 200)
    sf_size = random.randint(1, 4)
    elsa.penup()
    elsa.goto(x, y)
    elsa.pendown()
    snowflake(sf_size)
 
# leave the window open until you click to close 
wn.exitonclick() 


Output: 
 

 



Last Updated : 23 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads