Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

ASSIGNMENT:Python Program For

Fractals

DELPHINE PRICILLA.S
M.sc(Information Technology)
STELLA MARIS COLLEGE

ABSTRACT:
Its impressing how easy it is to draw fractals with
Python. Using the ubercool Python Imaging Library and
native complex number support in Python you can code an
elaborate and easy to understand fractals in python. Fractals
are awesome, they are built with very complex pattern and
they allow you to zoom in forever! we will see how easily it
is to plot several kinds of fractals using a tool called L-
Systems and the Python Turtle module for the step to step
plotting. I'm not going to dive into too many technical details
but instead I'll present a little introduction, a lot of animated
examples and at the end, the code to generate your own.
Additionally, there will be some resources for both code and
math background if you want to explore at the end.
INTRODUCTION:
Lets give a "non-strict" definition of
what a fractal is, it is basically a geometric figure which
shows the same characteristics no matter how much you
zoom in.It isn't quite correct but for those who wanted to
know the exact definition here it is (A fractal is an object or
quantity that displays self-similarity, in a somewhat technical
sense, on all scales. The object need not exhibit exactly the
same structure at all scales, but the same "type" of
structures must appear on all scales. A plot of the quantity
on a log-log graph versus scale then gives a straight line,
whose slope is said to be the fractal dimension) Fractals are
typically hard to draw, because there is a concept which is
deeply tight in them, recursion. When we talk about graphics
and plotting we usually talk about pixels or vectors, but there
is always a limit, fractals by definition are infinitely recursive.
So when we want to plot one we should stop at some point,
that's why we talk about "iterations". At each iteration the
fractal becomes more and more complex, but at some point
it is impossible to distinguish between to successive
iterations (this happens when changes occur at individual
pixel levels), so it is quite reasonable to stop there,
sometimes it is quite clear what the shape is and we can
stop even earlier.

FRACTALS WITH PYTHON


TURTLE MODULE: According to Python docs: "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."
The key here is that turtle recognizes basically 3 commands:
 Move Forwards
 Turn Left by angle
 Turn Right by angle
Additionally we have the option to:
 Disable writing
 Enable writing
This characteristics seems too simple for plotting such
complex graphics as fractals in python.

L-SYSTEM: An L-System is a way of representing recursive


structures (such as fractals) as a string of characters , this is
done by rewriting the string over and over. Again, the formal
definition is the following:
A Lindenmayer system, also known as an L-system, is a
string rewriting system that can be used to generate fractals
with dimension between 1 and 2.
Once we understand what an L-System is we can produce
recursive structures, but before we are able to do that we
need to understand what are the pieces we need. Every L-
System has:
 An alphabet: The set of symbols the L-System is going
to use.
 An axiom: The initial string for the generation.
 A set of production rules: These rules tells how each
symbol should be replaced in the following iteration.
we are going to use Turtle to plot and L-Systems to
represent what we want to plot we need to create a
relationship between them.
Since the only commands we have in Turtle are the
mentioned above we will assign each a symbol which will
represent the alphabet
 F: Move Forwards
 +: Turn Right
 -: Turn Left
In order to make this work, each fractal should also provide
an angle, which will be the angle the turtle will turn either
right or left, for simplicity reasons only one angle should be
provided and the L-System should be written taking that into
consideration.
The axiom and the production rules will depend on the
fractal only, but the fractal should be written in a way that
can be represented by these only three symbols. This
introduces a limitation, we will be able to produce only one-
line fractals, so some such as the Cantor Set won't be able
to be produced this way, this is only a simplification, since
we can introduce two other commands to move forwards
without writing and analogously for the backwards
movement, but to keep things simple we will keep that
simplification.

TYPES OF FRACTALS USING PYTHON


There are three types of fractals in python. They are:
 Koch snowflakes

 Sierpinski triangle

 Julia fractals

KOCH SNOWFLAKES:
The Koch Snowflake is a type of fractal.
drawn with increasing levels of complexity. The level of The
Koch fractal, also called the Koch snowflake, can be
complexity is called the order of the Koch fractal. A fractal is
pattern that produces a picture which contains an infinite
amount of copies of itself. It can be viewed as a drawing with
a self-similar structure that is defined in terms of itself.

Here is an iterative way of drawing the Koch Snowflake:

1. Starting with a line segment, replace it with


four new segments, each 1/3 the length of the
original. The first and last segment aligns with
the original line and the second and third
segment form a unilateral triangle with the
missing line. The result is shown below:

Repeat this process on all line segments of the


resulting image.

Repeat step 2 – the step replacing all line segments by


four new segments – for as long as desired.

SIERPINSKI TRIANGLE:
Another famous fractal called the
Sierpinski Triangle named after Polish mathematician
Waclaw Sierpinski. He seemed to have played around with it
the most. To state it simple, you start with an equilateral
triangle and then form smaller triangles by connecting the
midpoints of each of the sides. Some folks shade alternate
triangles to pretty up the pattern. We are using the canvas of
the Python Image Library (PIL) to draw the triangle and save
the final image to a file.Python has a module called turtle that
allows us to draw cool shapes very easily. A fun application
of recursion is in generating fractals. Fractals are shapes or
images that are generated by repeated application of an
equation or a set of rules on said shape or image. The
fascinating thing about fractals is their abundance in nature
and the wide variety of remarkable images and patterns one
could generate with only a simple set of rules. In this
assignment, you will be drawing two-dimensional fractals.

3.JULIA FRACTRALS:
Julia sets are closely related to the Mandelbrot Set, which
we explored last issue with some basic Python. All of these
are produced by the code we’ll develop here from the same
simple iterated function z2+c as the Mandelbrot fractals. You
can see that they are different to the Mandelbrot set, and yet
there’s something about them that is similar. You can also
see that some Julia sets are a single object like the
Mandelbrot, but some are many pieces, with some even
becoming so fragmented they are almost like dust. It might
not be obvious, but while there is only one Mandelbrot, there
are infinitely many Julia sets. The recipe, or algorithm, for
creating Julia sets is the same as that for the Mandelbrot set
except for one key difference.
CONCLUSION:
Fractals are all around us.
Mathematicians have developed and are continuing to
develop equations to generate these fractals. This project
detailed the Lindenmayer grammar system and discussed
how it is used to produce a certain type of fractal. Several
examples were given, and further mathematical properties
of some of them were explored, including the idea of
fractal dimension. There are many other types of fractals
that are produced using different methods, the most
notable being the Mandelbrot Set, the Julia Set and Koch
snowflakes fractals. Lindenmayer set out to model the
growth pattern of algae. His Lindenmayer systems are the
height of simplicity.

REFERENCE:
 Cameron, J. S., "Artificial Life - Aristid
Lindenmayer". http://www.alesdar.org/oldSite/IS/chap4-
1.html.
 Clayton, K., "Fractals & the Fractal
Dimension". http://www.vanderbilt.edu/AnS/psychology/c
ogsci/chaos/workshop/Fractals.html.
 Devaney, R. L., "Chaos in the Classroom - Fractal
Dimension". http://math.bu.edu/DYSYS/chaos-
game/node6.html.
 Frame, M. and Mandelbrot, B., "Box-Counting
Dimension of the Cantor Set". http://classes.yale.edu/99-
00/math190a/CantorBoxDim.html.
 Lewis-Bowen, J., "Lindenmayer
Systems". http://www.lewisbowen.uklinux.net/program/ls
ystem.
 Ochoa, G., "An Introduction to Lindenmayer
Systems". http://www.biologie.uni-hamburg.de/b-
online/e28_3/lsys.html
 Riddle, L., "Sierpinski
Gasket". http://ecademy.agnesscott.edu/~lriddle/ifs/siertr
i/siertri.htm.
 Schnier, T., "Lindenmayer Systems - Basics: Nature
Inspired
Design". http://www.cs.bham.ac.uk/~xin/courses/design/
11-l-systems-1-4up.pdf.
 Weisstein, E. W., "Koch Snowflake -- from
Mathworld". http://mathworld.wolfram.com/KochSnowflak
e.html.
 Weisstein, E. W., "Sierpinski Sieve -- from
Mathworld". http://mathworld.wolfram.com/SierpinskiSiev
e.html.

You might also like