-
-
Notifications
You must be signed in to change notification settings - Fork 50.1k
Add Gosper curve fractal implementation #14352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ab3aab5
5e06aed
cf34feb
042e435
127a6f0
91e7e62
86788d8
9f45232
0aa3224
4e32ef9
70add35
4c488fb
318f4f3
e917ae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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( | ||
badtommy67 marked this conversation as resolved.
Show resolved
Hide resolved
badtommy67 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
Uh oh!
There was an error while loading. Please reload this page.