#!/usr/bin/python3
# ===============================================================
# test polar to Cartesian coordinate conversion
#
# Polar coordinates are a way to describe the location of a
# point in a plane using a distance from a reference point
# (the pole) and an angle from a reference direction (the polar
# axis). They are represented as an ordered pair (r,Θ),
# where r is the radial distance from the pole and Θ is
# the angle measured from the polar axis.
# ---------------------------------------------------------------
# angle x
#
# + sin(x) = a/h
# /| cos(x) = b/h
# / |
# h / | a h = math.sqrt(a**2 + b**2)
# / |
# / | x = atan(a/b) x = atan2(xcoord,ycoord)
# / x | x = asin(a/h)
# +------+ x = acos(b/h)
# b
#
# ===============================================================
import math
import user_interface as ui
# ---------------------------------------------------------------
# ---- convert polar to Cartesian coordinates
# ---- convert Cartesian to polar coordinates
# ---- rho radial distance
# ---- phi angle (radians)
# ---------------------------------------------------------------
def polar2cart(rho:[int|float],
phi:[int|float]) -> tuple:
'''
convert polar to Cartesian coordinates
rho radial distance
phi angle (radians)
'''
x = rho * math.cos(phi)
y = rho * math.sin(phi)
return (x,y)
def cart2polar(x:[int|float],
y:[int|float]) -> tuple:
'''
convert Cartesian coordinates to polar coordinates
rho radial distance
phi angle (radians)
(-180 degrees to 180 degrees)
'''
rho = math.sqrt(x**2 + y**2)
phi = math.atan2(y,x)
return (rho,phi)
# ---------------------------------------------------------------
# ---- main
# ---------------------------------------------------------------
rho = 100
print()
print(f'This test program assumes a polar coord')
print(f'radial distance of {rho} for testing.')
print()
while True:
print()
s = ui.get_user_input('Enter polar coord angle (degrees): ')
if not s: break
tf,deg = ui.is_float(s)
if not tf:
print()
print('bad input - try again')
continue
phi = math.radians(deg)
x,y = polar2cart(rho,phi)
print()
print(f'Cartesian Coordinates: x={x:.6f} y={y:.6f}')
#!/usr/bin/python3
# ===============================================================
# test Cartesian to polar coordinate conversion
#
# Polar coordinates are a way to describe the location of a
# point in a plane using a distance from a reference point
# (the pole) and an angle from a reference direction (the polar
# axis). They are represented as an ordered pair (r,Θ),
# where r is the radial distance from the pole and Θ is
# the angle measured counterclockwise from the polar axis.
# ---------------------------------------------------------------
# angle x
#
# + sin(x) = a/h
# /| cos(x) = b/h
# / |
# h / | a h = math.sqrt(a**2 + b**2)
# / |
# / | x = atan(a/b) x = atan2(xcoord,ycoord)
# / x | x = asin(a/h)
# +------+ x = acos(b/h)
# b
#
# ===============================================================
import math
import user_interface as ui
# ---------------------------------------------------------------
# ---- convert polar to Cartesian coordinates
# ---- convert Cartesian to polar coordinates
# ---- rho radial distance
# ---- phi angle (radians)
# ---------------------------------------------------------------
def polar2cart(rho:[int|float],
phi:[int|float]) -> tuple:
'''
convert polar coordinates to Cartesian coordinates
rho radial distance
phi angle (radians)
'''
x = rho * math.cos(phi)
y = rho * math.sin(phi)
return (x,y)
def cart2polar(x:[int|float],
y:[int|float]) -> tuple:
'''
convert Cartesian coordinates to polar coordinates
rho radial distance
phi angle (radians)
'''
rho = math.sqrt(x**2 + y**2)
phi = math.atan2(y,x)
return (rho,phi)
# ---------------------------------------------------------------
# ---- main
# ---------------------------------------------------------------
while True:
print()
sx = ui.get_user_input('Enter x coord: ')
if not sx: break
tf,x = ui.is_float(sx)
if not tf:
print()
print('bad input - try again')
continue
print()
sy = ui.get_user_input('Enter y coord: ')
if not sy: break
tf,y = ui.is_float(sy)
if not tf:
print()
print('bad input - try again')
continue
hyp = math.sqrt(x**2 + y**2)
print()
print('Cartesian Coordinates: ',end='')
print(f'x={x:.4f} y={y:.4f} hyp={hyp:.4f}')
dst,rad = cart2polar(x,y) # radial distance,
# angle (radians)
deg = math.degrees(rad) # degrees (+180 to -180)
if deg < 0.0: deg = deg + 360 # degrees (counterclockwise)
print('Polar Coordinates : ',end='')
print(f'dst={dst:.4f} deg={deg:.4f} rad={rad:.4f}')