aboutsummaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2019-12-28 23:48:15 +0000
committergingerBill <bill@gingerbill.org>2019-12-28 23:48:15 +0000
commit8ee41c20afbe8d3373ba10e19b08a6bcfda8197d (patch)
tree67dae4178773bf0eb69e1838a9bd9b5b2d050c91 /core/math
parent11c705508da0902b651995913a43e266720da3c0 (diff)
Add more to `package math`
Diffstat (limited to 'core/math')
-rw-r--r--core/math/math.odin61
1 files changed, 61 insertions, 0 deletions
diff --git a/core/math/math.odin b/core/math/math.odin
index b56434428..b070129a5 100644
--- a/core/math/math.odin
+++ b/core/math/math.odin
@@ -112,6 +112,41 @@ unlerp_f32 :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
unlerp_f64 :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
unlerp :: proc{unlerp_f32, unlerp_f64};
+
+wrap :: proc(x, y: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
+ tmp := mod(x, y);
+ return tmp < 0 ? wrap + tmp : tmp;
+}
+angle_diff :: proc(a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
+
+ dist := wrap(b - a, TAU);
+ return wrap(dist*2, TAU) - dist;
+}
+
+angle_lerp :: proc(a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
+ return a + angle_diff(a, b) * t;
+}
+
+step :: proc(edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
+ return x < edge ? 0 : 1;
+}
+
+smoothstep :: proc(edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) {
+ t := clamp((x - edge0) / (edge1 - edge0), 0, 1);
+ return t * t * (3 - 2*t);
+}
+
+bias :: proc(t, b: $T) -> T where intrinsics.type_is_numeric(T) {
+ return t / (((1/b) - 2) * (1 - t) + 1);
+}
+gain :: proc(t, g: $T) -> T where intrinsics.type_is_numeric(T) {
+ if t < 0.5 {
+ return bias(t*2, g)*0.5;
+ }
+ return bias(t*2 - 1, 1 - g)*0.5 + 0.5;
+}
+
+
sign_f32 :: proc(x: f32) -> f32 { return f32(int(0 < x) - int(x < 0)); }
sign_f64 :: proc(x: f64) -> f64 { return f64(int(0 < x) - int(x < 0)); }
sign :: proc{sign_f32, sign_f64};
@@ -729,6 +764,32 @@ acos_f64 :: proc(x: f64) -> f64 {
acos :: proc{acos_f32};
+sinh_f32 :: proc(x: f32) -> f32 {
+ return (exp(x) - exp(-x))*0.5;
+}
+sinh_f64 :: proc(x: f64) -> f64 {
+ return (exp(x) - exp(-x))*0.5;
+}
+sinh :: proc{sinh_f32, sinh_f64};
+
+cosh_f32 :: proc(x: f32) -> f32 {
+ return (exp(x) + exp(-x))*0.5;
+}
+cosh_f64 :: proc(x: f64) -> f64 {
+ return (exp(x) + exp(-x))*0.5;
+}
+cosh :: proc{cosh_f32, cosh_f64};
+
+tanh_f32 :: proc(x: f32) -> f32 {
+ t := exp(2*x);
+ return (t - 1) / (t + 1);
+}
+tanh_f64 :: proc(x: f64) -> f64 {
+ t := exp(2*x);
+ return (t - 1) / (t + 1);
+}
+tanh :: proc{tanh_f32, tanh_f64};
+
F32_DIG :: 6;
F32_EPSILON :: 1.192092896e-07;