use crate::generator::Complex;
use super::EvaluationError;
#[must_use]
pub fn get_line(p1: Complex, p2: Complex) -> Complex {
let slope = if f64::abs(p1.real - p2.real) < 1e-6 {
return Complex::new(f64::INFINITY, p1.real);
} else {
(p1.imaginary - p2.imaginary) / (p1.real - p2.real)
};
let intercept = p1.imaginary - p1.real * slope;
Complex::new(slope, intercept)
}
pub fn get_crossing(l1: Complex, l2: Complex) -> Result<Complex, EvaluationError> {
let (l1, l2) = if l2.real.is_infinite() {
(l2, l1)
} else {
(l1, l2)
};
let x = if l1.real.is_infinite() {
if l2.real.is_infinite() {
return Err(EvaluationError::ParallelLines);
}
l1.imaginary
} else {
(l2.imaginary - l1.imaginary) / (l1.real - l2.real)
};
let y = l2.real * x + l2.imaginary;
Ok(Complex::new(x, y))
}
#[must_use]
pub fn get_angle(arm1: Complex, origin: Complex, arm2: Complex) -> f64 {
let arm1_vec = arm1 - origin;
let arm2_vec = arm2 - origin;
let dot_product = arm1_vec.real * arm2_vec.real + arm1_vec.imaginary * arm2_vec.imaginary;
f64::acos(dot_product / (arm1_vec.mangitude() * arm2_vec.mangitude()))
}