Mercurial > games > semicongine
changeset 1114:75e109bb62ba
add: assignment operator, normal 2D vector
author | sam <sam@basx.dev> |
---|---|
date | Mon, 15 Apr 2024 14:36:19 +0700 |
parents | 87f2e9fcd000 |
children | a3ab99155209 |
files | semicongine/core/vector.nim |
diffstat | 1 files changed, 31 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/semicongine/core/vector.nim Mon Apr 15 12:22:13 2024 +0700 +++ b/semicongine/core/vector.nim Mon Apr 15 14:36:19 2024 +0700 @@ -116,6 +116,9 @@ func length*(vec: TVec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]) func length*(vec: TVec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])) +func normal*[T: SomeFloat](vec: TVec2[T]): auto = + TVec2[T]([vec[1], -vec[0]]) + func normalized*[T: SomeFloat](vec: TVec2[T]): auto = let l = vec.length if l == 0: vec @@ -206,6 +209,34 @@ func `/`*[T: SomeFloat](a, b: TVec4[T]): auto = TVec4([a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]]) +# assignment operations, scalar +func `+=`*(a: var TVec2, b: SomeNumber) = a = a + b +func `+=`*(a: var TVec3, b: SomeNumber) = a = a + b +func `+=`*(a: var TVec4, b: SomeNumber) = a = a + b +func `-=`*(a: var TVec2, b: SomeNumber) = a = a - b +func `-=`*(a: var TVec3, b: SomeNumber) = a = a - b +func `-=`*(a: var TVec4, b: SomeNumber) = a = a - b +func `*=`*(a: var TVec2, b: SomeNumber) = a = a * b +func `*=`*(a: var TVec3, b: SomeNumber) = a = a * b +func `*=`*(a: var TVec4, b: SomeNumber) = a = a * b +func `/=`*(a: var TVec2, b: SomeNumber) = a = a / b +func `/=`*(a: var TVec3, b: SomeNumber) = a = a / b +func `/=`*(a: var TVec4, b: SomeNumber) = a = a / b +# assignment operations, vector +func `+=`*(a: var TVec2, b: TVec2) = a = a + b +func `+=`*(a: var TVec3, b: TVec3) = a = a + b +func `+=`*(a: var TVec4, b: TVec4) = a = a + b +func `-=`*(a: var TVec2, b: TVec2) = a = a - b +func `-=`*(a: var TVec3, b: TVec3) = a = a - b +func `-=`*(a: var TVec4, b: TVec4) = a = a - b +func `*=`*(a: var TVec2, b: TVec2) = a = a * b +func `*=`*(a: var TVec3, b: TVec3) = a = a * b +func `*=`*(a: var TVec4, b: TVec4) = a = a * b +func `/=`*(a: var TVec2, b: TVec2) = a = a / b +func `/=`*(a: var TVec3, b: TVec3) = a = a / b +func `/=`*(a: var TVec4, b: TVec4) = a = a / b + + # special operations func pow*(a: TVec2, b: SomeNumber): auto = TVec2([pow(a[0], b), pow(a[1], b)])