-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Refactor sierpinski_triangle.py
#8068
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
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f7ba9da
updating DIRECTORY.md
11aaefd
Update sierpinski_triangle.py header doc
tianyizheng02 71ec4d2
Remove unused PROGNAME var in sierpinski_triangle.py
tianyizheng02 025f03d
Refactor triangle() function to not use list of vertices
tianyizheng02 c3cb44e
Refactor sierpinski_triangle.py to use tuples
tianyizheng02 7547edf
Flip if-statement condition in sierpinski_triangle.py to avoid nesting
tianyizheng02 c27cc76
Add type hints to sierpinski_triangle.py
tianyizheng02 6546a8b
Add doctests to sierpinski_triangle.py
tianyizheng02 e763017
Fix return types in doctests
tianyizheng02 b8c89d5
Update fractals/sierpinski_triangle.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,85 @@ | ||
#!/usr/bin/python | ||
|
||
"""Author Anurag Kumar | [email protected] | git/anuragkumarak95 | ||
|
||
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. | ||
""" | ||
Author Anurag Kumar | [email protected] | git/anuragkumarak95 | ||
|
||
Requirements(pip): | ||
- turtle | ||
Simple example of fractal generation using recursion. | ||
|
||
Python: | ||
- 2.6 | ||
What is the Sierpiński Triangle? | ||
The Sierpiński triangle (sometimes spelled Sierpinski), also called the | ||
Sierpiński gasket or Sierpiński sieve, is a fractal 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—that is, it is a mathematically | ||
generated pattern that is reproducible at any magnification or reduction. It is | ||
named after the Polish mathematician Wacław Sierpiński, but appeared as a | ||
decorative pattern many centuries before the work of Sierpiński. | ||
|
||
Usage: | ||
- $python sierpinski_triangle.py <int:depth_for_fractal> | ||
Requirements (pip): turtle | ||
|
||
Credits: This code was written by editing the code from | ||
https://www.riannetrujillo.com/blog/python-fractal/ | ||
Usage: python sierpinski_triangle.py <int:depth_for_fractal> | ||
|
||
Credits: | ||
The above description is taken from | ||
https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle | ||
This code was written by editing the code from | ||
https://www.riannetrujillo.com/blog/python-fractal/ | ||
""" | ||
import sys | ||
import turtle | ||
|
||
PROGNAME = "Sierpinski Triangle" | ||
|
||
points = [[-175, -125], [0, 175], [175, -125]] # size of triangle | ||
|
||
|
||
def get_mid(p1, p2): | ||
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint | ||
|
||
|
||
def triangle(points, depth): | ||
|
||
def get_mid(p1: tuple[float, float], p2: tuple[float, float]) -> tuple[float, float]: | ||
""" | ||
Find the midpoint of two points | ||
|
||
>>> get_mid((0, 0), (2, 2)) | ||
(1.0, 1.0) | ||
>>> get_mid((-3, -3), (3, 3)) | ||
(0.0, 0.0) | ||
>>> get_mid((1, 0), (3, 2)) | ||
(2.0, 1.0) | ||
>>> get_mid((0, 0), (1, 1)) | ||
(0.5, 0.5) | ||
>>> get_mid((0, 0), (0, 0)) | ||
(0.0, 0.0) | ||
""" | ||
return (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2 | ||
|
||
|
||
def triangle( | ||
vertex1: tuple[float, float], | ||
vertex2: tuple[float, float], | ||
vertex3: tuple[float, float], | ||
depth: int, | ||
) -> None: | ||
""" | ||
Recursively draw the Sierpinski triangle given the vertices of the triangle | ||
and the recursion depth | ||
""" | ||
my_pen.up() | ||
my_pen.goto(points[0][0], points[0][1]) | ||
my_pen.goto(vertex1[0], vertex1[1]) | ||
my_pen.down() | ||
my_pen.goto(points[1][0], points[1][1]) | ||
my_pen.goto(points[2][0], points[2][1]) | ||
my_pen.goto(points[0][0], points[0][1]) | ||
my_pen.goto(vertex2[0], vertex2[1]) | ||
my_pen.goto(vertex3[0], vertex3[1]) | ||
my_pen.goto(vertex1[0], vertex1[1]) | ||
|
||
if depth > 0: | ||
triangle( | ||
[points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], | ||
depth - 1, | ||
) | ||
triangle( | ||
[points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], | ||
depth - 1, | ||
) | ||
triangle( | ||
[points[2], get_mid(points[2], points[1]), get_mid(points[0], points[2])], | ||
depth - 1, | ||
) | ||
if depth == 0: | ||
return | ||
|
||
triangle(vertex1, get_mid(vertex1, vertex2), get_mid(vertex1, vertex3), depth - 1) | ||
triangle(vertex2, get_mid(vertex1, vertex2), get_mid(vertex2, vertex3), depth - 1) | ||
triangle(vertex3, get_mid(vertex3, vertex2), get_mid(vertex1, vertex3), 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>" | ||
"Correct format for using this script: " | ||
"python fractals.py <int:depth_for_fractal>" | ||
) | ||
my_pen = turtle.Turtle() | ||
my_pen.ht() | ||
my_pen.speed(5) | ||
my_pen.pencolor("red") | ||
triangle(points, int(sys.argv[1])) | ||
|
||
vertices = [(-175, -125), (0, 175), (175, -125)] # vertices of triangle | ||
triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1])) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.