Add min/max functions for vectors.

This commit is contained in:
treeform 2021-06-11 08:02:08 -07:00
parent 698d47ddff
commit d0aace4c93
2 changed files with 35 additions and 0 deletions

View file

@ -743,6 +743,24 @@ proc dir*[T](angle: T): GVec2[T] =
sin(angle),
)
proc min*(a, b: Vec2): Vec2 =
vec2(min(a.x, b.x), min(a.y, b.y))
proc min*(a, b: Vec3): Vec3 =
vec3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z))
proc min*(a, b: Vec4): Vec4 =
vec4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w))
proc max*(a, b: Vec2): Vec2 =
vec2(max(a.x, b.x), max(a.y, b.y))
proc max*(a, b: Vec3): Vec3 =
vec3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z))
proc max*(a, b: Vec4): Vec4 =
vec4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w))
type
Mat2* = GMat2[float32]
Mat3* = GMat3[float32]

View file

@ -761,6 +761,23 @@ block:
let mat2d = translate(vec2(10, 20)) * rotate(45.toRadians) * scale(vec2(2))
let mat3d = translate(vec3(10, 20, 0)) * rotateZ(45.toRadians) * scale(vec3(2))
block:
let
a2 = vec2(10, -10)
b2 = vec2(-10, 10)
a3 = vec3(10, -10, 7)
b3 = vec3(-10, 10, 0)
a4 = vec4(10, -10, 7, -2)
b4 = vec4(-10, 10, 0, -1)
doAssert min(a2, b2) == vec2(-10, -10)
doAssert min(a3, b3) == vec3(-10, -10, 0)
doAssert min(a4, b4) == vec4(-10, -10, 0, -2)
doAssert max(a2, b2) == vec2(10, 10)
doAssert max(a3, b3) == vec3(10, 10, 7)
doAssert max(a4, b4) == vec4(10, 10, 7, -1)
echo "test finished successfully"
block: