Alberto Alvarez
Alberto Alvarez

Reputation: 855

How to draw a circle using turtle in python?

I wanted ask how can I draw a circle using turtle module in python just using turtle.forward and turtle.left? I use the code below:

 for i in range(30):
     turtle.forward(i)
     turtle.left(i)  
 turtle.done()

What I get is that the line does not stop once I get the full cirle. How can I code so that I have a circle of specific radius and that I have a full stop once the circle is drawn (without using turtle.circle).

Upvotes: 0

Views: 13757

Answers (7)

Joyal susilan
Joyal susilan

Reputation: 1

The output is given below:

import turtle
from turtle import Turtle, Screen
import random

tim = turtle
tim.colormode(255)


def random_colors():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    colors = (r, g, b)
    return colors


tim.speed(1000)


def draw_spirograph(size_of_gap):
    for i in range(int(360 / size_of_gap)):  
        tim.color(random_colors())
        tim.circle(100) 
        tim.setheading(tim.heading() + size_of_gap)


draw_spirograph(5)

screen = Screen()
screen.exitonclick()

Upvotes: -1

Dilshan Anurada
Dilshan Anurada

Reputation: 1

import turtle as t
from turtle import Screen
import random as rn


t.colormode(255)

def rand_color():
    r = rn.randint(0,255)
    g = rn.randint(0,255)
    b = rn.randint(0,255)
    return (r, g, b)

def til_circle(angle):
    for i in range(int(360/angle)):
        turtle.pensize(2)
        turtle.speed('fastest')
        turtle.color(rand_color())
        turtle.circle(200)
        turtle.setheading(turtle.heading() + angle)

til_circle()

screen = Screen()
screen.exitonclick()

11

Upvotes: -1

David Molina
David Molina

Reputation: 63

A spirograph code

from turtle import Turtle
import random

josh = Turtle()
josh.color('DarkRed')

def random_color()->tuple:
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    return (r,g,b)

josh.speed('fastest')
josh.pensize(2)
for i in range(72):
    josh.circle(100)
    josh.right(5)
    colormode(255)
    josh.pencolor(random_color())
        
screen = Screen()
screen.setup(800,800)
screen.exitonclick()

Upvotes: -1

Salim muneer lala
Salim muneer lala

Reputation: 99

Create a SPIROGRAPH using turtle. Final output:

final output

import random
import turtle

from turtle import Turtle, Screen

tim = Turtle()

tim.shape('arrow')

turtle.colormode(255)

def random_colour( ):
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    return (r, g, b)

tim.speed('fastest')

def draw_spirograph(size_of_gap):
    for _ in range(int(360/size_of_gap)):
        tim.color(random_colour())
        tim.circle(100)
        tim.setheading(tim.heading()+size_of_gap)

draw_spirograph(5)

screen = Screen()
screen.exitonclick()

Upvotes: -2

symmetry
symmetry

Reputation: 529

I made this image as a reference, enter image description here

Essentially you need to draw the inscribed polygon with n sides.

The initial left turn will be ϴ/2.

Then forward by a = 2rsin(ϴ/2).

Each forward is followed by a left turn of the full ϴ, except that after the last forward we want only a left turn of ϴ/2 so that the heading will be correctly updated to be tangential to the circle (or arc).

Something like this,

import turtle
import math

def circle2(radius,extent=360,steps=360):
    if extent<360 and steps==360:
        steps=extent
    
    theta=extent/steps
    step_size=2*radius*math.sin(math.radians(theta/2))
    turtle.left(theta/2)
    turtle.forward(step_size)
    for i in range(1,steps):
        turtle.left(theta)
        turtle.forward(step_size)
    
    turtle.left(theta/2)
    

turtle.hideturtle()
turtle.speed(0)
turtle.getscreen().tracer(False)

circle2(50)
circle2(100,180)
turtle.up()
turtle.home()
turtle.down()
circle2(130)
circle2(130,360,10)

turtle.update()
turtle.mainloop()

enter image description here

Upvotes: 2

Chris Doyle
Chris Doyle

Reputation: 11992

If you want to draw a circle the best thing to do is to simplyfy the problem, if we consider moving 1 space for each degree of the circle then we can simply write this as

def draw_circle1():
    for _ in range(360):
        turtle.forward(1)
        turtle.left(1)

enter image description here

Now what do we know about this basic circle that we drew? well we know it took 360 steps and each step was 1. so the circle has a circumference of 360. we can use a bit of math to calculate the radius.

circumference = 2 * 3.14... * radius
360 = 2 * 3.14... * radius
360 / 2 / 3.14... = radius
radius = 57.29...

So now we can reverse this, if we want to specify a circle of a given radius, we can calculate what circumference that circle should have. divide that by the 360 degrees and we know what size step to take before each turn of 1 degree.

def draw_circle(radis):
    circumfrence = 2 * math.pi * radis
    step_size = circumfrence / 360
    for _ in range(360):
        turtle.forward(step_size)
        turtle.left(1)

if we run this for 3 separate circles each increasing in size you see it gives us a consistent result

draw_circle(20)
draw_circle(40)
draw_circle(60)
turtle.hideturtle()
turtle.done()

enter image description here

So now we have a function which can accept a radius and draw a circle based on that radius

Upvotes: 3

Jason Yang
Jason Yang

Reputation: 13061

Example here,

import turtle

def circle(distance, sections):
    angle = 360/sections
    for i in range(1, sections+1):
        turtle.forward(distance)
        turtle.left(angle)

circle(20, 30)
turtle.done()

Upvotes: 0

Related Questions