如何从这个正弦波中去除直线

How to remove the straight line from this sine wave

如何删除穿过这个正弦波中间的线?

import pygame, sys, math
from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()

points = []

screen = pygame.display.set_mode((500, 500))

while True:
    clock.tick(60)
    screen.fill((0, 0, 0))

    # Draw Sine Wave
    for i in range(500):
        n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
        points.append([i, n])

    pygame.draw.lines(screen, (0, 255, 55), False, points, 2)

    # Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
    pygame.display.update()

你有两个错误

  1. 使用False代替True绘制开放多边形

     pygame.draw.lines(screen, (0, 255, 55), False, points, 2)
    
  2. 在每个循环中添加新点到列表,然后在最后一个点 (499,0) 之后你有 (0,0) 从右到左创建垂直线。如果您使用 print(len(point)) 就可以看到它 - 您将获得值 500、1000、1500 等。您应该在每个循环中清除 points。或者你应该只生成一次 points - 在 while 循环之前。


在创建新列表之前清除 points - 如果您想要动画波形,它会很有用。

while True:
    clock.tick(60)
    screen.fill((0, 0, 0))

    points = []  # <--- clear it

    # Draw Sine Wave
    for i in range(500):
        n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
        points.append([i, n])

    pygame.draw.lines(screen, (0, 255, 55), False, points, 1)

    # Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
    pygame.display.update()

或者在循环之前生成它 - 如果你想要静态波

# generate it only once - before `while` loop

points = []

# Draw Sine Wave
for i in range(500):
    n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
    points.append([i, n])
    
while True:
    clock.tick(60)
    screen.fill((0, 0, 0))

    pygame.draw.lines(screen, (0, 255, 55), False, points, 1)

    # Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

    pygame.display.update()

编辑:

使用 offset 动画波的版本

import sys
import math
import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((500, 500))

offset = 0

clock = pygame.time.Clock()

while True:

    # - all updates (without draws) -

    points = []

    # Draw Sine Wave
    for i in range(500):
        n = int(math.sin((i+offset) / 500 * 4 * math.pi) * 50 + 240)
        points.append([i, n])

    offset += 1

    # - all draws (without updates) -

    clock.tick(60)
    screen.fill((0, 0, 0))

    pygame.draw.lines(screen, (0, 255, 55), False, points[:500], 1)

    pygame.display.update()
        
    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()