diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 96e4bcff6..2298de7b5 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -42,10 +42,11 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Christopher Milan - Vexatos - Raven-Blue Dragon -- Björn Heinrichs +- Björn Heinrichs - Olav Sundfør - Ben Chislett - dovisutu - Antetokounpo - Akash Dhiman - Vincent Zalzal +- Jonathan D B Van Schenck diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 69e8f41ac..7cb36047b 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -130,6 +130,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:4-17, lang:"julia"](code/julia/IFS.jl) {% sample lang="cpp" %} [import:39-52, lang:"cpp"](code/c++/IFS.cpp) +{% sample lang="py" %} +[import:5-12, lang:"python"](code/python/IFS.py) {% endmethod %} If we set the initial points to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below: @@ -195,6 +197,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"julia"](code/julia/IFS.jl) {% sample lang="cpp" %} [import, lang:"cpp"](code/c++/IFS.cpp) +{% sample lang="py" %} +[import, lang:"python"](code/python/IFS.py) {% endmethod %} ### Bibliography diff --git a/contents/IFS/code/python/IFS.py b/contents/IFS/code/python/IFS.py new file mode 100644 index 000000000..a7808fb5c --- /dev/null +++ b/contents/IFS/code/python/IFS.py @@ -0,0 +1,25 @@ +from random import random, choice +from math import sqrt + +# This generator simulates a "chaos game" +def chaos_game(n, shape_points): + # Initialize the starting point + point = [random(), random()] + + for _ in range(n): + # Update the point position and yield the result + point = [(p + s) / 2 for p, s in zip(point, choice(shape_points))] + yield point + +# This will generate a Sierpinski triangle with a chaos game of n points for an +# initial triangle with three points on the vertices of an equilateral triangle: +# A = (0.0, 0.0) +# B = (0.5, sqrt(0.75)) +# C = (1.0, 0.0) +# It will output the file sierpinski.dat, which can be plotted after +shape_points = [[0.0, 0.0], + [0.5, sqrt(0.75)], + [1.0, 0.0]] +with open("sierpinski.dat", "w") as f: + for point in chaos_game(10000, shape_points): + f.write("{0}\t{1}\n".format(*point))