diff --git a/dqnt.h b/dqnt.h index e64f5b2..5aa2abb 100644 --- a/dqnt.h +++ b/dqnt.h @@ -49,6 +49,11 @@ typedef float f32; #define DQNT_SQUARED(x) ((x) * (x)) #define DQNT_SQRT(x) (sqrtf(x)) +//////////////////////////////////////////////////////////////////////////////// +// Math +//////////////////////////////////////////////////////////////////////////////// +DQNT_FILE_SCOPE f32 dqnt_lerp(f32 a, f32 t, f32 b); + //////////////////////////////////////////////////////////////////////////////// // Vec2 //////////////////////////////////////////////////////////////////////////////// @@ -214,7 +219,26 @@ DQNT_FILE_SCOPE i32 dqnt_rnd_pcg_range(DqntRandPCGState *pcg, i32 min, i32 max) #define WIN32_LEAN_AND_MEAN #endif +//////////////////////////////////////////////////////////////////////////////// +// Math +//////////////////////////////////////////////////////////////////////////////// +DQNT_FILE_SCOPE f32 dqnt_lerp(f32 a, f32 t, f32 b) +{ + /* + Linear blend between two values. We having a starting point "a", and + the distance to "b" is defined as (b - a). Then we can say + a + t(b - a) + + As our linear blend fn. We start from "a" and choosing a t from 0->1 + will vary the value of (b - a) towards b. If we expand this, this + becomes + + a + (t * b) - (a * t) == (1 - t)a + t*b + */ + f32 result = a + (b - a) * t; + return result; +} //////////////////////////////////////////////////////////////////////////////// // Vec2 diff --git a/dqnt_unit_test.cpp b/dqnt_unit_test.cpp index 71d0297..796a0ce 100644 --- a/dqnt_unit_test.cpp +++ b/dqnt_unit_test.cpp @@ -293,6 +293,29 @@ void dqnt_random_test() { printf("dqnt_random_test(): Completed successfully\n"); } +void dqnt_math_test() +{ + { // Lerp + { + f32 start = 10; + f32 t = 0.5f; + f32 end = 20; + DQNT_ASSERT(dqnt_lerp(start, t, end) == 15); + } + + { + f32 start = 10; + f32 t = 2.0f; + f32 end = 20; + DQNT_ASSERT(dqnt_lerp(start, t, end) == 30); + } + + printf("dqnt_math_test(): lerp: Completed successfully\n"); + } + + printf("dqnt_math_test(): Completed successfully\n"); +} + void dqnt_vec_test() { { // V2 @@ -438,7 +461,12 @@ int main(void) { dqnt_strings_test(); dqnt_random_test(); + dqnt_math_test(); dqnt_vec_test(); dqnt_other_test(); + + printf("\nPress Any Key to Exit\n"); + getchar(); + return 0; }