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

2024-03-29 01:11:59 发布

您现在位置:Python中文网/ 问答频道 /正文

在看了一些编码训练之后,我试着用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


Tags: thepositionlocationwindowwidthpygamedotint
1条回答
网友
1楼 · 发布于 2024-03-29 01:11:59

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

^{}将坐标转换为整数值,并仅将唯一坐标添加到容器中。注意,像素的坐标是整数。画一个点两次,并不能使它“更白”。你知道吗

使用^{}而不是列表来存储唯一的位置

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)

相关问题 更多 >