Code:
Explanation:
1. Import Libraries
import turtle
import math
import time
turtle → Used for drawing.
math → Calculates angles and coordinates.
time → Controls animation speed.
2. Create the Screen
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#020205")
Creates a 700 × 700 window.
Sets a dark background.
3. Configure the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(1)
Creates the turtle.
Hides the cursor.
Sets maximum drawing speed.
Uses a thin line.
4. Define Neon Colors
colors = ["#00ffff", "#ffb6d9", "#ffffff"]
Stores cyan, pink, and white colors.
These colors repeat throughout the pattern.
5. Create 90 Layers
for i in range(90):
Draws 90 rotating squares.
Each square creates another layer of the spiral.
6. Calculate Size and Rotation
r = 10 + i * 2.8
rotation = i * 4
r gradually increases the square size.
rotation rotates each square by 4°.
7. Create an Empty Point List
points = []
Creates a list to store the four square corners.
8. Calculate Four Corners
for j in range(4):
Runs four times because a square has four corners.
angle = math.radians(rotation + j * 90)
Calculates each corner's angle.
90° separates the four corners.
9. Calculate X and Y Coordinates
x = r * math.cos(angle)
y = r * math.sin(angle)
Calculates the position of each corner using trigonometry.
10. Store Each Point
points.append((x, y))
Saves the calculated (x, y) coordinate.
11. Select the Square Color
t.color(colors[i % 3])
Cycles through the three neon colors.
12. Move to the First Corner
t.penup()
t.goto(points[0])
t.pendown()
Moves to the first corner without drawing.
Starts drawing from that point.
13. Draw the Three Sides
for p in points[1:]:
Loops through the remaining three corners.
t.goto(p)
Connects each corner with a line.
screen.update()
time.sleep(0.10)
Updates the screen.
Creates a slow drawing animation.
14. Close the Square
t.goto(points[0])
Returns to the first corner.
Completes the fourth side.
15. Pause Between Layers
screen.update()
time.sleep(0.12)
time.sleep(0.08)
Updates the screen.
Adds pauses so each rotating layer is visible.
16. Finish the Drawing
turtle.done()
Keeps the Turtle window open.
Completes the neon spiral animation.

0 Comments:
Post a Comment