|
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 |
16 | 3 |
|
17 |
| -Requirements(pip): |
18 |
| - - turtle |
| 4 | +Simple example of fractal generation using recursion. |
19 | 5 |
|
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. |
22 | 15 |
|
23 |
| -Usage: |
24 |
| - - $python sierpinski_triangle.py <int:depth_for_fractal> |
25 | 16 |
|
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> |
28 | 18 |
|
| 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/ |
29 | 24 | """
|
30 | 25 | import sys
|
31 | 26 | import turtle
|
32 | 27 |
|
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): |
43 | 28 |
|
| 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 | + """ |
44 | 57 | my_pen.up()
|
45 |
| - my_pen.goto(points[0][0], points[0][1]) |
| 58 | + my_pen.goto(vertex1[0], vertex1[1]) |
46 | 59 | 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]) |
50 | 63 |
|
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) |
64 | 70 |
|
65 | 71 |
|
66 | 72 | if __name__ == "__main__":
|
67 | 73 | if len(sys.argv) != 2:
|
68 | 74 | 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>" |
71 | 77 | )
|
72 | 78 | my_pen = turtle.Turtle()
|
73 | 79 | my_pen.ht()
|
74 | 80 | my_pen.speed(5)
|
75 | 81 | 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