diff --git a/LinearAlgebra/src/Direction.cs b/LinearAlgebra/src/Direction.cs index d544d90..7d0b75b 100644 --- a/LinearAlgebra/src/Direction.cs +++ b/LinearAlgebra/src/Direction.cs @@ -24,15 +24,11 @@ namespace LinearAlgebra { horizontal = 0; vertical = 0; } - // public Direction(Angle horizontal, Angle vertical) { - // this.horizontal = horizontal.inDegrees; - - // } - // public Direction(float horizontal, float vertical) { - // this.horizontal = horizontal; - // this.vertical = vertical; - // //Normalize(); - // } + public Direction(Angle horizontal, Angle vertical) { + this.horizontal = horizontal.inDegrees; + this.vertical = vertical.inDegrees; + this.Normalize(); + } public static Direction Degrees(float horizontal, float vertical) { Direction d = new() { @@ -82,6 +78,33 @@ namespace LinearAlgebra { Vector3Float v = new(x, y, z); return v; } + + public static bool operator ==(Direction d1, Direction d2) { + bool horizontalEq = d1.horizontal == d2.horizontal; + bool verticalEq = d1.vertical == d2.vertical; + return horizontalEq && verticalEq; + } + + public static bool operator !=(Direction d1, Direction d2) { + bool horizontalNEq = d1.horizontal != d2.horizontal; + bool verticalNEq = d1.vertical != d2.vertical; + return horizontalNEq || verticalNEq; + } + + public override bool Equals(object obj) { + if (obj is not Direction d) + return false; + + bool horizontalEq = this.horizontal == d.horizontal; + bool verticalEq = this.vertical == d.vertical; + return horizontalEq && verticalEq; + } + + + public override int GetHashCode() { + return (this.horizontal, this.vertical).GetHashCode(); + } + } } \ No newline at end of file diff --git a/LinearAlgebra/test/AngleTest.cs b/LinearAlgebra/test/AngleTest.cs index 00e4981..67110e4 100644 --- a/LinearAlgebra/test/AngleTest.cs +++ b/LinearAlgebra/test/AngleTest.cs @@ -35,7 +35,7 @@ namespace LinearAlgebra.Test { angle = (float)Math.PI * 1.5f; a = Angle.Radians(angle); - Assert.AreEqual(-Math.PI * 0.5f, a.inRadians); + Assert.AreEqual(-Math.PI * 0.5f, a.inRadians, 1.0E-05F); // Revolutions angle = 0.0f; diff --git a/LinearAlgebra/test/DirectionTest.cs b/LinearAlgebra/test/DirectionTest.cs index 3cebe1a..2e099af 100644 --- a/LinearAlgebra/test/DirectionTest.cs +++ b/LinearAlgebra/test/DirectionTest.cs @@ -9,10 +9,12 @@ namespace LinearAlgebra.Test { [Test] public void Compare() { - Direction d = Direction.Degrees(45, 135); + Direction d1 = Direction.Degrees(45, 135); + Direction d2 = new(Angle.Degrees(45), Angle.Degrees(135)); bool r; - r = d == new Direction(Angle.Degrees(45), Angle.Degrees(135)); + r = d1 == d2; Assert.True(r); + Assert.AreEqual(d1, d2); } }; } diff --git a/LinearAlgebra/test/SphericalTest.cs b/LinearAlgebra/test/SphericalTest.cs index 8539dc0..4ec4429 100644 --- a/LinearAlgebra/test/SphericalTest.cs +++ b/LinearAlgebra/test/SphericalTest.cs @@ -46,7 +46,7 @@ namespace LinearAlgebra.Test { r = v1 + v2; Assert.AreEqual(Math.Sqrt(2), r.distance, 1.0E-05F, "Addition(1 0 90)"); Assert.AreEqual(45.0f, r.direction.horizontal, "Addition(1 0 90)"); - Assert.AreEqual(45.0f, r.direction.vertical, "Addition(1 0 90)"); + Assert.AreEqual(45.0f, r.direction.vertical, 1.0E-05F, "Addition(1 0 90)"); } } } diff --git a/src/Things/TouchSensor.cs b/src/Things/TouchSensor.cs index 4235c30..cdeb0dc 100644 --- a/src/Things/TouchSensor.cs +++ b/src/Things/TouchSensor.cs @@ -70,7 +70,7 @@ namespace RoboidControl { touchUpdated = false; return bytes; #else - return 0; + return Array.Empty(); #endif }