fix rounding error when constructing position with negatives

drone
thatcosmonaut 2020-02-14 15:19:37 -08:00
parent 960f4626f9
commit 982de6a910
2 changed files with 19 additions and 3 deletions

View File

@ -38,8 +38,8 @@ namespace MoonTools.Core.Structs
public Position2D(float X, float Y)
{
_x = Floor(X);
_y = Floor(Y);
_x = Truncate(X);
_y = Truncate(Y);
remainder = new Vector2(Remainder(X), Remainder(Y));
}
@ -147,6 +147,11 @@ namespace MoonTools.Core.Structs
return (int)Math.Floor(value);
}
private static int Truncate(float value)
{
return (int)Math.Truncate(value);
}
public override bool Equals(object other)
{
if (other is Position2D otherPosition)

View File

@ -8,6 +8,17 @@ namespace Tests
{
public class Position2DTest
{
[Test]
public void Negatives()
{
var position = new Position2D(-3.6f, -4.2f);
position.X.Should().Be(-3);
position.Y.Should().Be(-4);
position.ToVector2().X.Should().Be(-3.6f);
position.ToVector2().Y.Should().Be(-4.2f);
}
[Test]
public void PositionVectorAddition()
{
@ -99,4 +110,4 @@ namespace Tests
(one != two).Should().BeTrue();
}
}
}
}