DamnGood
DamnGood

Reputation: 109

How to draw a ring of colors using turtle module in python

I have the following code which is supposed to draw a ring of colors around a circle but only one color if printed and changed 8 times before moving to the next

import turtle

def drawCircle(colorList, radius):
    for color in colorList:
        turtle.color(color)
        for i in range(len(colorList)):
            turtle.penup()
            turtle.setpos(0, -radius)
            xpos=turtle.xcor()
            ypos=turtle.ycor()
            head=turtle.heading()
            turtle.begin_fill()
            turtle.pendown()
            turtle.home()
            turtle.setpos(xpos,ypos)
            turtle.setheading(head)
            turtle.circle(radius)
            turtle.end_fill()
            turtle.penup()
    return

colorList=["#880000","#884400","#888800","#008800",\
        "#008888","#000088","#440088","#880088"]

drawCircle(colorList,200)

How would I make it that each arc around the circle is a different color. here is an example

Upvotes: 2

Views: 2107

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114108

you will need something like this

def drawSegment(color,x, y, r, angleStart, angleEnd, step=1):
    #More efficient to work in radians
    radianStart = angleStart*pi / 180
    radianEnd   = angleEnd*pi / 180
    radianStep=step *pi/180

    #Draw the segment
    turtle.penup()
    turtle.setpos(x,y)
    turtle.color(color)
    turtle.begin_fill()
    turtle.pendown()
    for theta in arange(radianStart,radianEnd,radianStep):
        turtle.setpos(x + r * cos(theta), y + r * sin(theta))

    turtle.setpos(x + r * cos(radianEnd), y + r * sin(radianEnd))
    turtle.setpos(x, y);
    turtle.end_fill()

def drawCircle(colorList,radius):
    #do something to draw an equal segment for each color advancing it around 360 degree's

Upvotes: 1

Related Questions