简体   繁体   English

Pygame - 找到给定斜边和角度的 x 和 y

[英]Pygame - find x and y given hypotenuse and angle

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.所以我正在学习 python 教程,我需要通过给定的斜边 = 40 和角度 = 45 度来找到 x 和 y。 Can anyone explain how to find the x and y?谁能解释如何找到x和y? 三角形

Here is the code in python.这是python中的代码。 Look at the movePlayer function.查看 movePlayer 函数。 Finding the new x and y is done differently.寻找新的 x 和 y 的方式不同。 The angle is first subtracted from 180 and then divided by 2 which doesn't make sense yet it work.该角度首先从 180 中减去,然后除以 2,这没有意义,但它仍然有效。

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()

Somewhat basic trigonometry (assuming ang is in radians and zero angle points towards +x).有点基本的三角学(假设 ang 是弧度,零角度指向 +x)。

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

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.为了得到 x,y 坐标,从 (x,y) 画一条直线与 x 轴成直角是很方便的。 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)左下角是 90 度(这意味着它是一个等腰直角三角形)
  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).使用勾股定理,我们得到 x = - (40 / sqrt(2)) 和 y = 40 / sqrt(2)。

To use the trigonometry, we get:要使用三角函数,我们得到:

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

In your case, you have an angle of 45 degrees, consequently you have a right isosceles triangle.在您的情况下,您的角度为 45 度,因此您有一个直角等腰三角形。

The general Pythagoras formula is : h^2 = x^2 + y^2.一般的毕达哥拉斯公式是: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM