From a06415f3712be6063bbb1d3f2b593b4d40127fff Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 6 Apr 2023 10:54:53 -0500 Subject: [PATCH] Added angleDiff() function to vector to get the difference in angle from one vector to another. --- src/Vector.java | 6 ++++++ tests/VectorTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Vector.java b/src/Vector.java index 64e2823..b1cc038 100644 --- a/src/Vector.java +++ b/src/Vector.java @@ -1,3 +1,4 @@ +import static java.lang.Math.acos; import static java.lang.Math.sqrt; public class Vector { @@ -65,4 +66,9 @@ public class Vector { float mag = this.mag(); return new Vector(x / mag, y / mag, z / mag); } + + float angleDiff(Vector other){ + float dot = this.dot(other); + return (float)acos(dot / (this.mag() * other.mag())); + } } diff --git a/tests/VectorTest.java b/tests/VectorTest.java index 8382c28..15243f6 100644 --- a/tests/VectorTest.java +++ b/tests/VectorTest.java @@ -81,4 +81,17 @@ class VectorTest{ assertFloatEquals((float)-216.2, cross.z, (float)0.1); } + @Test + public void testAngleDiff(){ + Vector v1 = new Vector(1, 0); + Vector v2 = new Vector(0, 1); + Vector v3 = new Vector(-1, 0); + + assertFloatEquals(v1.angleDiff(v1), 0); + assertFloatEquals(v1.angleDiff(v2), (float) toRadians(90)); + assertFloatEquals(v1.angleDiff(v3), (float) toRadians(180)); + assertFloatEquals(v1.angleDiff(v1.mul(100)), 0); + assertFloatEquals(v1.angleDiff(v2.mul(100)), (float) toRadians(90)); + } + } \ No newline at end of file