From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- check-if-it-is-a-straight-line/Cargo.toml | 8 +++++++ check-if-it-is-a-straight-line/src/main.rs | 37 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 check-if-it-is-a-straight-line/Cargo.toml create mode 100644 check-if-it-is-a-straight-line/src/main.rs (limited to 'check-if-it-is-a-straight-line') diff --git a/check-if-it-is-a-straight-line/Cargo.toml b/check-if-it-is-a-straight-line/Cargo.toml new file mode 100644 index 0000000..596a8f3 --- /dev/null +++ b/check-if-it-is-a-straight-line/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "check-if-it-is-a-straight-line" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/check-if-it-is-a-straight-line/src/main.rs b/check-if-it-is-a-straight-line/src/main.rs new file mode 100644 index 0000000..711e12e --- /dev/null +++ b/check-if-it-is-a-straight-line/src/main.rs @@ -0,0 +1,37 @@ +fn main() { + let tests = [ + (vec![[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]], true), + (vec![[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]], false), + (vec![[0,0],[0,1],[0,-1]], true) + ]; + + for test in tests { + println!("{:?} is {} should be {}", + test.0, + check_straight_line(test.0.iter().map(|c| c.to_vec()).collect()), + test.1); + } +} + +pub fn check_straight_line(coordinates: Vec>) -> bool { + let base = get_lin_fn(coordinates[0].clone(), coordinates[1].clone()); + println!("{:?} base", base); + for c in 1..coordinates.len() { + let lin_fn = get_lin_fn(coordinates[0].clone(), coordinates[c].clone()); + println!("{} {:?}", c, lin_fn); + if lin_fn != base { + return false; + } + } + return true; +} + +pub fn get_lin_fn(a: Vec, b: Vec) -> (f32, f32) { + let mx = b[0] - a[0]; + let my = b[1] - a[1]; + let m = my as f32/mx as f32; + let b = a[1] as f32 - m*a[0] as f32; + + return (m,b); +} + -- cgit v1.2.3