Skip to content
Other Algorithms

Fractals

Recursive self-similarity — beauty in infinity

Koch Snowflakedepth = 0

Depth 0 — base shape

About Fractals

Fractals are shapes that look the same at every scale (self-similarity). Built with recursion: apply a simple rule to itself repeatedly. Infinite complexity from simple rules.

Self-similarNon-integer dimRecursiveFound in nature
koch.py
def koch(p1, p2, depth):
    if depth == 0:
        return [p1, p2]
    # ۳ نقطه میانی + نقطه قله
    p3 = lerp(p1, p2, 1/3)
    p5 = lerp(p1, p2, 2/3)
    angle = atan2(p2.y-p1.y, p2.x-p1.x) - pi/3
    length = dist(p1,p2) / 3
    p4 = p3 + Vec2(cos(angle), sin(angle)) * length

    return (koch(p1,p3,d-1) + koch(p3,p4,d-1)
          + koch(p4,p5,d-1) + koch(p5,p2,d-1))