anurag kumar anuragkumarak95gmail com gitanuragkumarak95 simple example of fractal generation using recursion what is the sierpiski triangle the sierpiski triangle sometimes spelled sierpinski also called the sierpiski gasket or sierpiski 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 selfsimilar setsthat is it is a mathematically generated pattern that is reproducible at any magnification or reduction it is named after the polish mathematician wacaw sierpiski but appeared as a decorative pattern many centuries before the work of sierpiski usage python sierpinskitriangle py int depthforfractal credits the above description is taken from https en wikipedia orgwikisierpic584skitriangle this code was written by editing the code from https www riannetrujillo comblogpythonfractal find the midpoint of two points getmid0 0 2 2 1 0 1 0 getmid3 3 3 3 0 0 0 0 getmid1 0 3 2 2 0 1 0 getmid0 0 1 1 0 5 0 5 getmid0 0 0 0 0 0 0 0 recursively draw the sierpinski triangle given the vertices of the triangle and the recursion depth 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 recursively draw the sierpinski triangle given the vertices of the triangle and the recursion depth vertices of triangle
import sys
import turtle


def get_mid(p1: tuple[float, float], p2: tuple[float, float]) -> tuple[float, float]:
    
    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:
    
    my_pen.up()
    my_pen.goto(vertex1[0], vertex1[1])
    my_pen.down()
    my_pen.goto(vertex2[0], vertex2[1])
    my_pen.goto(vertex3[0], vertex3[1])
    my_pen.goto(vertex1[0], vertex1[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(
            "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")

    vertices = [(-175, -125), (0, 175), (175, -125)]  
    triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))
    turtle.Screen().exitonclick()