607 lines
20 KiB
C#
Raw Permalink Normal View History

2011-10-31 05:36:24 +00:00
using System;
using System.Globalization;
using ANX.Framework.NonXNA.Development;
using System.ComponentModel;
using ANX.Framework.Design;
using ANX.Framework.NonXNA;
2011-10-31 05:36:24 +00:00
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
2011-10-31 05:36:24 +00:00
namespace ANX.Framework
{
[PercentageComplete(100)]
[Developer("xToast, GinieDp")]
[TestState(TestStateAttribute.TestState.Tested)]
#if !WINDOWSMETRO
[Serializable]
[TypeConverter(typeof(Vector2Converter))]
#endif
2011-10-31 05:36:24 +00:00
public struct Vector2 : IEquatable<Vector2>
{
#region fields
public float X;
public float Y;
#endregion
#region properties
#region One
/// <summary>
/// Returns a <see cref="Vector2"/> with both of its components set to one.
/// </summary>
public static Vector2 One
{
get
{
return new Vector2(1f, 1f);
}
}
#endregion
#region Zero
/// <summary>
/// Returns a <see cref="Vector2"/> with both of its components set to zero.
/// </summary>
public static Vector2 Zero
{
get
{
return new Vector2(0f, 0f);
}
}
#endregion
#region UnitX
/// <summary>
2011-10-31 05:36:24 +00:00
/// Returns the unit vector for the x-axis.
/// </summary>
public static Vector2 UnitX
{
get
{
return new Vector2(1f, 0f);
}
}
#endregion
#region UnitY
2011-10-31 05:36:24 +00:00
/// <summary>
/// Returns the unit vector for the y-axis.
/// </summary>
public static Vector2 UnitY
{
get
{
return new Vector2(0f, 1f);
}
}
#endregion
2011-10-31 05:36:24 +00:00
#endregion
#region constructors
public Vector2(float value)
2011-10-31 05:36:24 +00:00
{
this.X = value;
this.Y = value;
}
public Vector2(float x, float y)
{
this.X = x;
this.Y = y;
}
#endregion
#region public methods
#region Add
public static Vector2 Add(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Add(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Add(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = value1.X + value2.X;
result.Y = value1.Y + value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Barycentric
public static Vector2 Barycentric(Vector2 value1, Vector2 value2, Vector2 value3, float amount1, float amount2)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Barycentric(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, float amount1,
float amount2, out Vector2 result)
{
result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2);
result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2);
}
#endregion
2011-10-31 05:36:24 +00:00
#region CatmullRom
public static Vector2 CatmullRom(Vector2 value1, Vector2 value2, Vector2 value3, Vector2 value4, float amount)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void CatmullRom(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, ref Vector2 value4,
float amount, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount);
result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount);
}
#endregion
2011-10-31 05:36:24 +00:00
#region Clamp
public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Clamp(ref value1, ref min, ref max, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Clamp(ref Vector2 value1, ref Vector2 min,
ref Vector2 max, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = MathHelper.Clamp(value1.X, min.X, max.X);
result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y);
}
#endregion
2011-10-31 05:36:24 +00:00
#region Distance
public static float Distance(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
float result;
Distance(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Distance(ref Vector2 value1, ref Vector2 value2, out float result)
2011-10-31 05:36:24 +00:00
{
Vector2 tmp;
Subtract(ref value1, ref value2, out tmp);
result = tmp.Length();
}
#endregion
2011-10-31 05:36:24 +00:00
#region DistanceSquared
public static float DistanceSquared(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
float result;
DistanceSquared(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
2011-10-31 05:36:24 +00:00
{
Vector2 tmp;
Subtract(ref value1, ref value2, out tmp);
result = tmp.LengthSquared();
}
#endregion
2011-10-31 05:36:24 +00:00
#region Divide
public static Vector2 Divide(Vector2 value1, float divider)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Divide(ref value1, divider, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Divide(ref Vector2 value1, float divider, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
divider = 1f / divider;
result.X = value1.X * divider;
result.Y = value1.Y * divider;
2011-10-31 05:36:24 +00:00
}
2011-10-31 05:36:24 +00:00
public static Vector2 Divide(Vector2 value1, Vector2 value2)
{
Vector2 result;
Divide(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Divide(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = value1.X / value2.X;
result.Y = value1.Y / value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Dot
public static float Dot(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
return value1.X * value2.X + value1.Y * value2.Y;
2011-10-31 05:36:24 +00:00
}
public static void Dot(ref Vector2 value1, ref Vector2 value2, out float result)
2011-10-31 05:36:24 +00:00
{
result = value1.X * value2.X + value1.Y * value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region GetHashCode
public override int GetHashCode()
2011-10-31 05:36:24 +00:00
{
return this.X.GetHashCode() + this.Y.GetHashCode();
}
#endregion
2011-10-31 05:36:24 +00:00
#region Hermite
public static Vector2 Hermite(Vector2 value1, Vector2 tangent1, Vector2 value2, Vector2 tangent2, float amount)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Hermite(ref Vector2 value1, ref Vector2 tangent1, ref Vector2 value2, ref Vector2 tangent2,
float amount, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
}
#endregion
2011-10-31 05:36:24 +00:00
#region Length
public float Length()
2011-10-31 05:36:24 +00:00
{
return (float)Math.Sqrt((this.X * this.X) + (this.Y * this.Y));
}
#endregion
2011-10-31 05:36:24 +00:00
#region LengthSquared
public float LengthSquared()
2011-10-31 05:36:24 +00:00
{
return (this.X * this.X) + (this.Y * this.Y);
}
#endregion
2011-10-31 05:36:24 +00:00
#region Lerp
public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Lerp(ref value1, ref value2, amount, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Lerp(ref Vector2 value1, ref Vector2 value2,
float amount, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = value1.X + (value2.X - value1.X) * amount;
result.Y = value1.Y + (value2.Y - value1.Y) * amount;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Max
public static Vector2 Max(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Max(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Max(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = (value1.X > value2.X) ? value1.X : value2.X;
result.Y = (value1.Y > value2.Y) ? value1.Y : value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Min
public static Vector2 Min(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Min(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Min(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = (value1.X < value2.X) ? value1.X : value2.X;
result.Y = (value1.Y < value2.Y) ? value1.Y : value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Multiply
public static Vector2 Multiply(Vector2 value1, float scaleFactor)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Multiply(ref value1, scaleFactor, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Multiply(ref Vector2 value1, float scaleFactor, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = value1.X * scaleFactor;
result.Y = value1.Y * scaleFactor;
2011-10-31 05:36:24 +00:00
}
2011-10-31 05:36:24 +00:00
public static Vector2 Multiply(Vector2 value1, Vector2 value2)
{
Vector2 result;
Multiply(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Multiply(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = value1.X * value2.X;
result.Y = value1.Y * value2.Y;
}
#endregion
2011-10-31 05:36:24 +00:00
#region Negate
public static Vector2 Negate(Vector2 value)
2011-10-31 05:36:24 +00:00
{
// Is a bit faster than copying the vector floats to the operator method.
return new Vector2(-value.X, -value.Y);
2011-10-31 05:36:24 +00:00
}
public static void Negate(ref Vector2 value, out Vector2 result)
{
// Is a bit faster than copying the vector floats to the operator method.
result.X = -value.X;
result.Y = -value.Y;
}
#endregion
#region Normalize
public void Normalize()
2011-10-31 05:36:24 +00:00
{
float divider = 1f / Length();
2011-10-31 05:36:24 +00:00
this.X *= divider;
this.Y *= divider;
}
2011-10-31 05:36:24 +00:00
public static Vector2 Normalize(Vector2 value)
{
float divider = 1f / value.Length();
return new Vector2(value.X * divider, value.Y * divider);
2011-10-31 05:36:24 +00:00
}
2011-10-31 05:36:24 +00:00
public static void Normalize(ref Vector2 value, out Vector2 result)
{
float divider = 1f / value.Length();
result.X = value.X * divider;
result.Y = value.Y * divider;
}
#endregion
2011-10-31 05:36:24 +00:00
/*
Vect2 = Vect1 - 2 * WallN * (WallN DOT Vect1)
Formula from : http://www.gamedev.net/topic/165537-2d-vector-reflection-/
*/
#region Reflect
public static Vector2 Reflect(Vector2 vector, Vector2 normal)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Reflect(ref vector, ref normal, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
float sub = 2 * Dot(vector, normal);
2011-10-31 05:36:24 +00:00
result.X = vector.X - (sub * normal.X);
result.Y = vector.Y - (sub * normal.Y);
}
#endregion
2011-10-31 05:36:24 +00:00
#region SmoothStep
public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, float amount)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
SmoothStep(ref value1, ref value2, amount, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = MathHelper.SmoothStep(value1.X, value2.X, amount);
result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount);
}
#endregion
2011-10-31 05:36:24 +00:00
#region Subtract
public static Vector2 Subtract(Vector2 value1, Vector2 value2)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Subtract(ref value1, ref value2, out result);
2011-10-31 05:36:24 +00:00
return result;
}
public static void Subtract(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
{
result.X = value1.X - value2.X;
result.Y = value1.Y - value2.Y;
}
#endregion
#region ToString
public override string ToString()
{
var culture = CultureInfo.CurrentCulture;
// This may look a bit more ugly, but String.Format should be avoided cause of it's bad performance!
return "{X:" + X.ToString(culture) + " Y:" + Y.ToString(culture) + "}";
}
#endregion
#region Transform
public static Vector2 Transform(Vector2 position, Matrix matrix)
2011-10-31 05:36:24 +00:00
{
Vector2 result;
Transform(ref position, ref matrix, out result);
return result;
2011-10-31 05:36:24 +00:00
}
public static void Transform(ref Vector2 position, ref Matrix matrix, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
result.X = (position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41;
result.Y = (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42;
2011-10-31 05:36:24 +00:00
}
public static Vector2 Transform(Vector2 value, Quaternion rotation)
{
Vector2 result;
Transform(ref value, ref rotation, out result);
return result;
2011-10-31 05:36:24 +00:00
}
public static void Transform(ref Vector2 value, ref Quaternion rotation, out Vector2 result)
2011-10-31 05:36:24 +00:00
{
float x = 2 * (-rotation.Z * value.Y);
float y = 2 * (rotation.Z * value.X);
float z = 2 * (rotation.X * value.Y - rotation.Y * value.X);
result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y);
result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z);
2011-10-31 05:36:24 +00:00
}
public static void Transform(Vector2[] sourceArray, int sourceIndex, ref Matrix matrix,
Vector2[] destinationArray, int destinationIndex, int length)
2011-10-31 05:36:24 +00:00
{
length += sourceIndex;
for (int i = sourceIndex; i < length; i++, destinationIndex++)
{
Transform(ref sourceArray[i], ref matrix, out destinationArray[destinationIndex]);
}
2011-10-31 05:36:24 +00:00
}
public static void Transform(Vector2[] sourceArray, int sourceIndex, ref Quaternion rotation,
Vector2[] destinationArray, int destinationIndex, int length)
2011-10-31 05:36:24 +00:00
{
length += sourceIndex;
for (int i = sourceIndex; i < length; i++, destinationIndex++)
{
Transform(ref sourceArray[i], ref rotation, out destinationArray[destinationIndex]);
}
2011-10-31 05:36:24 +00:00
}
public static void Transform(Vector2[] sourceArray, ref Matrix matrix, Vector2[] destinationArray)
2011-10-31 05:36:24 +00:00
{
for (int i = 0; i < sourceArray.Length; i++)
{
Transform(ref sourceArray[i], ref matrix, out destinationArray[i]);
}
2011-10-31 05:36:24 +00:00
}
public static void Transform(Vector2[] sourceArray, ref Quaternion rotation, Vector2[] destinationArray)
2011-10-31 05:36:24 +00:00
{
for (int i = 0; i < sourceArray.Length; i++)
{
Transform(ref sourceArray[i], ref rotation, out destinationArray[i]);
}
}
#endregion
#region TransformNormal
public static Vector2 TransformNormal(Vector2 normal, Matrix matrix)
{
Vector2 result;
TransformNormal(ref normal, ref matrix, out result);
return result;
}
public static void TransformNormal(ref Vector2 normal, ref Matrix matrix, out Vector2 result)
{
result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21);
result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22);
}
public static void TransformNormal(Vector2[] sourceArray, int sourceIndex, ref Matrix matrix,
Vector2[] destinationArray, int destinationIndex, int length)
{
length += sourceIndex;
for (int i = sourceIndex; i < length; i++, destinationIndex++)
{
TransformNormal(ref sourceArray[i], ref matrix, out destinationArray[destinationIndex]);
}
}
public static void TransformNormal(Vector2[] sourceArray, ref Matrix matrix, Vector2[] destinationArray)
{
for (int i = 0; i < sourceArray.Length; i++)
{
TransformNormal(ref sourceArray[i], ref matrix, out destinationArray[i]);
}
}
#endregion
2011-10-31 05:36:24 +00:00
#endregion
#region IEquatable implementation
public override bool Equals(Object obj)
{
return obj is Vector2 && Equals((Vector2)obj);
2011-10-31 05:36:24 +00:00
}
public bool Equals(Vector2 other)
{
return this.X == other.X && this.Y == other.Y;
}
#endregion
#region operator overloading
public static Vector2 operator +(Vector2 value1, Vector2 value2)
{
2011-10-31 05:36:24 +00:00
return new Vector2(value1.X + value2.X, value1.Y + value2.Y);
}
public static Vector2 operator /(Vector2 value1, float divider)
{
float factor = 1f / divider;
return new Vector2(value1.X * factor, value1.Y * factor);
2011-10-31 05:36:24 +00:00
}
2011-10-31 05:36:24 +00:00
public static Vector2 operator /(Vector2 value1, Vector2 value2)
{
return new Vector2(value1.X / value2.X, value1.Y / value2.Y);
}
public static bool operator ==(Vector2 value1, Vector2 value2)
{
return (value1.X == value2.X) && (value1.Y == value2.Y);
2011-10-31 05:36:24 +00:00
}
2011-10-31 05:36:24 +00:00
public static bool operator !=(Vector2 value1, Vector2 value2)
{
return (value1.X != value2.X) || (value1.Y != value2.Y);
2011-10-31 05:36:24 +00:00
}
public static Vector2 operator *(float scaleFactor, Vector2 value)
{
return new Vector2(value.X * scaleFactor, value.Y * scaleFactor);
}
2011-10-31 05:36:24 +00:00
public static Vector2 operator *(Vector2 value, float scaleFactor)
{
return new Vector2(value.X * scaleFactor, value.Y * scaleFactor);
}
2011-10-31 05:36:24 +00:00
public static Vector2 operator *(Vector2 value1, Vector2 value2)
{
return new Vector2(value1.X * value2.X, value1.Y * value2.Y);
}
public static Vector2 operator -(Vector2 value1, Vector2 value2)
{
return new Vector2(value1.X - value2.X, value1.Y - value2.Y);
}
2011-10-31 05:36:24 +00:00
public static Vector2 operator -(Vector2 value)
{
return new Vector2(-value.X, -value.Y);
}
#endregion
}
}