在观看了一些编码火车之后,我尝试用 python 制作一个利萨如曲线表。我成功地制作了圆圈、绕轨道运行的点和线条。 但是,我似乎无法绘制实际的曲线。我创建了一个名为 Position 的列表,它从行和列中获取 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)
最佳答案
您必须将每帧中计算出的位置(_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)
但是,通过将曲线写入表面而不是存储在集合中可以获得最佳性能改进。
创建 pygame.Surface
的网格对象:
surf = pygame.Surface((tile_size, tile_size))
grid = [[surf.copy() for i in range(columns)] for j in range(rows)]
在每个帧的每个表面上绘制一个点:
for column, cx, x, y_ in lx:
for row, cy, x_, y in ly:
grid[column][row].set_at((x + tile_size // 2, y + tile_size // 2), (255, 255, 255))
在每个帧中绘制表面的网格:
for column, grid_row in enumerate(grid):
for row, cell_surf in enumerate(grid_row):
cx = (column + 1) * tile_size
cy = (row + 1) * tile_size
window.blit(cell_surf, (cx, cy))
最小示例: repl.it/@Rabbid76/PyGame-LissajousCurve
另请参阅Draw 2D
import math
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
angle = 0
tile_size = window.get_width() // 10
columns = window.get_width() // tile_size - 1
rows = window.get_height() // tile_size - 1
radius = (tile_size - 10) // 2
surf = pygame.Surface((tile_size, tile_size))
surf.fill((32, 0, 32))
grid = [[surf.copy() for i in range(columns)] for j in range(rows)]
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
lx = [(c-1, c*tile_size + tile_size//2, round(radius * math.cos(angle*c)), round(radius * math.sin(angle*c))) for c in range(1, columns+1)]
ly = [(r-1, r*tile_size + tile_size//2, round(radius * math.cos(angle*r)), round(radius * math.sin(angle*r))) for r in range(1, rows+1)]
angle += 0.01
for column, cx, x, y_ in lx:
for row, cy, x_, y in ly:
grid[column][row].set_at((x + tile_size // 2, y + tile_size // 2), (255, 255, 255))
window.fill((0, 0, 0))
for column, grid_row in enumerate(grid):
for row, cell_surf in enumerate(grid_row):
cx = (column + 1) * tile_size
cy = (row + 1) * tile_size
window.blit(cell_surf, (cx, cy))
cy = tile_size // 2
for column, cx, x, y in lx:
pygame.draw.circle(window, (127, 127, 127), (cx, cy), radius, 1)
pygame.draw.circle(window, (127, 127, 127), (x + cx, y + cy), 5)
pygame.draw.line(window, (127, 127, 127), (cx + x, cy + y), (cx + x, window.get_height()), 1)
cx = tile_size // 2
for row, cy, x, y in ly:
pygame.draw.circle(window, (127, 127, 127), (cx, cy), radius, 1)
pygame.draw.circle(window, (127, 127, 127), (x + cx, y + cy), 5)
pygame.draw.line(window, (127, 127, 127), (cx + x, cy + y), (window.get_width(), cy + y), 1)
for pos in [(x[1] + x[2], y[1] + y[3]) for x in lx for y in ly]:
pygame.draw.circle(window, (255, 0, 0), pos, 3)
pygame.display.flip()
pygame.quit()
exit()
关于python - 如何获取利萨如曲线表中所有曲线的X和Y位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55076531/