completed a handful of vector2 extension methods

This commit is contained in:
Dylan Wilson
2015-12-21 15:41:31 +10:00
parent 8c932548b2
commit 041a57138e
2 changed files with 53 additions and 0 deletions
@@ -63,5 +63,40 @@ namespace MonoGame.Extended.Tests
Assert.IsTrue(new Vector2(7.071068f, -7.071068f).EqualsWithTolerance(b));
}
[Test]
public void Vector2_Truncate_Test()
{
var a = new Vector2(10, 10);
var b = a.Truncate(5);
Assert.AreEqual(5f, b.Length(), 0.001f);
}
[Test]
public void Vector2_IsNaN_Test()
{
var a = new Vector2(float.NaN, 10);
var b = new Vector2(10, float.NaN);
var c = new Vector2(float.NaN, float.NaN);
var d = new Vector2(10, 10);
Assert.IsTrue(a.IsNaN());
Assert.IsTrue(b.IsNaN());
Assert.IsTrue(c.IsNaN());
Assert.IsFalse(d.IsNaN());
}
[Test]
public void Vector2_ToAngle_Test()
{
var a = new Vector2(0, -10);
var b = new Vector2(10, 0);
var c = -Vector2.UnitY.Rotate(MathHelper.ToRadians(45));
Assert.AreEqual(MathHelper.ToRadians(0), a.ToAngle());
Assert.AreEqual(MathHelper.ToRadians(90), b.ToAngle());
Assert.AreEqual(MathHelper.ToRadians(45), c.ToAngle());
}
}
}
@@ -33,5 +33,23 @@ namespace MonoGame.Extended
{
return new Vector2(vector2.Y, -vector2.X);
}
public static Vector2 Truncate(this Vector2 vector2, float maxLength)
{
if (vector2.LengthSquared() > maxLength * maxLength)
return vector2.NormalizedCopy() * maxLength;
return vector2;
}
public static bool IsNaN(this Vector2 vector2)
{
return float.IsNaN(vector2.X) || float.IsNaN(vector2.Y);
}
public static float ToAngle(this Vector2 vector2)
{
return (float) Math.Atan2(vector2.X, -vector2.Y);
}
}
}