diff --git a/README.md b/README.md index c384cbd..4cafa33 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_check_mark: | | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_check_mark: | | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_check_mark: | -| Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_large_square: | +| Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_check_mark: | | Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_large_square: | | Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: | | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_large_square: | diff --git a/Week-08/Day-53_Javelin-Parabolic-Throw/day53/Cargo.toml b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/Cargo.toml new file mode 100644 index 0000000..5b532e3 --- /dev/null +++ b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day53" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/lib.rs b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/lib.rs new file mode 100644 index 0000000..40e6058 --- /dev/null +++ b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/lib.rs @@ -0,0 +1,8 @@ +pub fn javelin_throw(initial_speed: f32, angle: u8) -> (u16, u16) { + const G: f32 = 9.81; + let angle = angle as f32; + + let h_max = initial_speed * initial_speed * f32::sin(angle.to_radians()).powi(2) / (2f32 * G); + let d = initial_speed * initial_speed * f32::sin(2f32 * angle.to_radians()) / G; + (h_max.round() as u16, d.round() as u16) +} diff --git a/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/main.rs b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/main.rs new file mode 100644 index 0000000..114e569 --- /dev/null +++ b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/src/main.rs @@ -0,0 +1,24 @@ +use std::io::{self, Write}; + +use day53::javelin_throw; + +fn read_number() -> T +where + T: std::str::FromStr, + ::Err: std::fmt::Debug, +{ + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + input.trim().parse().unwrap() +} + +fn main() { + print!("Enter the initial speed: "); + io::stdout().flush().unwrap(); + let initial_speed: f32 = read_number(); + print!("Enter the throw angle: "); + io::stdout().flush().unwrap(); + let angle: u8 = read_number(); + let (h_max, d) = javelin_throw(initial_speed, angle); + println!("Ymax={}, Xmax={}", h_max, d); +} diff --git a/Week-08/Day-53_Javelin-Parabolic-Throw/day53/tests/examples.rs b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/tests/examples.rs new file mode 100644 index 0000000..d2a9160 --- /dev/null +++ b/Week-08/Day-53_Javelin-Parabolic-Throw/day53/tests/examples.rs @@ -0,0 +1,19 @@ +#[cfg(test)] +mod examples { + use day53::javelin_throw; + + #[test] + fn example1() { + assert_eq!(javelin_throw(36.7, 45), (34, 137)); + } + + #[test] + fn example2() { + assert_eq!(javelin_throw(51.3, 20), (16, 172)); + } + + #[test] + fn example3() { + assert_eq!(javelin_throw(100.1, 89), (511, 36)); + } +}