Skip to content

Commit 8385617

Browse files
feat: add rectangular to polar coordinate conversion (#1003)
1 parent 89e784f commit 8385617

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
* [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
8484
* [Octal to Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs)
8585
* [Order of Magnitude Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/order_of_magnitude_conversion.rs)
86+
* [Rectangular to Polar](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rectangular_to_polar.rs)
8687
* [RGB-CMYK Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
8788
* [RGB-HSV Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_hsv_conversion.rs)
8889
* [Roman Numerals](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/roman_numerals.rs)

src/conversions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod octal_to_binary;
1313
mod octal_to_decimal;
1414
mod octal_to_hexadecimal;
1515
mod order_of_magnitude_conversion;
16+
mod rectangular_to_polar;
1617
mod rgb_cmyk_conversion;
1718
mod rgb_hsv_conversion;
1819
mod roman_numerals;
@@ -36,6 +37,7 @@ pub use self::octal_to_hexadecimal::octal_to_hexadecimal;
3637
pub use self::order_of_magnitude_conversion::{
3738
convert_metric_length, metric_length_conversion, MetricLengthUnit,
3839
};
40+
pub use self::rectangular_to_polar::rectangular_to_polar;
3941
pub use self::rgb_cmyk_conversion::rgb_to_cmyk;
4042
pub use self::rgb_hsv_conversion::{hsv_to_rgb, rgb_to_hsv, ColorError, Hsv, Rgb};
4143
pub use self::roman_numerals::{int_to_roman, roman_to_int};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Conversions between rectangular (Cartesian) and polar coordinate systems.
2+
//!
3+
//! This module provides utilities for converting rectangular coordinates
4+
//! into polar coordinates with angles expressed in degrees.
5+
//!
6+
//! More information: <https://en.wikipedia.org/wiki/Polar_coordinate_system>
7+
8+
/// Convert rectangular (Cartesian) coordinates to polar coordinates.
9+
///
10+
/// The returned tuple contains:
11+
/// - magnitude (r)
12+
/// - angle (θ) in degrees
13+
///
14+
/// Both values are rounded to 2 decimal places.
15+
///
16+
/// # Formula
17+
/// - r = sqrt(x² + y²)
18+
/// - θ = atan2(y, x) converted to degrees
19+
pub fn rectangular_to_polar(real: f64, imag: f64) -> (f64, f64) {
20+
let magnitude = (real.powi(2) + imag.powi(2)).sqrt();
21+
let angle = imag.atan2(real).to_degrees();
22+
23+
(
24+
round_to_two_decimals(magnitude),
25+
round_to_two_decimals(angle),
26+
)
27+
}
28+
29+
fn round_to_two_decimals(value: f64) -> f64 {
30+
(value * 100.0).round() / 100.0
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn test_rectangular_to_polar() {
39+
assert_eq!(rectangular_to_polar(5.0, -5.0), (7.07, -45.0));
40+
assert_eq!(rectangular_to_polar(-1.0, 1.0), (1.41, 135.0));
41+
assert_eq!(rectangular_to_polar(-1.0, -1.0), (1.41, -135.0));
42+
assert_eq!(rectangular_to_polar(1e-10, 1e-10), (0.0, 45.0));
43+
assert_eq!(rectangular_to_polar(-1e-10, 1e-10), (0.0, 135.0));
44+
assert_eq!(rectangular_to_polar(9.75, 5.93), (11.41, 31.31));
45+
assert_eq!(rectangular_to_polar(10000.0, 99999.0), (100497.76, 84.29));
46+
}
47+
}

0 commit comments

Comments
 (0)