Reputation: 13
How do I draw an archimedean spiral with Python 3 with random x ,y coordinates? I have this code here:
from turtle import *
from math import *
color("blue")
down()
for i in range(200):
t = i / 20 * pi
x = (1 + 5 * t) * cos(t)
y = (1 + 5 * t) * sin(t)
goto(x, y)
up()
done()
However, this spiral can only be drawn on a fixed coordinate. I want to be able to draw a couple of these in different spots with randint()
generated x, y coordinates.
I've been playing around with it, but with no luck. Can you help?
Upvotes: 1
Views: 7457
Reputation: 41872
The turtle starts out with (x, y) set to (0, 0) which is why the spiral is centered on the screen. You can pick a random location and in the goto()
add the x, y of that location to the calculated spiral x, y:
from turtle import Turtle, Screen
from math import pi, sin, cos
from random import randint, random
RADIUS = 180 # roughly the radius of a completed spiral
screen = Screen()
WIDTH, HEIGHT = screen.window_width(), screen.window_height()
turtle = Turtle(visible=False)
turtle.speed('fastest') # because I have no patience
turtle.up()
for _ in range(3):
x = randint(RADIUS - WIDTH//2, WIDTH//2 - RADIUS)
y = randint(RADIUS - HEIGHT//2, HEIGHT//2 - RADIUS)
turtle.goto(x, y)
turtle.color(random(), random(), random())
turtle.down()
for i in range(200):
t = i / 20 * pi
dx = (1 + 5 * t) * cos(t)
dy = (1 + 5 * t) * sin(t)
turtle.goto(x + dx, y + dy)
turtle.up()
screen.exitonclick()
Upvotes: 3