2019-07-21 06:11:50 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
namespace Revitalize.Framework.Utilities
|
|
|
|
{
|
|
|
|
public static class Vector2Utilities
|
|
|
|
{
|
|
|
|
public static double Magnitude(this Vector2 vec)
|
|
|
|
{
|
|
|
|
return Math.Sqrt(Math.Pow(vec.X, 2) + Math.Pow(vec.Y, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the unit vector.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="vec"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static Vector2 UnitVector(this Vector2 vec)
|
|
|
|
{
|
2019-07-23 11:46:33 +08:00
|
|
|
if (vec == Vector2.Zero) return Vector2.Zero;
|
2019-07-21 06:11:50 +08:00
|
|
|
double mag = Magnitude(vec);
|
2019-07-23 11:46:33 +08:00
|
|
|
if (mag == 0) return Vector2.Zero;
|
2019-07-21 06:11:50 +08:00
|
|
|
return new Vector2((float)(vec.X / mag),(float)(vec.Y / mag));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|