diff --git a/A/Archimedes' Spiral/ArchimedesSpiral.py b/A/Archimedes' Spiral/ArchimedesSpiral.py new file mode 100644 index 00000000..dad1828d --- /dev/null +++ b/A/Archimedes' Spiral/ArchimedesSpiral.py @@ -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() diff --git a/A/Archimedes' Spiral/README.md b/A/Archimedes' Spiral/README.md new file mode 100644 index 00000000..5dbb69a2 --- /dev/null +++ b/A/Archimedes' Spiral/README.md @@ -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. diff --git a/sirpinski_fractal.py b/sirpinski_fractal.py new file mode 100644 index 00000000..ec4c8460 --- /dev/null +++ b/sirpinski_fractal.py @@ -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()