# HG changeset patch # User sam # Date 1713166579 -25200 # Node ID 3845b9f1d27df4a6a2a4cf1de629f4b112891294 # Parent e7a53442eab80d1f842183325ec12985d4bbb380 add: assignment operator, normal 2D vector diff -r e7a53442eab8 -r 3845b9f1d27d semicongine/core/vector.nim --- 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)])