Frank Cold
Frank Cold

Reputation: 35

Python - Pygame: "Co-ordinates of square on sinus"

I have problem with this code, cause I want 4 points, (which are circles) to make vertex of square, but I don't know what the difference should be between those vertexes (variable "change"). I left this variable empty, please, can you give me value I should insert there and explain why?

Here's the code:

import pygame
from math import sin, cos

pygame.init()

screen = pygame.display.set_mode((800,600))

BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (0,0,255)
GRAY = (175,175,175)

clock = pygame.time.Clock()
Font = pygame.font.SysFont(None, 50)

angle = 0
angle_c = 0
ex = False

a = (0,0)
b = (0,0)
c = (0,0)
d = (0,0)

change = 

size = 95

x_c = 400
y_c = 200

while not ex:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             ex = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                angle_c = 0.05
            if event.key == pygame.K_RIGHT:
                angle_c = -0.05
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                angle_c = 0

    angle += angle_c

    a = (round(sin(angle)*size+x_c), round(cos(angle)*size+y_c))
    b = (round(sin(angle+change)*size+x_c), round(cos(angle+change)*size+y_c))
    c = (round(sin(angle+change*2)*size+x_c), round(cos(angle+change*2)*size+y_c))
    d = (round(sin(angle+change*3)*size+x_c), round(cos(angle+change*3)*size+y_c))

    screen.fill(WHITE)

    pygame.draw.circle(screen, BLUE, (400,200), round(sin(360)*100), 3)
    pygame.draw.circle(screen, BLUE, a, 10)
    pygame.draw.circle(screen, WHITE, a, 8)

    pygame.draw.circle(screen, BLUE, b, 10)
    pygame.draw.circle(screen, WHITE, b, 8)

    pygame.draw.circle(screen, BLUE, c, 10)
    pygame.draw.circle(screen, WHITE, c, 8)

    pygame.draw.circle(screen, BLUE, d, 10)
    pygame.draw.circle(screen, WHITE, d, 8)

    pygame.display.update()

    clock.tick(50)


pygame.quit()

Upvotes: 0

Views: 280

Answers (1)

Blckknght
Blckknght

Reputation: 104852

The angles from the center of a square to the square's vertices differ by 90 degrees, or pi / 2 radians (which is the unit expected by Python's sin and cos functions).

So you could set your change variable to could be pi / 2 (after adding pi to the list of names to import from the math module), and your code would probably work.

But it's even easier to calculate the coordinates than that, as rotations by 90 degrees change sine and cosine values in a predictable way:

  • sin(a + pi/2) is cos(a)
  • cos(a + pi/2) is -sin(a).

Applying this transformation repeatedly lets you figure out what the sines and cosines should be after further rotations.

You only need to call sin and cos once each and then you can use the values to find all the coordinates.

y = round(sin(angle) * size)
x = round(cos(angle) * size)

a = ( x + x_c,  y + y_c)
b = (-y + x_c,  x + y_c)
c = (-x + x_c, -y + y_c)
d = ( y + x_c, -x + y_c)

Note that in the code above I'm following the mathematical convention that angles start at zero along the positive x-axis and increase as you initially start rotating towards the positive y-axis (which will be clockwise in pygame's coordinate system). Your previous code seemed to measure angles from the positive y-axis and increase in the opposite direction. If that's what you really want, you can simply reverse x and y in the initial assignments (assign the cosine to y and the sine to x).

Upvotes: 1

Related Questions