Python:尝试将结果和角度转换为原始的i和j值。

2024-10-01 13:44:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让python得到一个结果值和角度值,以返回向量的原始i和j值。我可以让它从I和j值到合力和角度,但这是棘手的另一方面。你知道吗

    #vector conversion (x,y) to resultant and angle#
import math

menu = input('Vector Conversion: for vector to (x,y) press 1. \n                   for (x,y) to vector press 2.')
if menu == '2':
    user_distancex = float(input('What is the distance in x-direction?'))
    user_distancey = float(input('What is the distance in y-direction?'))

    r = (math.sqrt(user_distancex**2 + user_distancey**2))  
    theta = math.atan(user_distancey/user_distancex)*(180/math.pi)

    print(r, 'feet',theta, 'degrees')
elif menu == '1':

    user_angle = float(input('What is the angle of your vector?'))
    user_resultant = float(input('What is the distance (resultant) of your vector'))
    x_dist = user_resultant*math.cos(math.degrees(user_angle))
    y_dist = user_resultant*math.sin(math.degrees(user_angle))
    print((x_dist,y_dist))

Tags: thetoinputisdistmathfloatwhat
1条回答
网友
1楼 · 发布于 2024-10-01 13:44:57

math.degrees(user_angle)将用户角度从弧度转换为度。但是如果用户以度为单位输入它,它已经是以度为单位的了!你知道吗

您应该改用math.radians(user_angle)。你知道吗

它还有助于澄清您的输入提示:

user_angle = float(input('What is the angle of your vector (in degrees)?'))

相关问题 更多 >