Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Noorquacker
Created October 14, 2019 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noorquacker/faf06e401d2abef2afee2dd747d5ce7b to your computer and use it in GitHub Desktop.
Save Noorquacker/faf06e401d2abef2afee2dd747d5ce7b to your computer and use it in GitHub Desktop.
Pygame demo of the Fourier series
import pygame, time, sys, pygame.gfxdraw, math
print("Fourier Example thingy by Noorquacker. https://www.nqind.com")
pygame.init()
clock = pygame.time.Clock()
w = pygame.display.set_mode((1500,1000))
pygame.font.init()
pygame.display.set_caption("fourier example thingy")
font = pygame.font.SysFont("Times New Roman", 18)
iter = 5
circ_outs = [0]*500
ys = list([0]*750)
tick = 0
rate = 300
while True:
tick += 1
w.fill((0,0,0))
w.blit(font.render("FPS: " + str(round(clock.get_fps(),2)), True, (255,255,255), None),(2,2))
w.blit(font.render("Iterations: " + str(iter), True, (255,255,255), None), (2,20))
for i in range(0,iter):
rx, ry = 0, 0
if i != 0:
rx, ry = circ_outs[i-1]
color = int((iter-i)/iter*255)
pygame.gfxdraw.aacircle(w, round(rx+500), round(ry+500), round(400/((2*i+1)*math.pi)), (0,color,color))
theta = (2*i+1)*tick/rate
circ_outs[i] = (rx + 400*math.cos(theta)/((2*i+1)*math.pi), ry + 400*math.sin(theta)/((2*i+1)*math.pi))
pygame.gfxdraw.line(w, round(rx+500), round(ry+500), round(circ_outs[i][0]+500), round(circ_outs[i][1]+500), (color,color,color))
if i == iter-1:
x, y = int(circ_outs[i][0]), int(circ_outs[i][1])
pygame.gfxdraw.line(w, 700, y+500, x+500, y+500, (255,0,0))
if tick % int(rate/100) == 0:
ys.insert(0, circ_outs[iter-1][1])
del ys[-1]
for i in range(0,len(ys)):
pygame.gfxdraw.pixel(w, i+700, round(ys[i])+500, (0,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and iter < 500:
iter += 1
if event.key == pygame.K_DOWN and iter > 1:
iter -= 1
clock.tick(rate)
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment