With interface like that any user of Point class may choose convenient representation, no explicit conversions will be performed. All this ugly sines, cosines etc. will be hidden in one place. Point class. Only place where you should care which representation is used in computer memory.
import numpy as np
def pol2cart(r,theta):
'''
Parameters:
- r: float, vector amplitude
- theta: float, vector angle
Returns:
- x: float, x coord. of vector end
- y: float, y coord. of vector end
'''
z = r * np.exp(1j * theta)
x, y = z.real, z.imag
return x, y
def cart2pol(x, y):
'''
Parameters:
- x: float, x coord. of vector end
- y: float, y coord. of vector end
Returns:
- r: float, vector amplitude
- theta: float, vector angle
'''
z = x + y * 1j
r,theta = np.abs(z), np.angle(z)
return r,theta
In case, like me, you're trying to control a robot that accepts a speed and heading value based off of a joystick value, use this instead (it converts the radians to degrees: