如何在Lissajous曲线表中获取所有曲线的X和Y位置?

问题描述 投票:1回答:1

在看了一些Coding Train之后,我试图在python中创建一个Lissajous曲线表。我成功地制作了圆圈,轨道圆点和线条。但是,我似乎无法得到实际绘制的曲线。我创建了一个名为positions的列表,它从行和列中获取x_和y_值,但动画只绘制右下角。我无法弄清楚我的错误。我在GitHub上的完整代码:LissajousCurveTable

    width, height = 800, 800
name_of_window = ""
pygame.init()
window = pygame.display.set_mode((width, height))
pygame.display.set_caption(name_of_window)
clock = pygame.time.Clock()
angle = 1
circle_diameter = int(width / 10)
columns = int(width / circle_diameter) - 1
rows = int(height / circle_diameter) - 1
circle_diameter_draw = circle_diameter - 10
r = circle_diameter_draw / 2
position = []

is_running = True

while is_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False

    window.fill((0, 0, 0))

    for column in range(columns):
        # the circle x location
        cx = circle_diameter + column * circle_diameter + int(circle_diameter_draw / 2)
        # the circle y location
        cy = circle_diameter_draw / 2 + circle_diameter_draw / 10
        # the dot x location
        x = r * math.cos(angle * (column + 1))
        # the dot y location
        y = r * math.sin(angle * (column + 1))
        # draws circle
        pygame.draw.circle(window, (255, 255, 255), [cx, int(cy)], int(r), 1)
        # draws dot
        pygame.draw.circle(window, (255, 255, 255), [int(x + cx), int(y + cy)], 5)
        # draws line from dot pos
        pygame.draw.line(window, (255, 255, 255), (cx + x, cy + y), (cx + x, height), 1)
        angle += 0.001
        # adds the x
        x_ = cx + x

    for row in range(rows):
        # the circle y location
        cy = circle_diameter + row * circle_diameter + int(circle_diameter_draw / 2)
        # the circle x location
        cx = circle_diameter_draw / 2 + circle_diameter_draw / 10
        # the dot x location
        x = r * math.cos(angle * (row + 1))
        # the dot y location
        y = r * math.sin(angle * (row + 1))
        # draws circle
        pygame.draw.circle(window, (255, 255, 255), [int(cx), int(cy)], int(r), 1)
        # draws dot
        pygame.draw.circle(window, (255, 255, 255), [int(x + cx), int(y + cy)], 5)
        # draws line from dot pos
        pygame.draw.line(window, (255, 255, 255), (cx + x, cy + y), (width, cy + y), 1)
        angle += 0.001
        y_ = cy + y

    # adds the values to the
    position.append([x_, y_])

    for i in range(len(position)):
        pygame.draw.circle(window, (255, 255, 255), (int(position[i][0]), int(position[i][1])), 1)

Picture of Program

python animation pygame geometry position
1个回答
1
投票

您需要将每帧中计算的位置(_x, _y)的排列添加到容器position,而不是每帧单个位置。

round()坐标为整数值,只为容器添加唯一坐标。注意,像素的坐标是整数。两次画一点,不会使它“更白”。

使用qazxsw poi而不是列表来存储唯一的位置

set()

position = set() while is_running: # [...] lx_ = [] for column in range(columns): # [...] # adds the x lx_.append(int(round(cx + x))) ly_ = [] for row in range(rows): # [...] # adds the y ly_.append(int(round(cy + y))) # adds the values to the position.update([(x_, y_) for x_ in lx_ for y_ in ly_]) for pos in position: pygame.draw.circle(window, (255, 255, 255), pos, 1)

© www.soinside.com 2019 - 2024. All rights reserved.