2 using Microsoft.Xna.Framework;
6 public static class VectorExtensions
13 public static float Angle(
this Vector2 from, Vector2 to)
15 return (
float)Math.Acos(MathHelper.Clamp(Vector2.Dot(Vector2.Normalize(from), Vector2.Normalize(to)), -1f, 1f));
21 public static Vector2 Forward(
float radians,
float length = 1)
23 return new Vector2((
float)Math.Cos(radians), (
float)Math.Sin(radians)) * length;
29 public static Vector2 Backward(
float radians,
float length = 1)
31 return -Forward(radians, length);
37 public static Vector2 ForwardFlipped(
float radians,
float length = 1)
39 return new Vector2((
float)Math.Sin(radians), (
float)Math.Cos(radians)) * length;
45 public static Vector2 BackwardFlipped(
float radians,
float length = 1)
47 return -ForwardFlipped(radians, length);
53 public static Vector2
Right(
this Vector2 forward)
55 var normV = Vector2.Normalize(forward);
56 return new Vector2(normV.Y, -normV.X);
62 public static Vector2
Left(
this Vector2 forward)
64 return -forward.Right();
70 public static Vector2 TransformVector(
this Vector2 v, Vector2 up)
72 up = Vector2.Normalize(up);
73 return (up * v.Y) + (up.Right() * v.X);
79 public static Vector2 Flip(
this Vector2 v) =>
new Vector2(v.Y, v.X);
84 public static float Combine(
this Vector2 v) => v.X + v.Y;
86 public static Vector2 Clamp(
this Vector2 v, Vector2 min, Vector2 max)
88 return Vector2.Clamp(v, min, max);
91 public static bool NearlyEquals(
this Vector2 v, Vector2 other)
93 return MathUtils.NearlyEqual(v.X, other.X) && MathUtils.NearlyEqual(v.Y, other.Y);
96 public static Vector2 Pad(
this Vector2 v, Vector4 padding)
98 v.X += padding.X + padding.Z;
99 v.Y += padding.Y + padding.W;