Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ quat(1.0, 2.0, 3.0, 4.0) ~= quat(1.0, 2.0, 3.0, 4.0)
* `sign` - Returns the sign of a number, -1 or 1.
* `quantize` - Makes v be a multiple of n. Rounding to integer quantizes by 1.0.
* `lerp` - Interpolates value between a and b.
* `len` - Returns the vector component count or matrix column count.
* `step` - Returns 0.0 if x is less than edge, otherwise 1.0.
* `smoothstep` - Performs smooth Hermite interpolation between 0.0 and 1.0.

Expand Down
25 changes: 25 additions & 0 deletions src/vmath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,31 @@ type
DVec3* = GVec3[float64]
DVec4* = GVec4[float64]

when not defined(vmathArrayBased):
proc len*[T](a: GVec2[T]): int =
## Returns the number of vector components, mirroring GLSL's .length() method.
2

proc len*[T](a: GVec3[T]): int =
## Returns the number of vector components, mirroring GLSL's .length() method.
3

proc len*[T](a: GVec4[T]): int =
## Returns the number of vector components, mirroring GLSL's .length() method.
4

proc len*[T](a: GMat2[T]): int =
## Returns the number of matrix columns, mirroring GLSL's .length() method.
2

proc len*[T](a: GMat3[T]): int =
## Returns the number of matrix columns, mirroring GLSL's .length() method.
3

proc len*[T](a: GMat4[T]): int =
## Returns the number of matrix columns, mirroring GLSL's .length() method.
4

proc `~=`*[T: SomeFloat](a, b: T): bool =
## Almost equal.
const Epsilon = 0.000001
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ suite "approximate equality":
test "int ~= should not compile":
check not compiles(1 ~= 1)

suite "len":
test "vector component count":
check vec2(10.0, 2.0).len == 2
check len(vec3(1, 2, 3)) == 3
check vec4(0).len == 4
check dvec4(0).len == 4
check ivec3(0).len == 3
check uvec2(0).len == 2
check bvec4(false).len == 4

test "matrix column count":
check mat2().len == 2
check mat3().len == 3
check mat4().len == 4
check dmat2().len == 2
check dmat3().len == 3
check dmat4().len == 4

suite "scalar utilities":
test "between":
check between(0.5, 0, 1)
Expand Down
Loading