Skip to content

Commit 9f041e9

Browse files
tianyizheng02github-actionscclauss
authored
Refactor sierpinski_triangle.py (TheAlgorithms#8068)
* updating DIRECTORY.md * Update sierpinski_triangle.py header doc * Remove unused PROGNAME var in sierpinski_triangle.py The PROGNAME var was used to print an image description in the reference code that this implementation was taken from, but it's entirely unused here * Refactor triangle() function to not use list of vertices Since the number of vertices is always fixed at 3, there's no need to pass in the vertices as a list, and it's clearer to give the vertices distinct names rather than index them from the list * Refactor sierpinski_triangle.py to use tuples Tuples make more sense than lists for storing coordinate pairs * Flip if-statement condition in sierpinski_triangle.py to avoid nesting * Add type hints to sierpinski_triangle.py * Add doctests to sierpinski_triangle.py * Fix return types in doctests * Update fractals/sierpinski_triangle.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 725731c commit 9f041e9

File tree

1 file changed

+61
-53
lines changed

1 file changed

+61
-53
lines changed

fractals/sierpinski_triangle.py

+61-53
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,84 @@
1-
#!/usr/bin/python
2-
3-
"""Author Anurag Kumar | [email protected] | git/anuragkumarak95
4-
5-
Simple example of Fractal generation using recursive function.
6-
7-
What is Sierpinski Triangle?
8-
>>The Sierpinski triangle (also with the original orthography Sierpinski), also called
9-
the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set
10-
with the overall shape of an equilateral triangle, subdivided recursively into smaller
11-
equilateral triangles. Originally constructed as a curve, this is one of the basic
12-
examples of self-similar sets, i.e., it is a mathematically generated pattern that can
13-
be reproducible at any magnification or reduction. It is named after the Polish
14-
mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries
15-
prior to the work of Sierpinski.
1+
"""
2+
Author Anurag Kumar | [email protected] | git/anuragkumarak95
163
17-
Requirements(pip):
18-
- turtle
4+
Simple example of fractal generation using recursion.
195
20-
Python:
21-
- 2.6
6+
What is the Sierpiński Triangle?
7+
The Sierpiński triangle (sometimes spelled Sierpinski), also called the
8+
Sierpiński gasket or Sierpiński sieve, is a fractal attractive fixed set with
9+
the overall shape of an equilateral triangle, subdivided recursively into
10+
smaller equilateral triangles. Originally constructed as a curve, this is one of
11+
the basic examples of self-similar sets—that is, it is a mathematically
12+
generated pattern that is reproducible at any magnification or reduction. It is
13+
named after the Polish mathematician Wacław Sierpiński, but appeared as a
14+
decorative pattern many centuries before the work of Sierpiński.
2215
23-
Usage:
24-
- $python sierpinski_triangle.py <int:depth_for_fractal>
2516
26-
Credits: This code was written by editing the code from
27-
https://www.riannetrujillo.com/blog/python-fractal/
17+
Usage: python sierpinski_triangle.py <int:depth_for_fractal>
2818
19+
Credits:
20+
The above description is taken from
21+
https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle
22+
This code was written by editing the code from
23+
https://www.riannetrujillo.com/blog/python-fractal/
2924
"""
3025
import sys
3126
import turtle
3227

33-
PROGNAME = "Sierpinski Triangle"
34-
35-
points = [[-175, -125], [0, 175], [175, -125]] # size of triangle
36-
37-
38-
def get_mid(p1, p2):
39-
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint
40-
41-
42-
def triangle(points, depth):
4328

29+
def get_mid(p1: tuple[float, float], p2: tuple[float, float]) -> tuple[float, float]:
30+
"""
31+
Find the midpoint of two points
32+
33+
>>> get_mid((0, 0), (2, 2))
34+
(1.0, 1.0)
35+
>>> get_mid((-3, -3), (3, 3))
36+
(0.0, 0.0)
37+
>>> get_mid((1, 0), (3, 2))
38+
(2.0, 1.0)
39+
>>> get_mid((0, 0), (1, 1))
40+
(0.5, 0.5)
41+
>>> get_mid((0, 0), (0, 0))
42+
(0.0, 0.0)
43+
"""
44+
return (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2
45+
46+
47+
def triangle(
48+
vertex1: tuple[float, float],
49+
vertex2: tuple[float, float],
50+
vertex3: tuple[float, float],
51+
depth: int,
52+
) -> None:
53+
"""
54+
Recursively draw the Sierpinski triangle given the vertices of the triangle
55+
and the recursion depth
56+
"""
4457
my_pen.up()
45-
my_pen.goto(points[0][0], points[0][1])
58+
my_pen.goto(vertex1[0], vertex1[1])
4659
my_pen.down()
47-
my_pen.goto(points[1][0], points[1][1])
48-
my_pen.goto(points[2][0], points[2][1])
49-
my_pen.goto(points[0][0], points[0][1])
60+
my_pen.goto(vertex2[0], vertex2[1])
61+
my_pen.goto(vertex3[0], vertex3[1])
62+
my_pen.goto(vertex1[0], vertex1[1])
5063

51-
if depth > 0:
52-
triangle(
53-
[points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])],
54-
depth - 1,
55-
)
56-
triangle(
57-
[points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])],
58-
depth - 1,
59-
)
60-
triangle(
61-
[points[2], get_mid(points[2], points[1]), get_mid(points[0], points[2])],
62-
depth - 1,
63-
)
64+
if depth == 0:
65+
return
66+
67+
triangle(vertex1, get_mid(vertex1, vertex2), get_mid(vertex1, vertex3), depth - 1)
68+
triangle(vertex2, get_mid(vertex1, vertex2), get_mid(vertex2, vertex3), depth - 1)
69+
triangle(vertex3, get_mid(vertex3, vertex2), get_mid(vertex1, vertex3), depth - 1)
6470

6571

6672
if __name__ == "__main__":
6773
if len(sys.argv) != 2:
6874
raise ValueError(
69-
"right format for using this script: "
70-
"$python fractals.py <int:depth_for_fractal>"
75+
"Correct format for using this script: "
76+
"python fractals.py <int:depth_for_fractal>"
7177
)
7278
my_pen = turtle.Turtle()
7379
my_pen.ht()
7480
my_pen.speed(5)
7581
my_pen.pencolor("red")
76-
triangle(points, int(sys.argv[1]))
82+
83+
vertices = [(-175, -125), (0, 175), (175, -125)] # vertices of triangle
84+
triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))

0 commit comments

Comments
 (0)