Snowflakes Fractal using Python
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
import turtle
import random
wn = turtle.Screen()
wn.bgcolor( "cyan" )
elsa = turtle.Turtle()
elsa.speed( 15 )
sfcolor = [ "white" , "blue" , "purple" , "grey" , "magenta" ]
def snowflake(size):
elsa.penup()
elsa.forward( 10 * size)
elsa.left( 45 )
elsa.pendown()
elsa.color(random.choice(sfcolor))
for i in range ( 8 ):
branch(size)
elsa.left( 45 )
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)
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)
wn.exitonclick()
|
Output:
Last Updated :
23 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...