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