This commit is contained in:
fabolous005 2024-01-13 21:37:34 +01:00
parent e970a45cbb
commit d07048bb55
2 changed files with 156 additions and 0 deletions

8
Cargo.toml Normal file
View File

@ -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]

148
src/main.rs Normal file
View File

@ -0,0 +1,148 @@
use std::env;
fn main() {
let args: Vec<String> = 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<f64> = 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()
}
)
}