diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..ef7e0bcde7f7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -166,6 +166,7 @@ * [Octal To Binary](conversions/octal_to_binary.py) * [Octal To Decimal](conversions/octal_to_decimal.py) * [Octal To Hexadecimal](conversions/octal_to_hexadecimal.py) + * [Polar To Rectangular](conversions/polar_to_rectangular.py) * [Prefix Conversions](conversions/prefix_conversions.py) * [Prefix Conversions String](conversions/prefix_conversions_string.py) * [Pressure Conversions](conversions/pressure_conversions.py) diff --git a/conversions/polar_to_rectangular.py b/conversions/polar_to_rectangular.py new file mode 100644 index 000000000000..c86181181c96 --- /dev/null +++ b/conversions/polar_to_rectangular.py @@ -0,0 +1,36 @@ +import math + + +def pol_to_rec(mod: float, mag: float): + ### https://en.wikipedia.org/wiki/Polar_coordinate_system + """ + >>> pol_to_rec(5, 53) + (3.01, 3.99) + >>> pol_to_rec(10, 0) + (10.0, 0.0) + >>> pol_to_rec(0, 45) + (0.0, 0.0) + >>> pol_to_rec(5, 90) + (0.0, 5.0) + >>> pol_to_rec(5, 180) + (-5.0, 0.0) + >>> pol_to_rec(5, 270) + (-0.0, -5.0) + >>> pol_to_rec(5, 360) + (5.0, -0.0) + """ + + rad = math.radians(mag) + + real = round((mod * math.cos(rad)), 2) + img = round(mod * math.sin(rad), 2) + + rec = (real, img) + + return rec + + +if __name__ == "__main__": + import doctest + + doctest.testmod()