diff --git a/DIRECTORY.md b/DIRECTORY.md index c63e71a8c56..da8f84abc2a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -83,6 +83,7 @@ * [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs) * [Octal to Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs) * [Order of Magnitude Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/order_of_magnitude_conversion.rs) + * [Rectangular to Polar](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rectangular_to_polar.rs) * [RGB-CMYK Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs) * [RGB-HSV Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_hsv_conversion.rs) * [Roman Numerals](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/roman_numerals.rs) diff --git a/src/conversions/mod.rs b/src/conversions/mod.rs index 1c38f04754e..17df133a06e 100644 --- a/src/conversions/mod.rs +++ b/src/conversions/mod.rs @@ -13,6 +13,7 @@ mod octal_to_binary; mod octal_to_decimal; mod octal_to_hexadecimal; mod order_of_magnitude_conversion; +mod rectangular_to_polar; mod rgb_cmyk_conversion; mod rgb_hsv_conversion; mod roman_numerals; @@ -36,6 +37,7 @@ pub use self::octal_to_hexadecimal::octal_to_hexadecimal; pub use self::order_of_magnitude_conversion::{ convert_metric_length, metric_length_conversion, MetricLengthUnit, }; +pub use self::rectangular_to_polar::rectangular_to_polar; pub use self::rgb_cmyk_conversion::rgb_to_cmyk; pub use self::rgb_hsv_conversion::{hsv_to_rgb, rgb_to_hsv, ColorError, Hsv, Rgb}; pub use self::roman_numerals::{int_to_roman, roman_to_int}; diff --git a/src/conversions/rectangular_to_polar.rs b/src/conversions/rectangular_to_polar.rs new file mode 100644 index 00000000000..2428b14c8af --- /dev/null +++ b/src/conversions/rectangular_to_polar.rs @@ -0,0 +1,47 @@ +//! Conversions between rectangular (Cartesian) and polar coordinate systems. +//! +//! This module provides utilities for converting rectangular coordinates +//! into polar coordinates with angles expressed in degrees. +//! +//! More information: + +/// Convert rectangular (Cartesian) coordinates to polar coordinates. +/// +/// The returned tuple contains: +/// - magnitude (r) +/// - angle (θ) in degrees +/// +/// Both values are rounded to 2 decimal places. +/// +/// # Formula +/// - r = sqrt(x² + y²) +/// - θ = atan2(y, x) converted to degrees +pub fn rectangular_to_polar(real: f64, imag: f64) -> (f64, f64) { + let magnitude = (real.powi(2) + imag.powi(2)).sqrt(); + let angle = imag.atan2(real).to_degrees(); + + ( + round_to_two_decimals(magnitude), + round_to_two_decimals(angle), + ) +} + +fn round_to_two_decimals(value: f64) -> f64 { + (value * 100.0).round() / 100.0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_rectangular_to_polar() { + assert_eq!(rectangular_to_polar(5.0, -5.0), (7.07, -45.0)); + assert_eq!(rectangular_to_polar(-1.0, 1.0), (1.41, 135.0)); + assert_eq!(rectangular_to_polar(-1.0, -1.0), (1.41, -135.0)); + assert_eq!(rectangular_to_polar(1e-10, 1e-10), (0.0, 45.0)); + assert_eq!(rectangular_to_polar(-1e-10, 1e-10), (0.0, 135.0)); + assert_eq!(rectangular_to_polar(9.75, 5.93), (11.41, 31.31)); + assert_eq!(rectangular_to_polar(10000.0, 99999.0), (100497.76, 84.29)); + } +}