Home > Enterprise >  Pygame - find x and y given hypotenuse and angle
Pygame - find x and y given hypotenuse and angle

Time:11-11

So I am going over a tutorial in python where I need to find x and y by given hypotenuse = 40 and the angle = 45 deg. Can anyone explain how to find the x and y? triangle

Here is the code in python. Look at the movePlayer function. Finding the new x and y is done differently. The angle is first subtracted from 180 and then divided by 2 which doesn't make sense yet it work.

import pygame, math

pygame.init()

def movePlayer(direction, radius, absRot):
    yChange = 5
    # how many degrees to move in either side

    deltaTheta = int(90/(radius / yChange))

    if direction == 'left':
        deltaTheta *= -1

    # convert degrees to radians
    finalRot = (absRot   deltaTheta) * math.pi / 180

    hypotenuse = (radius * math.sin(finalRot) / (math.sin((math.pi - finalRot) / 2)))
    
    # why these work
    newX = hypotenuse * math.cos(math.pi/2-(math.pi - finalRot)/2)
    newY = hypotenuse * math.sin(math.pi/2-(math.pi - finalRot)/2)
    
    # these don't work
    #newX = hypotenuse * math.cos(math.pi/2-finalRot)
    #newY = hypotenuse * math.sin(math.pi/2-finalRot)


    return newX, newY,absRot   deltaTheta


WIDTH = 900
HEIGHT = 700
SCREEN_SIZE = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(SCREEN_SIZE)


player = pygame.Surface((64,64))

playerStart = player
currentRotation = 0

ball = pygame.Surface((32,32))
ball.fill('red')

playerX = WIDTH // 2
playerY = 530
playerXOriginal = playerX
playerYOriginal = playerY

ballX = WIDTH // 2 - ball.get_rect().width // 2
ballY = 450 - ball.get_rect().height // 2

radius = playerY - ballY

FPS = 30
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    pressedKeys = pygame.key.get_pressed()
    if pressedKeys[pygame.K_LEFT]:
        changeX, changeY, currentRotation = movePlayer('left', radius, currentRotation)
        playerX = playerXOriginal   changeX
        playerY = playerYOriginal - changeY
        print('left')
    elif pressedKeys[pygame.K_RIGHT]:
        changeX, changeY, currentRotation = movePlayer('right', radius, currentRotation)
        playerX = playerXOriginal   changeX
        playerY = playerYOriginal - changeY
        print('right')

    
    screen.fill('white')
    screen.blit(ball, (ballX,ballY))
    screen.blit(player, (playerX - player.get_rect().width //2,playerY - player.get_rect().height // 2))
    
    pygame.display.update()
    clock.tick(FPS)

pygame.quit()

CodePudding user response:

Somewhat basic trigonometry (assuming ang is in radians and zero angle points towards x).

x = cos(ang) * h
y = sin(ang) * h

CodePudding user response:

In your case, you have an angle of 45 degrees, consequently you have a right isosceles triangle.

The general Pythagoras formula is : h^2 = x^2 y^2. Since the triangle is isosceles, then :

x = y => h^2 = 2 * x^2 => x = y = (sqrt(2)/2)*h

In a more general case, where the triangle is not isosceles:

x = cos(ang) * h
y = sin(ang) * h

In python:

import math 
def triangleSides(hypotenuse,angleindegrees):
    angleinradians  = (angleindegrees * math.pi / 180)
    (x,y) = hypotenuse* math.cos(angleinradians),hypotenuse* math.sin(angleinradians)
    return (x,y)

CodePudding user response:

In order to get the x,y coordinates, it's convenient to draw a straight line from (x,y) to make a right angle with the x-axis. Given the diagram, I will make the following two assumptions:

  1. That the bottom left angle is 90 degree (which means it's an isosceles right triangle)
  2. That the bottom right angle of the triangle is at the origin (the blue dot in the diagram).

Using the Pythagorean theorem, we get that x = - (40 / sqrt(2)) and y = 40 / sqrt(2).

To use the trigonometry, we get:

a, h = math.pi/4, 40
x, y = - h * cos(a), h * sin(a)
  • Related