diff --git a/src/vmath.nim b/src/vmath.nim index 371d1d2..aeea51b 100644 --- a/src/vmath.nim +++ b/src/vmath.nim @@ -1573,8 +1573,12 @@ proc angle*[T](a: GVec2[T]): T = ## Angle of a Vec2. arctan2(a.y, a.x) -proc angle*[T; S: GVec2[T]|GVec3[T]](a, b: S): T = - ## Angle between 2 Vec2 or Vec3. +proc angle*[T; S: GVec2[T]](a, b: S): T = + ## Angle between 2 Vec2. + arctan2(a.y*b.x - a.x*b.y, a.x*b.x + a.y*b.y) + +proc angle*[T; S: GVec3[T]](a, b: S): T = + ## Angle between 2 Vec3. var dot = dot(a, b) dot = dot / (a.length * b.length) # The cases of angle((1, 1), (-1, -1)) and its 3d counterpart diff --git a/tests/test.nim b/tests/test.nim index db40f24..1f466a2 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1160,12 +1160,12 @@ block: template gen2DTestsFor(constructor: untyped): void = doAssert angle(constructor(1, 0), constructor(1, 0)) ~= 0 doAssert angle(constructor(1, 1), constructor(-1, -1)) ~= Pi - doAssert angle(constructor(1, 0), constructor(0, 1)) ~= Pi/2 - doAssert angle(constructor(1, 0), constructor(-1, 0)) ~= Pi + doAssert angle(constructor(1, 0), constructor(0, 1)) ~= -Pi/2 + doAssert angle(constructor(1, 0), constructor(-1, 0)) ~= -Pi doAssert angle(constructor(1, 1), constructor(1, -1)) ~= Pi/2 # Edge cases: - doAssert angle(constructor(0, 0), constructor(1, 0)).isNaN() + doAssert angle(constructor(0, 0), constructor(1, 0)) ~= 0 gen2DTestsFor vec2 gen2DTestsFor dvec2