From e92fdb4a99bf9d27009dd35fdd074ff14facfc03 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 5 Mar 2020 20:34:30 +0000 Subject: `x if cond else y` and `x when cond else y` expressions --- core/math/linalg/general.odin | 6 +++--- core/math/linalg/specific.odin | 32 ++++++++++++++++++++++---------- core/math/math.odin | 8 ++++---- 3 files changed, 29 insertions(+), 17 deletions(-) (limited to 'core/math') diff --git a/core/math/linalg/general.odin b/core/math/linalg/general.odin index 921d56c80..c7b455781 100644 --- a/core/math/linalg/general.odin +++ b/core/math/linalg/general.odin @@ -54,11 +54,11 @@ normalize :: proc{vector_normalize, quaternion_normalize}; vector_normalize0 :: proc(v: $T/[$N]$E) -> T where IS_NUMERIC(E) { m := length(v); - return m == 0 ? 0 : v/m; + return 0 if m == 0 else v/m; } quaternion_normalize0 :: proc(q: $Q) -> Q where IS_QUATERNION(Q) { m := abs(q); - return m == 0 ? 0 : q/m; + return 0 if m == 0 else q/m; } normalize0 :: proc{vector_normalize0, quaternion_normalize0}; @@ -258,7 +258,7 @@ vector_mix :: proc(x, y, a: $V/[$N]$E) -> V where IS_NUMERIC(E) { vector_step :: proc(edge, x: $V/[$N]$E) -> V where IS_NUMERIC(E) { s: V; for i in 0.. Vector3 { y := abs(v.y); z := abs(v.z); - other: Vector3 = x < y ? (x < z ? {1, 0, 0} : {0, 0, 1}) : (y < z ? {0, 1, 0} : {0, 0, 1}); - + other: Vector3; + if x < y { + if x < z { + other = {1, 0, 0}; + } else { + other = {0, 0, 1}; + } + } else { + if y < z { + other = {0, 1, 0}; + } else { + other = {0, 0, 1}; + } + } return normalize(cross(v, other)); } @@ -124,7 +136,7 @@ vector4_hsl_to_rgb :: proc(h, s, l: Float, a: Float = 1) -> Vector4 { g = l; b = l; } else { - q := l < 0.5 ? l * (1+s) : l+s - l*s; + q := l * (1+s) if l < 0.5 else l+s - l*s; p := 2*l - q; r = hue_to_rgb(p, q, h + 1.0/3.0); g = hue_to_rgb(p, q, h); @@ -147,10 +159,10 @@ vector4_rgb_to_hsl :: proc(col: Vector4) -> Vector4 { if v_max != v_min { d: = v_max - v_min; - s = l > 0.5 ? d / (2.0 - v_max - v_min) : d / (v_max + v_min); + s = d / (2.0 - v_max - v_min) if l > 0.5 else d / (v_max + v_min); switch { case v_max == r: - h = (g - b) / d + (g < b ? 6.0 : 0.0); + h = (g - b) / d + (6.0 if g < b else 0.0); case v_max == g: h = (b - r) / d + 2.0; case v_max == b: @@ -627,9 +639,9 @@ matrix4_inverse :: proc(m: Matrix4) -> Matrix4 { matrix4_minor :: proc(m: Matrix4, c, r: int) -> Float { cut_down: Matrix3; for i in 0..<3 { - col := i < c ? i : i+1; + col := i if i < c else i+1; for j in 0..<3 { - row := j < r ? j : j+1; + row := j if j < r else j+1; cut_down[i][j] = m[col][row]; } } @@ -638,7 +650,7 @@ matrix4_minor :: proc(m: Matrix4, c, r: int) -> Float { matrix4_cofactor :: proc(m: Matrix4, c, r: int) -> Float { sign, minor: Float; - sign = (c + r) % 2 == 0 ? 1 : -1; + sign = 1 if (c + r) % 2 == 0 else -1; minor = matrix4_minor(m, c, r); return sign * minor; } diff --git a/core/math/math.odin b/core/math/math.odin index d5ec9f3ae..cb7231b86 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -115,7 +115,7 @@ 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; + return wrap + tmp if tmp < 0 else tmp; } angle_diff :: proc(a, b: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) { @@ -128,7 +128,7 @@ angle_lerp :: proc(a, b, t: $T) -> T where intrinsics.type_is_numeric(T), !intri } step :: proc(edge, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) { - return x < edge ? 0 : 1; + return 0 if x < edge else 1; } smoothstep :: proc(edge0, edge1, x: $T) -> T where intrinsics.type_is_numeric(T), !intrinsics.type_is_array(T) { @@ -246,10 +246,10 @@ trunc_f64 :: proc(x: f64) -> f64 { trunc :: proc{trunc_f32, trunc_f64}; round_f32 :: proc(x: f32) -> f32 { - return x < 0 ? ceil(x - 0.5) : floor(x + 0.5); + return ceil(x - 0.5) if x < 0 else floor(x + 0.5); } round_f64 :: proc(x: f64) -> f64 { - return x < 0 ? ceil(x - 0.5) : floor(x + 0.5); + return ceil(x - 0.5) if x < 0 else floor(x + 0.5); } round :: proc{round_f32, round_f64}; -- cgit v1.2.3