python与分形0012 - 【教程】旋转的彩色N边形(圆盘)

今天周五,513330又不能666了,哎,腿都蹲麻了。

395fd49aa46fba01be52c4475f8f3807.png
513330

在上一节的教程中,我们讲了怎么通过三角形画一个多边形的方法。

今天,我们先来给它弄点色彩,再让它动起来,先来看看效果:

a211f33713c07ec2af0971d909b2fea2.gif

注:gif动图上传有1M限制,这个图被压缩了91.83%,原图更酷炫,看文末视频。

开始吧。

第一步,涂色

前面我们讲到多边形是用三角形拼接而成的,今天我们新引进两个函数,用来对封闭区域涂色。

turtle.begin_fill()

To be called just before drawing a shape to be filled.

turtle.end_fill()

Fill the shape drawn after the last call to begin_fill().

效果如下:

8cc5c071724b3e2b213e240c35963465.gif

代码:

length = 400
turtle.color("white")

turtle.goto(0, 0)
turtle.begin_fill()
turtle.pendown()
turtle.seth(0)
turtle.fd(length)
(x,y)=turtle.pos()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.seth(72)
turtle.fd(length)
turtle.goto(x,y)
turtle.penup()

turtle.end_fill()

第二步,更多涂色

然后,我们再来画多边形,给每个三角形不同的颜色。

我们用到的库是:colorsys

The colorsys module defines bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) color space used in computer monitors and three other coordinate systems: YIQ, HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value). Coordinates in all of these color spaces are floating point values. In the YIQ space, the Y coordinate is between 0 and 1, but the I and Q coordinates can be positive or negative. In all other spaces, the coordinates are all between 0 and 1.

我们使用其中的函数:

colorsys.hsv_to_rgb(h, s, v)
Convert the color from HSV coordinates to RGB coordinates.

问:为什么用这个hsv转rgb?

答:HSV分别是色调、饱和度和亮度,其中H代表颜色信息。

如果把S和V固定,那么调整H就可以调整颜色,比较简单。

H,S,V ∈  [0, 1]

代码变一下:

def draw_gon(length, start_angle, line):
    angle = 360//line
    for index in range(line):
        (r,g,b) = colorsys.hsv_to_rgb(index/line,1,1)
        turtle.color((r,g,b))
        turtle.goto(0, 0)
        turtle.begin_fill()        
        turtle.pendown()
        turtle.seth(start_angle + angle*index)
        turtle.fd(length)
        (x,y)=turtle.pos()
        turtle.penup()
        turtle.goto(0, 0)
        turtle.pendown()
        turtle.seth(start_angle+angle*(index+1))
        turtle.fd(length)
        turtle.goto(x,y)
        turtle.penup()
        turtle.end_fill()

彩色五边形示例:

9adae92a392b1e2c5637f2a642d670aa.png
彩色五边形
692711b293ae91894e33c63c5714dc34.png
彩色360边形

第三步,动起来

参考第一个教程中,让直线旋转起来的方式,让它动起来。

每次刷新的时候,我们改变多边形的角度。

最简单的代码:

for angle in range(360):
    turtle.clear()
    draw_gon(400, angle, 60)
    turtle.update()
    time.sleep(0.05)

ontimer版本:

angle=1
length=400

def movie_gon():
    global angle
    turtle.clear()
    draw_gon(length, angle, 60)
    angle += 1
    if angle > 360:
        angle = 0
    turtle.ontimer(movie_gon, 0)
    
movie_gon()

--EOF--

例行求粉,谢谢!

6787198566c1f07baa4f150bff08d85c.png e9d396953296ae6346559fb011e344b0.gif
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值