Skip to content

Achimedes Spiral in Python #225

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions A/Archimedes' Spiral/ArchimedesSpiral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from turtle import Turtle, Screen
from math import pi, sin, cos
from random import randint, random

RADIUS = 180 # roughly the radius of a completed spiral

screen = Screen()
screen.title("Archimedes' Spiral")

WIDTH, HEIGHT = screen.window_width(), screen.window_height()

t = Turtle(visible=False)
t.speed('fastest') # because I have no patience

t.up()

for _ in range(3):
x = randint(RADIUS - WIDTH//2, WIDTH//2 - RADIUS)
y = randint(RADIUS - HEIGHT//2, HEIGHT//2 - RADIUS)
t.goto(x, y)

t.color(random(), random(), random())
t.down()

for i in range(200):
t_ = i / 20 * pi
dx = (1 + 5 * t_) * cos(t_)
dy = (1 + 5 * t_) * sin(t_)

t.goto(x + dx, y + dy)

t.up()

screen.exitonclick()
12 changes: 12 additions & 0 deletions A/Archimedes' Spiral/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Code Explanation

### Purpose
This Python code utilizes the Turtle graphics library to create an illustration of three sections of an Archimedes' spiral. It achieves this by using random starting positions and colors for each section.

### Spiral Generation
The Archimedes' spiral is generated using trigonometric functions and mathematical principles. The Turtle graphics library is employed to visualize the spiral, with the turtle moving along the spiral path and drawing it on the screen.

### Interaction
The script allows for user interaction by closing the graphical window when you click on the drawing.

In summary, the code combines randomness, mathematics, and visual graphics to produce a captivating representation of Archimedes' spiral.
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()