Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions fractals/gosper_curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Description
The Gosper curve (also known as the flowsnake) is a fractal curve discovered
by Bill Gosper. It is generated recursively by replacing each line segment
with a specific pattern of smaller segments rotated by multiples of
60 degrees.

With each iteration, the curve becomes more complex and gradually fills
a hexagonal region.

(description adapted from https://en.wikipedia.org/wiki/Gosper_curve)

Requirements (pip):
- turtle (standard library)
"""

import math
import turtle


def draw_gosper_curve(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file fractals/gosper_curve.py, please provide doctest for the function draw_gosper_curve

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it open a window for drawing, so it can't have some tests. please, just run the python file and change function parameters for different tests

side_length: float, depth: int, direction: int = -1, angle: float = 60.0
) -> None:
"""
Recursively draw a Gosper curve using turtle graphics.

Args:
side_length: Length of the current segment.
depth: Recursive depth of the fractal.
direction: Direction of the curve (1 or -1).
angle: Turn angle in degrees.

Note:
Do not run this as a doctest; it uses turtle graphics.
"""
if depth == 0:
turtle.forward(side_length)
return

side_length /= math.sqrt(7)
depth -= 1

if direction == -1:
draw_gosper_curve(side_length, depth, -1, angle)
turtle.left(angle)
draw_gosper_curve(side_length, depth, 1, angle)
turtle.left(2 * angle)
draw_gosper_curve(side_length, depth, 1, angle)
turtle.right(angle)
draw_gosper_curve(side_length, depth, -1, angle)
turtle.right(2 * angle)
draw_gosper_curve(side_length, depth, -1, angle)
draw_gosper_curve(side_length, depth, -1, angle)
turtle.right(angle)
draw_gosper_curve(side_length, depth, 1, angle)
turtle.left(angle)
else:
turtle.right(angle)
draw_gosper_curve(side_length, depth, -1, angle)
turtle.left(angle)
draw_gosper_curve(side_length, depth, 1, angle)
draw_gosper_curve(side_length, depth, 1, angle)
turtle.left(2 * angle)
draw_gosper_curve(side_length, depth, 1, angle)
turtle.left(angle)
draw_gosper_curve(side_length, depth, -1, angle)
turtle.right(2 * angle)
draw_gosper_curve(side_length, depth, -1, angle)
turtle.right(angle)
draw_gosper_curve(side_length, depth, 1, angle)


if __name__ == "__main__":
turtle.title("Gosper Curve")
turtle.speed(0)

turtle.penup()
turtle.goto(0, -200)
turtle.pendown()

draw_gosper_curve(200.0, 4)

turtle.exitonclick()