1
+ '''Author Anurag Kumar | [email protected] | git/anuragkumarak95
2
+
3
+ Simple example of Fractal generation using recursive function.
4
+
5
+ What is Sierpinski Triangle?
6
+ >>The Sierpinski triangle (also with the original orthography Sierpinski), also called the Sierpinski gasket or the Sierpinski Sieve,
7
+ is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller
8
+ equilateral triangles. Originally constructed as a curve, this is one of the basic examples of self-similar sets, i.e.,
9
+ it is a mathematically generated pattern that can be reproducible at any magnification or reduction. It is named after
10
+ the Polish mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries prior to the work of Sierpinski.
11
+
12
+ Requirements(pip):
13
+ - turtle
14
+
15
+ Python:
16
+ - 2.6
17
+
18
+ Usage:
19
+ - $python sierpinski_triangle.py <int:depth_for_fractal>
20
+
21
+ Credits: This code was written by editing the code from http://www.lpb-riannetrujillo.com/blog/python-fractal/
22
+
23
+ '''
24
+ import turtle
25
+ import sys
26
+ PROGNAME = 'Sierpinski Triangle'
27
+ if len (sys .argv ) != 2 :
28
+ raise Exception ('right format for using this script: $python fractals.py <int:depth_for_fractal>' )
29
+
30
+ myPen = turtle .Turtle ()
31
+ myPen .ht ()
32
+ myPen .speed (5 )
33
+ myPen .pencolor ('red' )
34
+
35
+ points = [[- 175 ,- 125 ],[0 ,175 ],[175 ,- 125 ]] #size of triangle
36
+
37
+ def getMid (p1 ,p2 ):
38
+ return ( (p1 [0 ]+ p2 [0 ]) / 2 , (p1 [1 ] + p2 [1 ]) / 2 ) #find midpoint
39
+
40
+ def triangle (points ,depth ):
41
+
42
+ myPen .up ()
43
+ myPen .goto (points [0 ][0 ],points [0 ][1 ])
44
+ myPen .down ()
45
+ myPen .goto (points [1 ][0 ],points [1 ][1 ])
46
+ myPen .goto (points [2 ][0 ],points [2 ][1 ])
47
+ myPen .goto (points [0 ][0 ],points [0 ][1 ])
48
+
49
+ if depth > 0 :
50
+ triangle ([points [0 ],
51
+ getMid (points [0 ], points [1 ]),
52
+ getMid (points [0 ], points [2 ])],
53
+ depth - 1 )
54
+ triangle ([points [1 ],
55
+ getMid (points [0 ], points [1 ]),
56
+ getMid (points [1 ], points [2 ])],
57
+ depth - 1 )
58
+ triangle ([points [2 ],
59
+ getMid (points [2 ], points [1 ]),
60
+ getMid (points [0 ], points [2 ])],
61
+ depth - 1 )
62
+
63
+
64
+ triangle (points ,int (sys .argv [1 ]))
0 commit comments