# HG changeset patch # User sam # Date 1713166579 -25200 # Node ID 75e109bb62ba26e36f231fe81cbfca909c3305f5 # Parent 87f2e9fcd0007b298f482415ca49a13298520571 add: assignment operator, normal 2D vector diff -r 87f2e9fcd000 -r 75e109bb62ba 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)])