Как нарисавать Архимедову спираль на Python?
Привет. Эту задачку я нашёл в курсе python от МФТИ. Сразу оговорюсь, учиться кодить я начал где-то с месяц назад.
Есть 2 варианта — непосредственное «рисование» спирали черепашкой:
import turtle
turtle.shape(‘turtle’)
k=1
fi_rad=0.1
fi_degr=fi_rad*(180/3.14)
for i in range (0,1000):
ro=k*fi_rad
turtle.forward(ro)
turtle.left(fi_degr)
fi_rad+=0.1
ro+=ro
или перемещение черепашки на спираль:
import turtle
turtle.shape(‘turtle’)
import math
k=1
fi_rad=0.1
for i in range (0,1000):
ro=k*fi_rad
x = math.cos(fi_rad)*ro
y = math.sin(fi_rad)*ro
turtle.goto(x,y)
fi_rad+=0.1
В любом случае, настоятельно рекомендую решить задачу самостоятельно, т.к. именно те муки, которые ты испытываешь при решении задачи — которые заставляют тебя гуглить твой запрос, генерируют новые нейроны в твоем мозгу и делают тебя умнее.
Turtle Spirals
Drawing with Python Turtles can be a lot of fun! You can draw nice turbines with Python turtle with the codes in this tutorial.
We will explain how you can twist the code to give more flavor to your drawings and practice coding while drawing or vice versa, who knows
Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
Simple Turtle Spiral
Turtle Step and Direction Adjustment (forward, backward, left, right)
Let’s make the spiral more dense with decreasing the steps ( a.forward(2+i/4) and
a.left(30-i/12) ) and increasing the amount of turns ( for i in range(240): ).
Turtle Spiral with denser pattern
Smaller Loop (Fibonacci Sequence)
Fibonacci sequence is an interesting number sequence, sometimes referred to as golden ratio, that can be traced in many natural patterns in universe such as flowers, shells, snails, trees, leaves, storms, galaxies and fingerprints.
Here is an attempt to draw on Fibonacci proportions. Not exactly it, but close.
Turtle Spiral — Shell or Fibonacci Shape
Turtle with Colors
You can escape Black & White or Mono-Color drawings by implementing .color() method of turtle.
Mono-color Turtle Drawing (Only implements one color throughout)
Turtle Spiral — Shell or Fibonacci Shape (red colored)
Multi-color Turtle Drawing (Navigates through a list of different colors)
Another fun idea is iterating through different colors. This can be easily achieved by defining a set of color and some basic loop iteration with turtle.
Color list is iterated using the help of “Modulus” operator ( % ) in Python. If you’d like to read an extensive article about Python operators including Modulus you can click here.
Turtle Spiral — Shell or Fibonacci Shape (rainbow)
That’s it. What you can do with Python Turtle is up to your imagination, so there is no limit. Try something that’s relevant to you and enjoy practicing!
If you find turtle interesting we have a very extensive tutorial that explains different Python concepts (such as if-else, user functions, user input, operators, data types, loops etc.) through turtle here: Python Turtle Tutorial.
ps: Don’t forget to include turtle.done() in the end so your turtle window can be terminated.
pss: We recommend Spyder IDE which comes with Anaconda Open Source All-in-One installation solution. Although it’s an IDE specialized in scientific applications it’s also perfect for Python practice. You can read more about effortless Python Installation here.
Colorful Spiral Web Using Turtle Python
“T urtle” is a python feature like a drawing board, which lets you command a turtle to draw all over it!
You can use functions like turtle.forward(. ) and turtle.left(. ) which can move the turtle around.
Before you can use turtle, you have to import it. We recommend playing around with it in the interactive interpreter first, as there is an extra bit of work required to make it work from files. Just go to your terminal and type:
How to draw a spiral with Python turtle
In the previous 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 using turtle.circle but we’re going to draw it in a different way
Exit fullscreen mode
In the code above, tracer and update 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
Exit fullscreen mode
i replaces 1 as parameter to forward

We’ll get something that quickly moves past the screen. Let’s see if we can make it turn faster
Exit fullscreen mode
20 replaces 1 as parameter to right

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
Exit fullscreen mode
Code prints 4 circle sections in 4 quadrants

We can use turtle’s circle 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
Exit fullscreen mode
forward and right function calls are replaced by circle
The circle 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
Exit fullscreen mode
In the code above, t1 and t2 are two turtles that have been initially set to look to the right and to the left respectively using the setheading command

Both turtle have now started their own spiral in their own direction. Now let’s see how to make this more configurable
Exit fullscreen mode
We have simply changed the code such that any number of turtles can be created by changing N , 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
Exit fullscreen mode
We set angle to 30 and we squared the radius
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)