Skip to content

Commit 05dc004

Browse files
Achimedes Spiral in Python
1 parent adfa11f commit 05dc004

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from turtle import Turtle, Screen
2+
from math import pi, sin, cos
3+
from random import randint, random
4+
5+
RADIUS = 180 # roughly the radius of a completed spiral
6+
7+
screen = Screen()
8+
screen.title("Archimedes' Spiral")
9+
10+
WIDTH, HEIGHT = screen.window_width(), screen.window_height()
11+
12+
t = Turtle(visible=False)
13+
t.speed('fastest') # because I have no patience
14+
15+
t.up()
16+
17+
for _ in range(3):
18+
x = randint(RADIUS - WIDTH//2, WIDTH//2 - RADIUS)
19+
y = randint(RADIUS - HEIGHT//2, HEIGHT//2 - RADIUS)
20+
t.goto(x, y)
21+
22+
t.color(random(), random(), random())
23+
t.down()
24+
25+
for i in range(200):
26+
t_ = i / 20 * pi
27+
dx = (1 + 5 * t_) * cos(t_)
28+
dy = (1 + 5 * t_) * sin(t_)
29+
30+
t.goto(x + dx, y + dy)
31+
32+
t.up()
33+
34+
screen.exitonclick()

A/Archimedes' Spiral/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Code Explanation
2+
3+
### Purpose
4+
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.
5+
6+
### Spiral Generation
7+
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.
8+
9+
### Interaction
10+
The script allows for user interaction by closing the graphical window when you click on the drawing.
11+
12+
In summary, the code combines randomness, mathematics, and visual graphics to produce a captivating representation of Archimedes' spiral.

0 commit comments

Comments
 (0)