【问题标题】:Drawing radiating circular trapezoid pattern with Turtle用 Turtle 绘制辐射圆形梯形图案
【发布时间】:2020-09-17 14:53:25
【问题描述】:

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

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

我现在的输出是什么:

这是我目前编写的代码:

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()

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

【问题讨论】:

标签: python turtle-graphics python-turtle


【解决方案1】:

当给定这样的图案时,我经常采用的方法是考虑一个简单的技巧,它可以比弄清楚如何绘制梯形等可能很挑剔的东西更容易产生结果。

在这种情况下,如果您删除空白,我们会得到一个从中间向外辐射的不同纯色的“饼”形圆圈。如果我们能够设法解决这个更简单的问题,那么我们可以在顶部画白线并最终得到想要的结果。

我们需要的唯一三角函数是角度坐标公式:

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

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

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 默认绘制圆角边缘,这不适合我们的需要。有many tricks 用于制作方形边缘(冲压是一种合理的方法)。我在这里使用的方法是编写一个自定义矩形绘图函数,该函数利用turtle.distance 和turtle.setheading(turtle.towards(x, y)),它可以让我指向圆周上的下一个点。其余的只是计算角度和选择正确值的循环。

代码如下:

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()

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    相关资源
    最近更新 更多