Advertisement
Folufi

Trigonometry Circle

Jul 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #Made by Folufi
  2. import pygame, math
  3.  
  4. pygame.init()
  5. pygame.font.init()
  6. #Color
  7. red = (255, 0, 0)
  8. yellow = (255, 255,0)
  9. green = (0, 255, 0)
  10. blue = (0, 0, 255)
  11. black = (0, 0, 0)
  12. white = (255, 255, 255)
  13.  
  14. #Window configs
  15. resolution = (800, 600)
  16. screen = pygame.display.set_mode(resolution)
  17. title = pygame.display.set_caption("Trigonometry circle")
  18. clock, fps = pygame.time.Clock(), 60
  19.  
  20. #Rotation matrice
  21. def rotate(x, y, deg):
  22.     s = math.sin(deg/180*math.pi)
  23.     c = math.cos(deg/180*math.pi)
  24.    
  25.     if deg == 90 or deg == -90:
  26.         s = 1
  27.     elif deg == 270 or deg == -270:
  28.         s = -1
  29.        
  30.     if deg == 180 or deg == -180:
  31.         c = -1
  32.     elif deg == 0 or deg == 360:
  33.         c = 1
  34.    
  35.     return x*c-y*s, x*s+y*c
  36.  
  37. def create_text(text, pos, size, color):
  38.     font = pygame.font.SysFont("Calibri", size)
  39.     text_surf = font.render(text, 1, color)
  40.     text_rect = text_surf.get_rect()
  41.     text_rect.center = pos
  42.    
  43.     screen.blit(text_surf, text_rect)
  44.  
  45. #App
  46. def app():
  47.     closed = False
  48.    
  49.     #Coordinations
  50.     pos = (200, 0) #Radius coordination
  51.     angle = 0
  52.    
  53.     points = []
  54.     while not closed:
  55.         screen.fill(black)
  56.         for event in pygame.event.get():
  57.             if event.type == pygame.QUIT:
  58.                 closed = True
  59.        
  60.         #Trigonometry
  61.         pos2 = rotate(pos[0], pos[1], angle)
  62.         angle += .4 #You can change rotation speed here!
  63.         s, c, t = math.sin(angle/180*math.pi), math.cos(angle/180*math.pi), math.tan(angle/180*math.pi)
  64.        
  65.         #Axis
  66.         pygame.draw.line(screen, white, (400-250, 300), (400+250, 300)) #X axis
  67.         pygame.draw.line(screen, white, (400, 300-250), (400, 300+250))#Y axis
  68.        
  69.         #Line and circle
  70.         pos3 = rotate(pos[0]/4, pos[1], angle)
  71.         points.append(pos3)
  72.        
  73.         for x, y in points:
  74.             pygame.draw.line(screen, white, (x+400, y+300), (x+400, y+300), 1)
  75.            
  76.        
  77.         pygame.draw.line(screen, green, (400, 300),(pos2[0]+400, pos2[1]+300))#Rotating line
  78.         pygame.draw.circle(screen, white, (400, 300), 200, 1)
  79.        
  80.         pygame.draw.line(screen, red, (pos2[0]+400, pos2[1]+300), (pos2[0]+400, (pos2[1]+300)-(pos2[1]+300 - 300)))#Sine line
  81.         pygame.draw.line(screen, blue, (pos2[0]+400, (pos2[1]+300)-(pos2[1]+300 - 300)), (400, (pos2[1]+300)-(pos2[1]+300 - 300)))#Cosine line
  82.        
  83.         #Texts
  84.         create_text("Sine: "+str(s*-1), (630, 485), 15, red) #Sine
  85.         create_text("Cosine: "+str(c), (630, 500), 15, blue) #Cosine
  86.         create_text("Tangent: "+str(t*-1), (630, 515), 15, yellow) #Tangent
  87.        
  88.         create_text("Angle: "+str(round(angle*-1, 2))+"(degrees) / "+str(round((angle*-1/180*math.pi), 4))+"(radians)", (630, 530), 15, white)
  89.        
  90.        
  91.         #Angle adjustment
  92.         if angle > 360:
  93.             angle -= 360
  94.             points = []
  95.         elif angle < -360:
  96.             angle += 360
  97.             points = []
  98.        
  99.         pygame.display.flip()
  100.         clock.tick(fps)
  101. app()
  102. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement