Code :
import turtle, colorsys, math
s = turtle.Screen()
s.bgcolor("black")
s.setup(700, 700)
s.tracer(0)
t = turtle.Turtle()
t.speed(0)
t.hideturtle()
t.pensize(1)
hue = 0
for i in range(120):
t.penup()
t.goto(0, 0)
t.setheading(i * 3)
t.pendown()
t.pencolor(colorsys.hsv_to_rgb(hue, 1, 1))
hue += 0.008
for j in range(361):
a = math.radians(j)
r = 220 + 35 * math.sin(2 * a + i * 0.08)
t.goto(r * math.cos(a), r * math.sin(a))
s.update()
t.pensize(2)
for i in range(120):
t.penup()
t.goto(0, 0)
t.setheading(i * 3)
t.pendown()
t.pencolor(colorsys.hsv_to_rgb(i / 120, 1, 1))
t.forward(115)
s.update()
turtle.done()
Explanation:
1. Import Required Libraries
import turtle, colorsys, math
turtle → Used to create drawings and graphics.
colorsys → Used to generate HSV-based rainbow colors.
math → Used for mathematical functions such as sin(), cos(), and converting angles to radians.
import turtle, colorsys, math
turtle → Used to create drawings and graphics.
colorsys → Used to generate HSV-based rainbow colors.
math → Used for mathematical functions such as sin(), cos(), and converting angles to radians.
2. Create the Screen
s = turtle.Screen()
Creates the Turtle graphics window and stores it in the variable s.
3. Set Background Color
s.bgcolor("black")
Sets the background of the Turtle window to black.
4. Set Window Size
s.setup(700, 700)
Creates a 700 × 700 pixel Turtle window.
5. Turn Off Automatic Updates
s.tracer(0)
Stops Turtle from refreshing the screen after every drawing command.
This makes the drawing much faster and allows us to control when the screen updates.
๐ข Creating the Turtle
6. Create Turtle Object
t = turtle.Turtle()
Creates a Turtle and stores it in the variable t.
7. Set Turtle Speed
t.speed(0)
Sets the Turtle to its fastest drawing speed.
8. Hide the Turtle
t.hideturtle()
Hides the Turtle cursor so only the artwork is visible.
9. Set Pen Thickness
t.pensize(1)
Sets the drawing line thickness to 1 pixel, creating a fine and detailed pattern.
๐ Creating the Outer Rainbow Ring
10. Start Hue
hue = 0
Initializes the color value.
hue controls the position in the HSV color spectrum.
11. Create 120 Layers
for i in range(120):
Runs the outer-ring drawing process 120 times.
Each iteration creates another colorful curved layer.
12. Lift the Pen
t.penup()
Lifts the Turtle's pen so it can move without drawing.
13. Move to Center
t.goto(0, 0)
Moves the Turtle to the center of the screen.
14. Rotate the Turtle
t.setheading(i * 3)
Changes the Turtle's direction.
Since i increases each time, the starting direction changes by 3° per layer.
This creates the overlapping circular effect.
15. Put the Pen Down
t.pendown()
Places the pen back down so the Turtle starts drawing.
16. Set Rainbow Color
t.pencolor(colorsys.hsv_to_rgb(hue, 1, 1))
Converts the HSV color value into an RGB color and applies it to the Turtle's pen.
The values:
Hue → hue
Saturation → 1
Brightness → 1
produce bright, vivid colors. ๐
17. Increase the Hue
hue += 0.008
Slightly changes the hue after every layer.
This creates the smooth rainbow color transition.
๐ Drawing the Wavy Ring
18. Create 360 Points
for j in range(361):
Runs from 0 to 360.
This gives the Turtle enough points to complete a full circular shape.
19. Convert Degrees to Radians
a = math.radians(j)
Converts the angle from degrees into radians because Python's sin() and cos() functions use radians.
20. Calculate the Radius
r = 220 + 35 * math.sin(2 * a + i * 0.08)
This is the main mathematical trick behind the design. ๐
220 → Base radius of the ring.
35 → Controls how much the ring waves outward and inward.
sin() → Creates the smooth wave.
2 * a → Creates two major waves around the circle.
i * 0.08 → Changes the wave position for every layer.
This produces the beautiful wavy outer ring instead of a simple circle.
21. Calculate X and Y
t.goto(r * math.cos(a), r * math.sin(a))
Calculates the X and Y coordinates of each point on the circular pattern.
The mathematical formulas are:
X = r × cos(angle)
Y = r × sin(angle)
The Turtle moves through these points and creates the curved ring.
22. Update the Screen
s.update()
Refreshes the screen after each layer.
Because tracer(0) was used earlier, the drawing only becomes visible when update() is called.
This creates the live layer-by-layer drawing effect.
๐ Creating the Center Rainbow Rays
23. Increase Pen Size
t.pensize(2)
Makes the center rays slightly thicker than the outer pattern.
24. Create 120 Rays
for i in range(120):
Runs 120 times to create 120 colorful rays from the center.
25. Lift the Pen
t.penup()
Stops the Turtle from drawing while moving.
26. Return to Center
t.goto(0, 0)
Moves the Turtle back to the center before drawing each ray.
27. Set Ray Direction
t.setheading(i * 3)
Rotates the Turtle by 3° for every iteration.
This spreads the rays around the complete circle.
28. Start Drawing
t.pendown()
Puts the pen down so the ray can be drawn.
29. Give Each Ray a Rainbow Color
t.pencolor(colorsys.hsv_to_rgb(i / 120, 1, 1))
Generates a different HSV color for each ray.
As i changes from 0 to 119, the colors smoothly move through the rainbow spectrum.
30. Draw the Ray
t.forward(115)
Moves the Turtle 115 pixels forward, creating one ray from the center.
✅ Finish the Drawing
31. Final Screen Update
s.update()
Refreshes the screen one final time so the complete artwork is displayed.
32. Keep the Window Open
turtle.done()
Finishes the Turtle program and keeps the graphics window open.

0 Comments:
Post a Comment