Fractals: The Mathematics of God

This post is an archive of my old blog, which I no longer maintain!


Wondering, why fractals are mentioned as the mathematics of God, you might know from the fractalist Benoit Mandelbrot: the mind who introduced the fractal geometry to the world.

Fractals and The art of roughness

Basically, fractals are self-repeating patterns, with or without some randomness. If you ever lost in a forest, you know what does the phrase “self-repeating” sounds like! Fractals are everywhere, from microscopic DNA to the Galaxies and even universe. Leaves, trees, forests, mountains, lightening and many other natural objects follows fractal geometry.

Today, Very realistic computer graphics are being designed using fractals and credit goes to “the art of roughness”. Without knowing what roughness means in geometrical term, we were unable to design such realistic graphics. The fractional Hausdroff dimension of a fractal determines the roughness of the curve. Fractals are surely a major turning factor in computer graphic’s world.

I tried implementing some basic fractals in Turtle graphics. I will explain how to program a simple tree in python using Turtle module, before that let’s have a look at the fractals I implemented: tree, bush, snowflakesierpinski triangle and dragon curve. 🙂

Before moving forward check off the following checklist:

  1. Visit The previous blog post: The beauty of Mathematics
  2. Know how to code in Python 3.
  3. Know how to use Turtle Graphics in Python.

Now you are ready to look at the code 😉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import dependencies
import turtle
from random import randint
from math import sin,cos,degrees,radians
  
  
#intial setup
scr=turtle.Screen() #Screen Intialization, opens up a window
scr.clearscreen() #clears the screen
scr.tracer(0,0) #makes the turtle works faster (doesn't animate every step)
tobj=turtle.Turtle() #intializes a turtle on the screen opened
tobj.hideturtle() #hides the turtle pointer
  
  
  
#important functions
def gotoxy(t:turtle.Turtle,x,y):
    '''moves the turtle to (x,y) position without drawing anything'''
    t.penup()
    t.setposition(x,y)
    t.pendown()
 
 
def Tree(t:turtle.Turtle,len,w):
    '''draws a random fractal Tree.'''
    #base condition
    if len<5:
        # setting the colour of leaves
        t.color('#57c165'
        t.width(randint(2,5))
        t.fd(len)
        return
    #recursion
    else:
        t.width(w)
        # setting the colour of branch
        t.color('#7c5e20')
        t.fd(len)                # draws the current branch
        x,y=t.pos()              # remembering the fork position
        t.lt(randint(20,40))     # left branch with random angle
        angle=t.heading()        # remembering the left branch angle
        Tree(t,len*randint(73,75)/100,w*.35)     # drawing left subtree with lenght diminishing factor between 73 to 75 percent
        gotoxy(tobj,x,y)         # return to the fork position
        t.setheading(angle)      # recover to the left branch angle
        t.rt(randint(20,80))     # right branch with random angle
        Tree(t,len*randint(73,75)/100,w*.35)     # drawing the right subtree
 
#running the code
tobj.seth(90)       #sets the turtle head upwards
gotoxy(tobj,0,-300) #sets the current position of turtle
Tree(tobj,150,10)
scr.update()        #updates the screen
scr.exitonclick()   #creates an event handler to close the window on click

As you can see in the code, each branch of the tree is a smaller version of tree, that’s why recursion is playing a crucial role. Other important point is we are incorporating some randomness while choosing angle for the branches and the length diminishing factor and that’s because without randomness it won’t look this awesome. You can try removing the randomness and see how boring it looks like! 😛

I hope you enjoyed the tour. Start experimenting with fractals to design interesting patterns. You may also find some good tools to design fractals ( Such as inkscape has very nice extension for designing fractals using L-Systems )

Hopefully, I will be uploading some more interesting posts on Fractals. Thanks for your patience! Leave your experience or queries below in the comment box. Singing out 🙂