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

时间:2019-03-09 10:47:22

标签: python animation pygame geometry position

看了一些编码培训后,我尝试用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

1 个答案:

答案 0 :(得分:1)

您必须将在每个帧中计算出的位置(_x, _y)的排列添加到容器position中,而不是在每个帧中添加一个位置。

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

使用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)