简体   繁体   English

如何使用乌龟图形创建色轮?

[英]How to create a color wheel using turtle graphics?

I am trying to create somewhat of a color wheel using the turtle module in Python. 我正在尝试使用Python中的turtle模块创建某种色轮。 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: 我的目标是绕一个半径为250px的圆绘制颜色:

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°. 幸运的是,对于初始值,这些值的计算相对简单:将位置设置为(circle_center x值+半径,circle_center y值),其正北方向为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 结果

在此处输入图片说明

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 OUTPUT

在此处输入图片说明

This approach takes slightly less code and produces noticeably faster output. 这种方法占用的代码少一点,并且输出明显更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM