2018-10-19 07:58:21 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# encoding=utf8
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
"""Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95
|
2017-10-23 10:02:51 +00:00
|
|
|
|
|
|
|
Simple example of Fractal generation using recursive function.
|
|
|
|
|
|
|
|
What is Sierpinski Triangle?
|
|
|
|
>>The Sierpinski triangle (also with the original orthography Sierpinski), also called the Sierpinski gasket or the Sierpinski Sieve,
|
|
|
|
is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller
|
|
|
|
equilateral triangles. Originally constructed as a curve, this is one of the basic examples of self-similar sets, i.e.,
|
|
|
|
it is a mathematically generated pattern that can be reproducible at any magnification or reduction. It is named after
|
|
|
|
the Polish mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries prior to the work of Sierpinski.
|
|
|
|
|
|
|
|
Requirements(pip):
|
|
|
|
- turtle
|
|
|
|
|
|
|
|
Python:
|
|
|
|
- 2.6
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
- $python sierpinski_triangle.py <int:depth_for_fractal>
|
|
|
|
|
2019-05-08 19:48:30 +00:00
|
|
|
Credits: This code was written by editing the code from http://www.riannetrujillo.com/blog/python-fractal/
|
2017-10-23 10:02:51 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2017-10-23 10:02:51 +00:00
|
|
|
import turtle
|
|
|
|
import sys
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
PROGNAME = "Sierpinski Triangle"
|
2017-10-23 10:02:51 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
points = [[-175, -125], [0, 175], [175, -125]] # size of triangle
|
2017-10-23 10:02:51 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
def getMid(p1, p2):
|
|
|
|
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint
|
|
|
|
|
|
|
|
|
|
|
|
def triangle(points, depth):
|
2017-10-23 10:02:51 +00:00
|
|
|
|
|
|
|
myPen.up()
|
2019-10-05 05:14:13 +00:00
|
|
|
myPen.goto(points[0][0], points[0][1])
|
2017-10-23 10:02:51 +00:00
|
|
|
myPen.down()
|
2019-10-05 05:14:13 +00:00
|
|
|
myPen.goto(points[1][0], points[1][1])
|
|
|
|
myPen.goto(points[2][0], points[2][1])
|
|
|
|
myPen.goto(points[0][0], points[0][1])
|
|
|
|
|
|
|
|
if depth > 0:
|
|
|
|
triangle(
|
|
|
|
[points[0], getMid(points[0], points[1]), getMid(points[0], points[2])],
|
|
|
|
depth - 1,
|
|
|
|
)
|
|
|
|
triangle(
|
|
|
|
[points[1], getMid(points[0], points[1]), getMid(points[1], points[2])],
|
|
|
|
depth - 1,
|
|
|
|
)
|
|
|
|
triangle(
|
|
|
|
[points[2], getMid(points[2], points[1]), getMid(points[0], points[2])],
|
|
|
|
depth - 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
raise ValueError(
|
|
|
|
"right format for using this script: "
|
|
|
|
"$python fractals.py <int:depth_for_fractal>"
|
|
|
|
)
|
2019-07-08 15:27:51 +00:00
|
|
|
myPen = turtle.Turtle()
|
|
|
|
myPen.ht()
|
|
|
|
myPen.speed(5)
|
2019-10-05 05:14:13 +00:00
|
|
|
myPen.pencolor("red")
|
|
|
|
triangle(points, int(sys.argv[1]))
|