Skip to content

Create sirpinski_fractal.py #1

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

Merged
merged 1 commit into from
Oct 29, 2023
Merged
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
37 changes: 37 additions & 0 deletions sirpinski_fractal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import turtle

# Function to draw a Sierpinski triangle
def sierpinski(order, size):
if order == 0:
for _ in range(3):
turtle.forward(size)
turtle.left(120)
else:
size /= 2
sierpinski(order - 1, size)
turtle.forward(size)
sierpinski(order - 1, size)
turtle.backward(size)
turtle.left(60)
turtle.forward(size)
turtle.right(60)
sierpinski(order - 1, size)
turtle.left(60)
turtle.backward(size)
turtle.right(60)

# Initialize the Turtle
turtle.speed(0) # Fastest drawing speed
turtle.penup()
turtle.goto(-150, -150)
turtle.pendown()

# Set the order and size of the Sierpinski triangle
order = 3 # You can adjust this to change the level of detail
size = 300

# Draw the Sierpinski triangle
sierpinski(order, size)

# Close the Turtle graphics window on click
turtle.exitonclick()