From 5df06748e1866434ad154cd5d08bd4f55f50baea Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 2 Jan 2023 12:46:56 +0100 Subject: [PATCH] getDegreeAngleFromDirection fix --- assets/Scripts/Services/Utils/MathUtils.ts | 4 +++- tests/utils/mathUtils.test.ts | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/assets/Scripts/Services/Utils/MathUtils.ts b/assets/Scripts/Services/Utils/MathUtils.ts index ac3c2e8..904134f 100644 --- a/assets/Scripts/Services/Utils/MathUtils.ts +++ b/assets/Scripts/Services/Utils/MathUtils.ts @@ -8,5 +8,7 @@ export function randomPositiveOrNegative(): number { export function getDegreeAngleFromDirection(x: number, y: number): number { const radianAngle = Math.atan2(y, x); - return (radianAngle / Math.PI) * 180; + const angle = (radianAngle / Math.PI) * 180; + + return angle < 0 ? angle + 360 : angle; } diff --git a/tests/utils/mathUtils.test.ts b/tests/utils/mathUtils.test.ts index 87d85a2..88d5d44 100644 --- a/tests/utils/mathUtils.test.ts +++ b/tests/utils/mathUtils.test.ts @@ -8,8 +8,7 @@ const testCases: { x: number; y: number; expectedAngle: number }[] = [ for (const testCase of testCases) { test(`returns correct degree angle for direction [X: ${testCase.x} , Y: ${testCase.y}] (${testCase.expectedAngle} degrees)`, () => { - let angle = getDegreeAngleFromDirection(testCase.x, testCase.y); - if (angle < 0) angle += 360; + const angle = getDegreeAngleFromDirection(testCase.x, testCase.y); expect(angle).toBe(testCase.expectedAngle); });