From d07048bb55536834666e87d97ec81014f061af85 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Sat, 13 Jan 2024 21:37:34 +0100 Subject: [PATCH] Initial --- Cargo.toml | 8 +++ src/main.rs | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0733c56 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "cular" +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/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6ccbb61 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,148 @@ +use std::env; + +fn main() { + let args: Vec = env::args().skip(1).take(3).collect(); + let mut points: [[f64; 2]; 3] = [[0.0; 2]; 3]; + + for (i, part) in args.iter().enumerate().take(3) { + let values: Vec = part + .split(':') + .filter_map(|s| s.parse().ok()) + .collect(); + + if values.len() == 2 { + points[i][0] = values[0]; + points[i][1] = values[1]; + } else { + println!("Invalid input format: {}", part); + } + } + group_by_x(&mut points); + + let m1 = (points[0][1] - points[1][1]) / (points[0][0] - points[1][0]); + if m1 == (points[1][1] - points[2][1]) / (points[1][0] - points[2][0]) { + println!("{}", straight_string(m1, &points)); + } else { + println!("{}", square_string(&points)); + } +} + +fn group_by_x(arr: &mut [[f64;2];3]) { + arr.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap_or(std::cmp::Ordering::Equal)); +} + +fn nearest_to_zero(arr: &[[f64;2];3]) -> usize { + let mut nearest = arr[1][1]; + let mut nearest_index = 0; + for (i, point) in arr.iter().enumerate() { + if nearest < point[1].abs() { + nearest = point[i-1]; + nearest_index = i-1; + } + } + nearest_index +} + +fn straight_string(m: f64, points: &[[f64;2];3]) -> String { + format!( + "{}x{}", + if m != 1.0 { + format!("{} * ", m) + } else { + "".to_string() + }, + { + let lowest_y = nearest_to_zero(points); + let result = points[lowest_y][0] - points[lowest_y][1] * m; + if result != 0.0 { + format!( + "{}{}", + if result < 0.0 { + " - ".to_string() + } else { + " + ".to_string() + }, + result.abs() + ) + } else { + "".to_string() + } + } + ) +} + +fn square_string(points: &[[f64;2];3]) -> String { + let a = -((points[0][0] * points[1][1] - + points[0][0] * points[2][1] - + points[1][0] * points[0][1] + + points[1][0] * points[2][1] + + points[2][0] * points[0][1] - + points[2][0] * points[1][1]) + / + ((points[0][0] - points[1][0]) * + (points[0][0] - points[2][0]) * + (points[1][0] - points[2][0]))); + + let b = (points[0][0].powi(2) * points[1][1] - + points[0][0].powi(2) * points[2][1] - + points[1][0].powi(2) * points[0][1] + + points[1][0].powi(2) * points[2][1] + + points[2][0].powi(2) * points[0][1] - + points[2][0].powi(2) * points[1][1]) + / + ((points[0][0] - points[1][0]) * + (points[0][0] - points[2][0]) * + (points[1][0] - points[2][0])); + + let c = (points[0][0].powi(2) * points[1][0] * points[2][1] - + points[0][0].powi(2) * points[2][0] * points[1][1] - + points[0][0] * points[1][0].powi(2) * points[2][1] + + points[0][0] * points[2][0].powi(2) * points[1][1] + + points[1][0].powi(2) * points[2][0] * points[0][1] - + points[1][0] * points[2][0].powi(2) * points[0][1]) + / + ((points[0][0] - points[1][0]) * + (points[0][0] - points[2][0]) * + (points[1][0] - points[2][0])); + + format!( + "{}x^2{}{}", + if a == -1.0 { + "-".to_string() + } else if a != 1.0 { + a.to_string() + } else { + "".to_string() + }, + if b != 0.0 { + format!( + "{}{}", + if b < 0.0 { + " - ".to_string() + } else { + " + ".to_string() + }, + if b != 1.0 { + format!("{}x", b.abs()) + } else { + "x".to_string() + } + ) + } else { + "".to_string() + }, + if c != 0.0 { + format!( + "{}{}", + if c < 0.0 { + " - ".to_string() + } else { + " + ".to_string() + }, + c.abs() + ) + } else { + "".to_string() + } + ) +}