-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Add IFS in python #700
Changes from 4 commits
c15b2a9
98488d8
4cdf194
ae230f7
7573977
c7e84e7
6845f97
62a6174
428683e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
from random import random, choice | ||||||
from math import sqrt, sin, cos, pi | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove your unused imports? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! yup, my bad |
||||||
|
||||||
# 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))] | ||||||
jonathanvanschenck marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the underscores here, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change them |
||||||
yield point | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) |
||||||
|
||||||
# This function generates the shape points for an N-gon inscribed inside a | ||||||
# unit circle, with one of the edges oriented along the bottom | ||||||
def ngon_shape_points(N): | ||||||
jonathanvanschenck marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# Exterior N-gon angle | ||||||
phi = 2 * pi / N | ||||||
|
||||||
# Offset to put an edge at the bottom | ||||||
delta = (pi + phi) / 2 | ||||||
|
||||||
return [[cos(- delta - m * phi), sin(- delta - m * phi)] for m in range(N)] | ||||||
|
||||||
# This will generate a Sierpinski triangle with a chaos game of n points for an | ||||||
# initial equilateral triangle inscribed on a unit circle. The shape points are: | ||||||
# A = (-sqrt(3)/2, -0.5) | ||||||
# B = ( 0.0, 1.0) | ||||||
# C = ( sqrt(3)/2, -0.5) | ||||||
# It will output the file sierpinski.dat, which can be plotted after | ||||||
shape_points = ngon_shape_points(3) | ||||||
with open("sierpinski.dat", "w") as f: | ||||||
for point in chaos_game(10000, shape_points): | ||||||
f.write("{0},{1}\n".format(*point)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with the Julia implementation, the plot output will be easier to read if it's tab-separated:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay!