
In theprevious post , we learnt how to draw a filled star using Python turtle. In this article lets do a spiral because why not.
Circle
We can easily draw a circle usingturtle.circle but we're going to draw it in a different way
importturtleastt.tracer(10,1)foriinrange(360):t.forward(1)t.right(1)t.update()
In the code above,tracer andupdate commands are used to increase the drawing speed. We can remove them if we want.
The turtle moves a step forward then turns right by 1 degree. By the time the loop completes the turtle has turned by 360 degrees so has completed a full rotation and we get a circle
Increasing radius and rate of turn
Instead of moving at a constant distance what would happen if the turtle moves more on every iteration
foriinrange(360):t.forward(i)t.right(1)
i
replaces1
as parameter toforward
We'll get something that quickly moves past the screen. Let's see if we can make it turn faster
foriinrange(360):t.forward(i)t.right(20)
20
replaces1
as parameter toright
Here's our spiral. But notice that the curve isn't smooth
Make the curve smoother
Let's see how we can make the spiral curve smoother. But first try out the following code
# Quadrant 1t1=t.Turtle()t1.penup()t1.goto(125,125)t1.pendown()t1.circle(100)# Quadrant 2t2=t.Turtle()t2.penup()t2.goto(-125,125)t2.pendown()t2.circle(100,270)# Quadrant 3t2=t.Turtle()t2.penup()t2.goto(-125,-125)t2.pendown()t2.circle(100,180)# Quadrant 4t3=t.Turtle()t3.penup()t3.goto(125,-125)t3.pendown()t3.circle(100,90)
Code prints 4 circle sections in 4 quadrants
We can use turtle'scircle
function to draw a portion of a circle. We can use this feature to make our turtle move in a smoother way along the spiral
importturtleastt.tracer(10,1)foriinrange(360):t.circle(i,20)t.update()
forward
andright
function calls are replaced bycircle
Thecircle
function above moves the turtle forward but also turns by a certain angle. Here's what we get
We now have a smooth spiral!
Add more arms
But what if we wanted something that looks a little different - like a spiral galaxy or the milky way? We first need to add more arms.
To do this we're going to create multiple turtles
importturtleastt.tracer(10,1)t1=t.Turtle()t2=t.Turtle()t1.setheading(0)# Looks to the rightt2.setheading(180)# Looks to the rightforxinrange(360):radius=xangle=1t1.circle(radius,angle)t2.circle(radius,angle)t.update()
In the code above,t1
andt2
are two turtles that have been initially set to look to the right and to the left respectively using thesetheading command
Both turtle have now started their own spiral in their own direction. Now let's see how to make this more configurable
importturtleastt.tracer(10,1)N=10angle=1turtles=[]forpositioninrange(N):look_at=360/N*positionnew=t.Turtle()new.setheading(look_at)turtles.append(new)forradiusinrange(360):formyinturtles:my.circle(radius,angle)t.update()
We have simply changed the code such that any number of turtles can be created by changingN
, and they all look towards different directions in a symmetric manner
Interestingly the spiral arms seem to intersect. We can prevent that from happening by making a few adjustments
importturtleastt.tracer(10,1)N=10angle=30turtles=[]forpositioninrange(N):look_at=360/N*positionnew=t.Turtle()new.setheading(look_at)turtles.append(new)forradiusinrange(360):formyinturtles:my.circle(radius*radius,angle)t.update()
We set
angle
to30
and we squared theradius
By making the angle of turn on each iteration larger and increasing the rate at which the spiral increases (by squaring the radius -radius*radius
) we can prevent the spirals from intersecting. (Note that I found this out by accident)
Yayy!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse