use super::curve::{Curve, CurveVector};
use crate::math::{Vector2, Vector3};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct LineCurve<P> {
pub v1: P,
pub v2: P,
}
pub type LineCurve3 = LineCurve<Vector3>;
impl<P> LineCurve<P> {
pub fn new(v1: P, v2: P) -> Self {
Self { v1, v2 }
}
}
pub trait LineVector: CurveVector {
fn lerp_line(v1: &Self, v2: &Self, t: f64) -> Self;
fn direction(v1: &Self, v2: &Self) -> Self;
const TYPE_NAME: &'static str;
}
macro_rules! line_vector {
($v:ty, $name:literal) => {
impl LineVector for $v {
fn lerp_line(v1: &Self, v2: &Self, t: f64) -> Self {
let mut point = *v2;
point.sub(v1);
point.multiply_scalar(t).add(v1);
point
}
fn direction(v1: &Self, v2: &Self) -> Self {
let mut target = <$v>::default();
target.sub_vectors(v2, v1).normalize();
target
}
const TYPE_NAME: &'static str = $name;
}
};
}
line_vector!(Vector2, "LineCurve");
line_vector!(Vector3, "LineCurve3");
impl<P: LineVector> Curve for LineCurve<P> {
type Point = P;
fn type_name(&self) -> &'static str {
P::TYPE_NAME
}
fn curve_path_resolution(&self, _divisions: usize) -> usize {
1
}
fn get_point(&self, t: f64) -> P {
if t == 1.0 {
self.v2
} else {
P::lerp_line(&self.v1, &self.v2, t)
}
}
fn get_point_at(&self, u: f64) -> P {
self.get_point(u)
}
fn get_tangent(&self, _t: f64) -> P {
P::direction(&self.v1, &self.v2)
}
fn get_tangent_at(&self, u: f64) -> P {
self.get_tangent(u)
}
}