use vector3::Vector3;
use std::fmt;
#[derive(Clone, Copy)]
pub struct Line3 {
pub a: Vector3, pub v: Vector3, pub qa: f64 }
impl Line3 {
pub fn new(a: &Vector3, b:&Vector3) -> Line3 {
let v: Vector3 = *b - *a;
let qa: f64 = v.x.powi(2) + v.y.powi(2) + v.z.powi(2);
if qa == 0. {
panic!("The line cannot be defined by two equal points.");
}
Self { a: *a, v, qa }
}
pub fn calc_point(&self, lambda: f64) -> Vector3 {
self.a + self.v * lambda
}
pub fn dist_point(&self, p: &Vector3) -> f64 {
let ap: Vector3 = *p - self.a;
ap.cross(&self.v).magnitude() / self.qa.sqrt()
}
}
impl fmt::Debug for Line3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!("({}, {}, {}) --> ({}, {}, {})", self.a.x, self.a.y, self.a.z, self.v.x, self.v.y, self.v.z))
}
}
impl fmt::Display for Line3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "A ({}, {}, {}) --> V ({}, {}, {})", self.a.x, self.a.y, self.a.z, self.v.x, self.v.y, self.v.z)
}
}