From 05dc00457ac2238b4ca8c10135faae43e37fea58 Mon Sep 17 00:00:00 2001 From: Moez <96164867+kaziadilmemon@users.noreply.github.com> Date: Mon, 23 Oct 2023 01:14:11 +0500 Subject: [PATCH 1/2] Achimedes Spiral in Python --- A/Archimedes' Spiral/ArchimedesSpiral.py | 34 ++++++++++++++++++++++++ A/Archimedes' Spiral/README.md | 12 +++++++++ 2 files changed, 46 insertions(+) create mode 100644 A/Archimedes' Spiral/ArchimedesSpiral.py create mode 100644 A/Archimedes' Spiral/README.md 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. From 0d1bdfd8feb9981037a955882737fa9c2989027b Mon Sep 17 00:00:00 2001 From: Muhammad Adil Memon <96164867+kaziadilmemon@users.noreply.github.com> Date: Mon, 30 Oct 2023 04:21:27 +0500 Subject: [PATCH 2/2] Create sirpinski_fractal.py --- sirpinski_fractal.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sirpinski_fractal.py 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()