From be17671009c336066a798ccb8309eb69f838b811 Mon Sep 17 00:00:00 2001 From: xix xeaon Date: Mon, 25 Nov 2024 15:40:19 +0100 Subject: [PATCH 1/2] Signed angle 2D --- src/vmath.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 From 29b56f01c7154c49097420e84ef269262ab77050 Mon Sep 17 00:00:00 2001 From: xix xeaon Date: Fri, 6 Dec 2024 18:04:59 +0100 Subject: [PATCH 2/2] Updated the tests --- tests/test.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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