繁体   English   中英

简化类似功能的Python Turtle绘图

[英]Simplify Python Turtle plotting of similar functions

有没有一种方法可以简化此功能,减少if语句并一遍又一遍地键入相同的代码?

此功能根据用户请求的数量创建正弦图。 有很多重复的代码。 我试图使用列表来存储对象,但是在尝试访问它们时出现错误。 我怎样才能解决这个问题?

 def createWaves(amp, count):
            even_num = [2, 4, 6, 8, 10, 12]
            alpha = createTurtle("turtle", "darkcyan", 2)
            alpha.ht()
            beta = createTurtle("turtle", "purple", 2)
            beta.ht()
            gamma = createTurtle("turtle", "orange", 2)
            gamma.ht()
            delta = createTurtle("turtle", "magenta", 2)
            delta.ht()
            epsilon = createTurtle("turtle", "deepskyblue", 2)
            epsilon.ht()
            zeta = createTurtle("turtle", "green", 2)
            zeta.ht()
            omega = createTurtle("turtle", "black", 3)
            omega.ht()
            if count == 0:
                alpha.st()
                for x in range(361):
                    y_alpha = (amp/even_num[0]) *(math.sin(math.radians(x*even_num[0])))
                    alpha.goto(x, y_alpha)
            elif count == 1:
                alpha.st()
                beta.st()
                omega.st()
                for x in range(361):
                    y_alpha = (amp/even_num[0]) *(math.sin(math.radians(x*even_num[0])))
                    y_beta = (amp/even_num[1]) *(math.sin(math.radians(x*even_num[1])))
                    y_omega = y_alpha + y_beta
                    alpha.goto(x, y_alpha)
                    beta.goto(x, y_beta)
                    omega.goto(x, y_omega)
            elif count == 2:
                alpha.st()
                beta.st()
                gamma.st()
                omega.st()
                for x in range(361):
                    y_alpha = (amp/even_num[0]) *(math.sin(math.radians(x*even_num[0])))
                    y_beta = (amp/even_num[1]) *(math.sin(math.radians(x*even_num[1])))
                    y_gamma = (amp/even_num[2]) *(math.sin(math.radians(x*even_num[2])))
                    y_omega = y_alpha + y_beta + y_gamma
                    alpha.goto(x, y_alpha)
                    beta.goto(x, y_beta)
                    gamma.goto(x, y_gamma)
                    omega.goto(x, y_omega)

这是一个重新实现,它避免了重复,并且适用于从1到EVEN_NUMBERSCOLORS的限制的EVEN_NUMBERS

import math
from turtle import Turtle, Screen

EVEN_NUMBERS = [2, 4, 6, 8, 10, 12]

COLORS = ["darkcyan", "purple", "orange", "magenta", "deepskyblue", "green", "black"]

def createWaves(amp, count):

    screen.tracer(False)

    for x in range(361):
        y_offsets = []

        for i in range(count):
            turtles[i].showturtle()

            y_offset = math.sin(math.radians(x * EVEN_NUMBERS[i])) * amp / EVEN_NUMBERS[i]

            turtles[i].goto(x, y_offset)

            y_offsets.append(y_offset)

        if count > 1:
                turtles[-1].showturtle()
                turtles[-1].goto(x, sum(y_offsets))

        screen.update()

    screen.tracer(True)

screen = Screen()

turtles = []

for color in COLORS:
    turtle = Turtle("turtle", visible=False)
    turtle.speed('fastest')
    turtle.color(color)
    turtle.width(2)

    turtles.append(turtle)

turtles[-1].width(3)  # summation turtle

createWaves(100, 4)

screen.exitonclick()

在此处输入图片说明

暂无
暂无

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

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