Search code examples
pythonvectorangle

Python : Trying to convert resultant and angle to original i and j values.


Im trying to get python to take a resultant value and angle value to return the original i and j values of a vector. I can get it to go from i and j values to resultant and angle but it is tricky the other way around.

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

Solution

  • math.degrees(user_angle) converts your user angle from radians to degrees. But it is already in degrees if the user inputs it in degrees!

    You should use math.radians(user_angle) instead.

    It also helps to clarify your input prompt:

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