-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourier.py
76 lines (65 loc) · 2.17 KB
/
fourier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pygame
import math
pygame.init()
windowwidth, windowheight = 955, 500
win = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.set_caption("Fourier Series")
clock = pygame.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
pi = math.pi
X, Y = windowwidth//2, 300
def drawpoints(points):
for point in points:
point[0] += 1
win.set_at(point, white)
def drawcircles():
tick = pygame.time.get_ticks() / 1000
centres = [(180, windowheight//2)]
freq = 20
xx , yy = 0, 0
for k in range(terms):
n = 2 * k + 1
radius = round(55 * (4 / (n * pi)))
angle = round(2 * pi * freq * tick * n)
xx = centres[k][0] + math.cos(math.radians(angle)) * radius
yy = centres[k][1] + math.sin(math.radians(angle)) * radius
pygame.draw.circle(win, white, centres[k], radius, 1)
xx = round(xx)
yy = round(yy)
pygame.draw.line(win, white, centres[k], (xx, yy), 1)
centres.insert(k + 1, (xx, yy))
pygame.draw.line(win, white, (xx, yy), (windowwidth//2, yy), 1)
Y = yy
pixels.append([X, Y])
def redrawGameWindow():
win.fill(black)
text = font.render(f'Terms = {terms}', 1, white)
win.blit(text, (windowwidth//2 - text.get_width() // 2, windowheight - 70))
drawcircles()
drawpoints(pixels)
pygame.display.update()
font = pygame.font.SysFont('bahnscrift', 30, True)
msg = pygame.font.SysFont('consolas', 18, True)
terms = 1
pixels = []
presscount = 0
run = True
while run:
clock.tick(120)
if presscount > 0:
presscount += 1
if presscount > 15:
presscount = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and terms < 60 and presscount == 0:
terms += 1
presscount += 1
if keys[pygame.K_DOWN] and terms >= 2 and presscount == 0:
terms -= 1
presscount += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redrawGameWindow()
pygame.quit()