如何在python中使用龟模块绘制一个颜色环

时间:2014-05-02 21:37:22

标签: python python-3.x

我有以下代码,它应该围绕一个圆圈绘制一个颜色的环,但是如果打印并且在移动到下一个之前只更改了8次,则只有一种颜色

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)

我如何确定圆周围的每个弧都是不同的颜色。 here is an example

1 个答案:

答案 0 :(得分:1)

你需要这样的东西

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