Wednesday, 18 July 2012

Sierpinski's Fractal

     Sierpinski's triangle is a fractal or an attractive fixed set named after the Polish Mathematician Waclaw Sierpinski. A mathematically generated pattern which can be reproduced to any order.

 How to start?

  • start with an equilateral triangle with base parallel to the horizontal axis.
  • Shrink the triangle to 1/2 height and 1/2 width, make three copies, and position the three shrunken triangles so that each triangle touches the two other triangles at a corner   
  • repeat the earlier step with the smaller triangles.





I created these perfect Sierpinski triangles in two approaches. One way is to find the center coordinates of the screen, then finding equidistant points which form a equilateral. Another way is to by providing any 3 equidistant points. Either way we find the midpoints of the lines joining the 3 points. Then we recursively call the function which requires the parameters - the midpoints and the depth till which you need to create the fractals. The code fragment is as follows:-



def sierp(screen,color,a,b,c,deep):

        if(deep <= 0):
                tri(screen,color,a,b,c)
        else:
                ab = midpoint(a,b)
                ac = midpoint(a,c)
                bc = midpoint(b,c)

                sierp(screen,color,a,ab,ac,deep-1)
                sierp(screen,color,b,ab,bc,deep-1)
                sierp(screen,color,c,ac,bc,deep-1)


The ouput for the fractal for both methods are similar as shown in figures below.

using any 3 equidistant co-ordinates


using center co-ordinates

 The Sierpinski triangles are used in logic sets and gaming logics.
For complete code refer to my git repository - https://github.com/anjalirmenon/sierpinski

 

No comments:

Post a Comment