Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Made by Folufi
- import pygame, math
- pygame.init()
- pygame.font.init()
- #Color
- red = (255, 0, 0)
- yellow = (255, 255,0)
- green = (0, 255, 0)
- blue = (0, 0, 255)
- black = (0, 0, 0)
- white = (255, 255, 255)
- #Window configs
- resolution = (800, 600)
- screen = pygame.display.set_mode(resolution)
- title = pygame.display.set_caption("Trigonometry circle")
- clock, fps = pygame.time.Clock(), 60
- #Rotation matrice
- def rotate(x, y, deg):
- s = math.sin(deg/180*math.pi)
- c = math.cos(deg/180*math.pi)
- if deg == 90 or deg == -90:
- s = 1
- elif deg == 270 or deg == -270:
- s = -1
- if deg == 180 or deg == -180:
- c = -1
- elif deg == 0 or deg == 360:
- c = 1
- return x*c-y*s, x*s+y*c
- def create_text(text, pos, size, color):
- font = pygame.font.SysFont("Calibri", size)
- text_surf = font.render(text, 1, color)
- text_rect = text_surf.get_rect()
- text_rect.center = pos
- screen.blit(text_surf, text_rect)
- #App
- def app():
- closed = False
- #Coordinations
- pos = (200, 0) #Radius coordination
- angle = 0
- points = []
- while not closed:
- screen.fill(black)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- closed = True
- #Trigonometry
- pos2 = rotate(pos[0], pos[1], angle)
- angle += .4 #You can change rotation speed here!
- s, c, t = math.sin(angle/180*math.pi), math.cos(angle/180*math.pi), math.tan(angle/180*math.pi)
- #Axis
- pygame.draw.line(screen, white, (400-250, 300), (400+250, 300)) #X axis
- pygame.draw.line(screen, white, (400, 300-250), (400, 300+250))#Y axis
- #Line and circle
- pos3 = rotate(pos[0]/4, pos[1], angle)
- points.append(pos3)
- for x, y in points:
- pygame.draw.line(screen, white, (x+400, y+300), (x+400, y+300), 1)
- pygame.draw.line(screen, green, (400, 300),(pos2[0]+400, pos2[1]+300))#Rotating line
- pygame.draw.circle(screen, white, (400, 300), 200, 1)
- pygame.draw.line(screen, red, (pos2[0]+400, pos2[1]+300), (pos2[0]+400, (pos2[1]+300)-(pos2[1]+300 - 300)))#Sine line
- 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
- #Texts
- create_text("Sine: "+str(s*-1), (630, 485), 15, red) #Sine
- create_text("Cosine: "+str(c), (630, 500), 15, blue) #Cosine
- create_text("Tangent: "+str(t*-1), (630, 515), 15, yellow) #Tangent
- create_text("Angle: "+str(round(angle*-1, 2))+"(degrees) / "+str(round((angle*-1/180*math.pi), 4))+"(radians)", (630, 530), 15, white)
- #Angle adjustment
- if angle > 360:
- angle -= 360
- points = []
- elif angle < -360:
- angle += 360
- points = []
- pygame.display.flip()
- clock.tick(fps)
- app()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement