x if cond else y and x when cond else y expressions

This commit is contained in:
gingerBill
2020-03-05 20:34:30 +00:00
parent 2fe0eaf2ad
commit e92fdb4a99
20 changed files with 336 additions and 44 deletions

View File

@@ -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..<N {
s[i] = x[i] < edge[i] ? 0 : 1;
s[i] = 0 if x[i] < edge[i] else 1;
}
return s;
}

View File

@@ -8,7 +8,7 @@ import "intrinsics"
Float :: f32;
FLOAT_EPSILON :: size_of(Float) == 4 ? 1e-7 : 1e-15;
FLOAT_EPSILON :: 1e-7 when size_of(Float) == 4 else 1e-15;
Vector2 :: distinct [2]Float;
Vector3 :: distinct [3]Float;
@@ -39,7 +39,7 @@ Matrix2 :: Matrix2x2;
Matrix3 :: Matrix3x3;
Matrix4 :: Matrix4x4;
Quaternion :: distinct (size_of(Float) == size_of(f32) ? quaternion128 : quaternion256);
Quaternion :: distinct (quaternion128 when size_of(Float) == size_of(f32) else quaternion256);
MATRIX1_IDENTITY :: Matrix1{{1}};
MATRIX2_IDENTITY :: Matrix2{{1, 0}, {0, 1}};
@@ -71,8 +71,20 @@ vector3_orthogonal :: proc(v: Vector3) -> 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;
}

View File

@@ -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};