Skip to content

Add IFS in python #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would fix the range so that the imports in the source code file aren't included.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay!

[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:
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions contents/IFS/code/python/IFS.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)


# 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))