Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to draw a spiral with Python turtle
taarimalta
taarimalta

Posted on • Edited on

     

How to draw a spiral with Python turtle

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()
Enter fullscreen modeExit fullscreen mode

In the code above,tracer andupdate commands are used to increase the drawing speed. We can remove them if we want.

image

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)
Enter fullscreen modeExit fullscreen mode

i replaces1 as parameter toforward

image

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)
Enter fullscreen modeExit fullscreen mode

20 replaces1 as parameter toright

image

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)
Enter fullscreen modeExit fullscreen mode

Code prints 4 circle sections in 4 quadrants

image

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()
Enter fullscreen modeExit fullscreen mode

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

image

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()
Enter fullscreen modeExit fullscreen mode

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

image

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()
Enter fullscreen modeExit fullscreen mode

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

image

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()
Enter fullscreen modeExit fullscreen mode

We setangle 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)

image

Yayy!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromtaarimalta

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp