Skip to content
Open
Changes from 11 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
89 changes: 89 additions & 0 deletions fractals/gosper_curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
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(
Copy link
Copy Markdown

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
Copy Markdown
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.

>>> import turtle
>>> draw_gosper_curve(100.0, 1)
"""

Check failure on line 37 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:25:1: invalid-syntax: Expected an indented block after function definition
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)
```

Check failure on line 73 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:73:3: invalid-syntax: Got unexpected token `

Check failure on line 73 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:73:2: invalid-syntax: Got unexpected token `

Check failure on line 73 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:73:1: invalid-syntax: Got unexpected token `

Check failure on line 74 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:73:4: invalid-syntax: Expected a statement
if **name** == "**main**":

Check failure on line 75 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:75:13: invalid-syntax: Expected an expression

Check failure on line 75 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:75:4: invalid-syntax: Expected an expression
turtle.title("Gosper Curve")

Check failure on line 76 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:76:1: invalid-syntax: Expected an indented block after `if` statement
turtle.speed(0)

```

Check failure on line 79 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:79:2: invalid-syntax: Got unexpected token `

Check failure on line 79 in fractals/gosper_curve.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

fractals/gosper_curve.py:79:1: invalid-syntax: Got unexpected token `
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()

draw_gosper_curve(200.0, 4)

turtle.exitonclick()
```

"""
Loading