This is a demonstration of a Standing wave produced by the contribution of two waves with the same aplitude and frequency, on opposing directions. The Standing wave equation:
import pygame
import math
import time
pygame.init()
WIDTH, HEIGHT = 1000, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Standing")
A = 50
T = 1
λ = 100
def displayText(text, size, color, x,y):
font = pygame.font.Font('freesansbold.ttf', size)
text = font.render(str(text), True, (color))
textRect = text.get_rect()
textRect.center = (x, y)
WIN.blit(text, textRect)
def drawballat(x, y, size):
pygame.draw.circle(WIN, (255, 255, 255), (x+WIDTH/2, y+HEIGHT/2), size)
def y(x,t):
return 2*A*math.cos(2*3.14*x/λ)*math.sin(2*3.14*t/T)
def renderScreen():
WIN.fill((0,0,0))
displayText("FPS: "+ str(round(FPS)), 10, (255,255,255), 20,20)
pygame.draw.line(WIN, (200, 200, 200), (0, (HEIGHT / 2)), (WIDTH, (HEIGHT / 2)))
pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2, 0), (WIDTH/2, HEIGHT))
for i in range(-WIDTH,WIDTH):
drawballat(i, y(i, totalFrames/45), 2)
totalFrames = 0
startTime = time.time
running = True
FPS = 0
prevFrame = 0
newFrame = 0
while running:
totalFrames+=1
newFrame = time.time()
FPS = 1/(newFrame-prevFrame)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
renderScreen()
pygame.display.update()
pygame.time.delay(16)
prevFrame = newFrame
pygame.quit()