문제

I am trying to create somewhat of a color wheel using the turtle module in Python. Let's say I have a list of colors:

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

I am aiming to go around a circle with a radius of 250px plotting in the colors:

def drawColors():
    for color in colors:
        turtle.color(dark)
        for i in range(len(colors)):
            turtle.begin_fill
            turtle.circle(150)
            turtle.end_fill()
        turtle.done()
도움이 되었습니까?

해결책

You can do it by dividing the circle up into multiple circular sectors (aka pie slices) and drawing each one in a different color. The tricky part doing it with turtle graphics is setting the initial position and heading (or direction) of the turtle to be at the start of the arc of each one. Also, unlike with the case with a full circle, you need to manually close the figure before filling it by drawing the final line segment from the end of the arc back to the center of the circle.

While this could be calculated mathematically, doing that is avoided in the following code by remembering, for all but the first sector, where the previous one left off and using that as the starting position and heading for the next. Fortunately for the initial one, these values are relatively simple to compute: the position is set to the (circle_center x value + radius, circle_center y value) with a due North heading of 90°.

import turtle

colors = ['#880000','#884400','#888800','#008800',
          '#008888','#000088','#440088','#880088']

def draw_color_wheel(colors, radius, center=(0, 0)):
    slice_angle = 360 / len(colors)
    heading, position = 90, (center[0] + radius, center[1])
    for color in colors:
        turtle.color(color, color)
        turtle.penup()
        turtle.goto(position)
        turtle.setheading(heading)
        turtle.pendown()
        turtle.begin_fill()
        turtle.circle(radius, extent=slice_angle)
        heading, position = turtle.heading(), turtle.position()
        turtle.penup()
        turtle.goto(center)
        turtle.end_fill()

draw_color_wheel(colors, 150, center=(25, 50))
turtle.hideturtle()
print('done - press any key to exit')
turtle.onkeypress(exit)
turtle.listen()
turtle.done()

Result

enter image description here

다른 팁

Since this question has become active again, let's solve it using stamping rather than drawing:

from turtle import Turtle, Screen

colors = ['#880000', '#884400', '#888800', '#008800',
          '#008888', '#000088', '#440088', '#880088']

def draw_color_wheel(colors, radius, center=(0, 0)):
    slice_angle = 360 / len(colors)

    yertle = Turtle(visible=False)
    yertle.penup()

    yertle.begin_poly()
    yertle.sety(radius)
    yertle.circle(-radius, extent=slice_angle)
    yertle.home()
    yertle.end_poly()

    screen.register_shape('slice', yertle.get_poly())
    yertle.shape('slice')
    yertle.setposition(center)

    for color in colors:
        yertle.color(color)
        yertle.stamp()
        yertle.left(slice_angle)

screen = Screen()

draw_color_wheel(colors, 250, center=(25, 50))

screen.exitonclick()

OUTPUT

enter image description here

This approach takes slightly less code and produces noticeably faster output.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top