I wrote this code using Python 3.5 and Pygame to help me learn more about sine waves. Its goal is to show the variations in the sine and cosine values in the Trigonometry Circle as the distance covered by a point in the circumference increases.
I’d appreciate suggestions and advices about the code, specially about the math involved and my usage of Pygame.
Thanks.
import pygame import math SCR_WIDTH = 220 SCR_HEIGHT = 480 RAD_SIZE = 100 CIRCLE_X = int(105) CIRCLE_Y = int(285) STD_FONT_SZ = 25 def drawChart(frequency, time, turns, sin, cos, surface): """Draws the frequency, time, distance, and sine and cosine values on the given surface""" freq_txt = font.render("Frequency: {:.3}Hz".format(frequency), False, (0,0,0)) time_txt = font.render("Time: {:.3}s".format(time), False, (0,0,0)) turn_txt = font.render("Turns: {:.3}".format(turns), False, (0,0,0)) sin_txt = font.render("Sine: {:.3}".format(sin), False, (0,0,0)) cos_txt = font.render("Cosine: {:.3}".format(cos), False, (0,0,0)) surface.blit(time_txt,(0,0)) surface.blit(freq_txt,(0,STD_FONT_SZ)) surface.blit(sin_txt,(0,STD_FONT_SZ*2)) surface.blit(cos_txt,(0,STD_FONT_SZ*3)) surface.blit(turn_txt,(0,STD_FONT_SZ*4)) def drawCircle(x,y, surface): """Draws the Trigonometry circle on the given surface in position x, y""" pygame.draw.line(surface,(0,0,0),(x-RAD_SIZE,y),(x+RAD_SIZE,y)) #x-axis pygame.draw.line(surface,(0,0,0),(x,y-RAD_SIZE),(x,y+RAD_SIZE)) #y-axis pygame.draw.circle(surface,(0,0,0), (x,y), RAD_SIZE, 1) #main circle surface.blit(x_txt,(x+RAD_SIZE, y)) #x character surface.blit(y_txt,(x,y-RAD_SIZE-STD_FONT_SZ-4)) #y character pygame.init() pygame.display.set_caption("SineRep v. 1.0") screen = pygame.display.set_mode((SCR_WIDTH, SCR_HEIGHT), 0, 32) font = pygame.font.SysFont("arial",STD_FONT_SZ) frequency = 0.1 x_txt = font.render("x", False, (0,0,0)) y_txt = font.render("y", False, (0,0,0)) while True: screen.fill((255,255,255)) time = pygame.time.get_ticks()/1000 distance = 2*math.pi*time*frequency turns = time*frequency cos = math.cos(distance) sin = math.sin(distance) #size of cosine and sine in pixels cos_pxl = int(cos*RAD_SIZE) sin_pxl = int(-sin*RAD_SIZE) ##### Draw stuff in the screen drawChart(frequency, time, turns, sin, cos, screen) drawCircle(CIRCLE_X, CIRCLE_Y, screen) pygame.draw.circle(screen,(255,0,0), (CIRCLE_X+cos_pxl,CIRCLE_Y+sin_pxl), 5, 0) #dot #Auxiliary shapes pygame.draw.circle(screen,(255,0,0), (CIRCLE_X+cos_pxl,CIRCLE_Y), 3, 1) #cosine dot pygame.draw.circle(screen,(255,0,0), (CIRCLE_X,CIRCLE_Y+sin_pxl), 3, 1) #sine dot pygame.draw.line(screen,(255,0,0),(CIRCLE_X,CIRCLE_Y),(CIRCLE_X+cos_pxl,CIRCLE_Y), 2) #projection in x (cos) pygame.draw.line(screen,(255,0,0),(CIRCLE_X,CIRCLE_Y),(CIRCLE_X,CIRCLE_Y+sin_pxl),2) #projection in y (sine) event = pygame.event.poll() if event.type == pygame.QUIT: break pygame.display.update() pygame.quit()