用乌龟绘制辐射圆形梯形图案 [英] Drawing radiating circular trapezoid pattern with Turtle

查看:56
本文介绍了用乌龟绘制辐射圆形梯形图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何在我的海龟代码中绘制一些梯形.

I was just wondering how to draw some trapezoids in my turtle code.

我希望我的输出是这样的:

I want my output to be like this:

我现在的输出是什么:

这是我目前编写的代码:

Here's the code I've written so far:

import turtle as trtl

num_sides = 6
side_length = 15

circumradius = side_length

trtl.pencolor((245, 176, 66))
trtl.pensize(8)

for move_turtle in range(1):
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2

side_length = side_length + 8


trtl.pensize(12)

for move_turtle in range(1):
    trtl.pencolor((255, 83, 71))
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2

for move_turtle in range(1):
    trtl.pencolor((247, 220, 67))
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2


trtl.hideturtle()

我可以使用哪些技术来获得所需的输出?

What techniques can I use to get the desired output?

推荐答案

当给定这样的模式时,我经常采取的方法是考虑一个简单的技巧,它可以比弄清楚如何产生结果更容易画一些像梯形这样可能很挑剔的东西.

The approach I often take when given patterns like this is to think about a slight-of-hand trick that can produce the result with less trouble than figuring out how to draw something potentially fussy like a trapezoid.

在这种情况下,如果您移除空白区域,我们会得到一个从中间辐射出不同纯色的饼"形圆圈.如果我们能够设法解决这个更简单的问题,那么我们就可以在顶部绘制白线并最终得到所需的结果.

In this case, if you remove the white spaces, we have a "pie"-shaped circle of different solid colors radiating from the middle. If we can manage to solve that simpler problem, then we can draw white lines on top and wind up with the desired result.

我们需要的唯一触发是角坐标公式:

The only trig we need is the angle-to-coordinates formula:

x = cos(radians(angle)) * radius
y = sin(radians(angle)) * radius

使用它,我们可以沿着带有循环的圆的圆周迭代 n 个点(带有一些旋转偏移)并填充颜色以制作饼图:

Using this, we can iterate over n points (with some rotational offset) along the circumference of a circle with a loop and fill in the colors to make a pie:

import math
import turtle

def draw_pie(t, r, n, colors, rot_offset=0.5):
    for i in range(n + 1):
        a = 360 / n * (i + rot_offset)
        t.color(colors[i%len(colors)])
        t.begin_fill()
        t.goto(0, 0)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        t.goto(x, y)
        t.end_fill()

if __name__ == "__main__":
    t = turtle.Turtle()
    t.screen.setup(540, 540)
    t.penup()
    t.speed("fastest")
    sides = 6
    draw_pie(t, 450, sides, ["#dd2", "orange", "#d02"])
    t.ht()
    turtle.exitonclick()

接下来,我们需要绘制一堆带有方角的粗白线.不幸的是,海龟默认绘制圆边,这不适合我们的需要.制作方边有许多技巧(冲压是一种合理的方法).我在这里使用的方法是编写一个自定义矩形绘制函数,它利用了 turtle.distanceturtle.setheading(turtle.towards(x, y)) 这让我可以指向圆周上的下一个点.剩下的就是计算角度和选择正确值的循环.

Next, we need to draw a bunch of thick white lines with square corners. Unfortunately, turtle draws rounded edges by default which aren't suitable for our needs. There are many tricks for making square edges (stamping is a reasonable method). The approach I used here was writing a custom rectangle drawing function that takes advantage of turtle.distance and turtle.setheading(turtle.towards(x, y)) which lets me point to the next point along the circumference of the circle. The rest is just loops to compute angles and picking the right values.

代码如下:

import math
import turtle

def draw_rect(t, x, y, xx, yy, width):
    t.goto(x, y)
    t.setheading(t.towards(xx, yy))
    t.begin_fill()
    
    for d in [t.distance(xx, yy), width] * 2:
        t.forward(d)
        t.left(90)

    t.end_fill()

def draw_pie(t, r, n, colors, rot_offset=0.5):
    for i in range(n + 1):
        a = 360 / n * (i + rot_offset)
        t.color(colors[i%len(colors)])
        t.begin_fill()
        t.goto(0, 0)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        t.goto(x, y)
        t.end_fill()

def draw_reg_polygon(t, r, n, thickness, rot_offset=0.5):
    for i in range(n):
        a = 360 / n * (i + rot_offset)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        a = 360 / n * (1 + i + rot_offset)
        xx = math.cos(math.radians(a)) * r
        yy = math.sin(math.radians(a)) * r
        draw_rect(t, x, y, xx, yy, thickness)

if __name__ == "__main__":
    t = turtle.Turtle()
    t.screen.setup(540, 540)
    t.penup()
    t.speed("fastest")
    sides = 6
    draw_pie(t, 450, sides, ["#dd2", "orange", "#d02"])
    t.color("white")

    for r in range(50, 500, 100):
        draw_reg_polygon(t, r, sides, 45)

    t.ht()
    turtle.exitonclick()

这篇关于用乌龟绘制辐射圆形梯形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆