feat(math): added to_point method into the InnerSpace trait and added tangent evaluation to the Bezier curve struct (changed point to vecs inside the struct as well)

This commit is contained in:
lisk77 2025-07-26 01:14:47 +02:00
parent d04c706a94
commit 05a4679f38
3 changed files with 929 additions and 831 deletions

View file

@ -1,27 +1,42 @@
use crate::Point;
use crate::{InnerSpace, Point};
pub struct Bezier<P: Point> {
points: Vec<P>,
degree: usize
pub struct Bezier<V: InnerSpace> {
points: Vec<V>,
degree: usize,
}
impl<P: Point + Clone> Bezier<P> {
pub fn new(points: Vec<P>) -> Self {
let degree = points.len() - 1;
impl<V: InnerSpace + Clone> Bezier<V> {
pub fn new(points: Vec<V>) -> Self {
let degree = points.len() - 1;
Self {
points,
degree
}
}
Self { points, degree }
}
pub fn evaluate(&self, t: f32) -> P {
let mut new_points = self.points.clone();
for i in 0..self.degree {
for j in 0..(self.degree - i) {
new_points[j] = new_points[j].lerp(&new_points[j + 1], t);
}
}
new_points[0].clone()
}
}
pub fn evaluate(&self, t: f32) -> V {
let mut new_points = self.points.clone();
for i in 0..self.degree {
for j in 0..(self.degree - i) {
new_points[j] = new_points[j].lerp(&new_points[j + 1], t);
}
}
new_points[0].clone()
}
pub fn evaluate_tangent(&self, t: f32) -> V {
let n = self.degree as f32;
let mut d_pts: Vec<V> = self
.points
.windows(2)
.map(|w| ((w[1] - w[0]) * n))
.collect::<Vec<V>>();
for i in 0..(self.degree - 1) {
for j in 0..(self.degree - 1 - i) {
d_pts[j] = d_pts[j].lerp(&d_pts[j + 1], t);
}
}
d_pts[0].clone().normalize()
}
}