1.0.3: Add fractional, speed up lerp.

This commit is contained in:
treeform 2021-04-18 16:50:48 -07:00
parent 9cdf152ec7
commit 19eb9dd330
3 changed files with 19 additions and 3 deletions

View file

@ -51,12 +51,19 @@ proc quantize*[T: SomeFloat](v, n: T): T =
## Makes v be multiple of n. Rounding to integer quantize by 1.0.
sign(v) * floor(abs(v) / n) * n
proc fractional*[T: SomeFloat](v: T): T =
## Returns fractional part of a number.
## 3.14 -> 0.14
## -3.14 -> 0.14
result = abs(v)
result = result - floor(result)
proc lerp*[T: SomeFloat](a, b, v: T): T =
## Interpolates value between a and b.
## * 0 -> a
## * 1 -> b
## * 0.5 -> between a and b
a * (1.0 - v) + b * v
v * (b - a) + a
proc fixAngle*[T: SomeFloat](angle: T): T =
## Make angle be from -PI to PI radians.
@ -339,7 +346,7 @@ genMathFn(ln)
genMathFn(log2)
genMathFn(sqrt)
genMathFn(floor)
genMathFn(ciel)
genMathFn(ceil)
genMathFn(abs)
proc `~=`*[T](a, b: GVec2[T]): bool =
@ -491,6 +498,9 @@ proc `~=`*[T](a, b: GMat3[T]): bool =
proc `~=`*[T](a, b: GMat4[T]): bool =
a[0] ~= b[0] and a[1] ~= b[1] and a[2] ~= b[2] and a[3] ~= b[3]
proc pos*[T](a: GMat3[T]): GVec2[T] =
return [a[2][2], a[2][1]]
proc `*`*[T](a, b: GMat3[T]): GMat3[T] =
result[0, 0] = b[0, 0] * a[0, 0] + b[0, 1] * a[1, 0] + b[0, 2] * a[2, 0]
result[0, 1] = b[0, 0] * a[0, 1] + b[0, 1] * a[1, 1] + b[0, 2] * a[2, 1]

View file

@ -41,6 +41,12 @@ block:
doAssert quantize(1.23456789, 0.1) ~= 1.2
doAssert quantize(1.23456789, 0.01) ~= 1.23
doAssert fractional(0.0) ~= 0.0
doAssert fractional(3.14) ~= 0.14
doAssert fractional(-3.14) ~= 0.14
doAssert fractional(1.23456789) ~= 0.23456789
doAssert fractional(-1.23456789) ~= 0.23456789
doAssert lerp(0.0, 1.0, 0.5) ~= 0.5
doAssert lerp(0.0, 10.0, 0.5) ~= 5.0
doAssert lerp(0.0, 100.0, 0.5) ~= 50.0

View file

@ -1,4 +1,4 @@
version = "1.0.2"
version = "1.0.3"
author = "treeform"
description = "Your single stop for vector math routines for 2d and 3d graphics."
license = "MIT"