mirror of
https://github.com/Cactus-minecraft-server/World.git
synced 2025-12-07 10:40:37 +00:00
Auto commit
This commit is contained in:
29
src/lib.rs
29
src/lib.rs
@@ -35,9 +35,30 @@ fn normalize(v1: &Vector) -> Vector {
|
|||||||
y: v1.y / calculate_norm(v1),
|
y: v1.y / calculate_norm(v1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn fade(x: f32) -> f32 {
|
||||||
|
// This is a mathematic equation, it take a value and return the result of the value by that
|
||||||
|
// equation. That equation if derivate should be easy when close to 0 and 1 and hard in the
|
||||||
|
// middle. Take a f32 value return f32.Both value are between 0 and 1
|
||||||
|
6_f32 * x.powi(5) - 15_f32 * x.powi(4) + 10_f32 * x.powi(3)
|
||||||
|
}
|
||||||
|
/// Linearly interpolates between two scalar values.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
/// - `a`: start value (when `t = 0.0`)
|
||||||
|
/// - `b`: end value (when `t = 1.0`)
|
||||||
|
/// - `t`: interpolation factor in `[0.0, 1.0]`
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// A value on the line from `a` to `b`:
|
||||||
|
/// - if `t = 0.0`, yields `a`
|
||||||
|
/// - if `t = 1.0`, yields `b`
|
||||||
|
/// - otherwise `a + t * (b - a)`
|
||||||
|
fn linear_interpolation(a: f32, b: f32, t: f32) -> f32 {
|
||||||
|
a + t * (b - a)
|
||||||
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{Vector, calculate_norm, dot_product, normalize};
|
use crate::{Vector, calculate_norm, dot_product, linear_interpolation, normalize};
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dot_product() {
|
fn test_dot_product() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -67,4 +88,10 @@ mod tests {
|
|||||||
let v1 = Vector { x: 0.5, y: 0.5 };
|
let v1 = Vector { x: 0.5, y: 0.5 };
|
||||||
assert_eq!(calculate_norm(&normalize(&v1)).round(), 1_f32);
|
assert_eq!(calculate_norm(&normalize(&v1)).round(), 1_f32);
|
||||||
}
|
}
|
||||||
|
//fn test_fade() {todo!()}
|
||||||
|
#[test]
|
||||||
|
fn test_linear_interpolation() {
|
||||||
|
assert_eq!(linear_interpolation(0.0, 10.0, 0.25), 2.5);
|
||||||
|
assert_eq!(linear_interpolation(5.0, 15.0, 0.75), 12.5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user