#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion
using System;
using System.Runtime.InteropServices;
namespace OpenTK.Math
{
/// Represents a 2D vector using two single-precision floating-point numbers.
///
/// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.
///
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector2 : IEquatable
{
#region Fields
///
/// The X component of the Vector2.
///
public float X;
///
/// The Y component of the Vector2.
///
public float Y;
#endregion
#region Constructors
///
/// Constructs a new Vector2.
///
/// The x coordinate of the net Vector2.
/// The y coordinate of the net Vector2.
public Vector2(float x, float y)
{
X = x;
Y = y;
}
///
/// Constructs a new Vector2 from the given Vector2.
///
/// The Vector2 to copy components from.
[Obsolete]
public Vector2(Vector2 v)
{
X = v.X;
Y = v.Y;
}
///
/// Constructs a new Vector2 from the given Vector3.
///
/// The Vector3 to copy components from. Z is discarded.
[Obsolete]
public Vector2(Vector3 v)
{
X = v.X;
Y = v.Y;
}
///
/// Constructs a new Vector2 from the given Vector4.
///
/// The Vector4 to copy components from. Z and W are discarded.
[Obsolete]
public Vector2(Vector4 v)
{
X = v.X;
Y = v.Y;
}
#endregion
#region Public Members
#region Instance
#region public void Add()
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
public void Add( Vector2 right )
{
this.X += right.X;
this.Y += right.Y;
}
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
public void Add( ref Vector2 right )
{
this.X += right.X;
this.Y += right.Y;
}
#endregion public void Add()
#region public void Sub()
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
public void Sub( Vector2 right )
{
this.X -= right.X;
this.Y -= right.Y;
}
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
public void Sub( ref Vector2 right )
{
this.X -= right.X;
this.Y -= right.Y;
}
#endregion public void Sub()
#region public void Mult()
/// Multiply this instance by a scalar.
/// Scalar operand.
public void Mult( float f )
{
this.X *= f;
this.Y *= f;
}
#endregion public void Mult()
#region public void Div()
/// Divide this instance by a scalar.
/// Scalar operand.
public void Div( float f )
{
float mult = 1.0f / f;
this.X *= mult;
this.Y *= mult;
}
#endregion public void Div()
#region public float Length
///
/// Gets the length (magnitude) of the vector.
///
///
///
public float Length
{
get
{
return (float)System.Math.Sqrt(X * X + Y * Y);
}
}
#endregion
#region public float LengthFast
///
/// Gets an approximation of the vector length (magnitude).
///
///
/// This property uses an approximation of the square root function to calculate vector magnitude, with
/// an upper error bound of 0.001.
///
///
///
public float LengthFast
{
get
{
return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y);
}
}
#endregion
#region public float LengthSquared
///
/// Gets the square of the vector length (magnitude).
///
///
/// This property avoids the costly square root operation required by the Length property. This makes it more suitable
/// for comparisons.
///
///
///
public float LengthSquared
{
get
{
return X * X + Y * Y;
}
}
#endregion
#region public Vector2 PerpendicularRight
///
/// Gets the perpendicular vector on the right side of this vector.
///
public Vector2 PerpendicularRight
{
get
{
return new Vector2(Y, -X);
}
}
#endregion
#region public Vector2 PerpendicularLeft
///
/// Gets the perpendicular vector on the left side of this vector.
///
public Vector2 PerpendicularLeft
{
get
{
return new Vector2(-Y, X);
}
}
#endregion
#region public void Normalize()
///
/// Scales the Vector2 to unit length.
///
public void Normalize()
{
float scale = 1.0f / this.Length;
X *= scale;
Y *= scale;
}
#endregion
#region public void NormalizeFast()
///
/// Scales the Vector2 to approximately unit length.
///
public void NormalizeFast()
{
float scale = Functions.InverseSqrtFast(X * X + Y * Y);
X *= scale;
Y *= scale;
}
#endregion
#region public void Scale()
///
/// Scales the current Vector2 by the given amounts.
///
/// The scale of the X component.
/// The scale of the Y component.
public void Scale(float sx, float sy)
{
this.X = X * sx;
this.Y = Y * sy;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
public void Scale( Vector2 scale )
{
this.X *= scale.X;
this.Y *= scale.Y;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
[CLSCompliant(false)]
public void Scale( ref Vector2 scale )
{
this.X *= scale.X;
this.Y *= scale.Y;
}
#endregion public void Scale()
#endregion
#region Static
#region Fields
///
/// Defines a unit-length Vector2 that points towards the X-axis.
///
public static readonly Vector2 UnitX = new Vector2(1, 0);
///
/// Defines a unit-length Vector2 that points towards the Y-axis.
///
public static readonly Vector2 UnitY = new Vector2(0, 1);
///
/// Defines a zero-length Vector2.
///
public static readonly Vector2 Zero = new Vector2(0, 0);
///
/// Defines the size of the Vector2 struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2());
#endregion
#region Add
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static Vector2 Add(Vector2 a, Vector2 b)
{
a.X += b.X;
a.Y += b.Y;
return a;
}
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static void Add(ref Vector2 a, ref Vector2 b, out Vector2 result)
{
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
}
#endregion
#region Sub
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static Vector2 Sub(Vector2 a, Vector2 b)
{
a.X -= b.X;
a.Y -= b.Y;
return a;
}
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static void Sub(ref Vector2 a, ref Vector2 b, out Vector2 result)
{
result.X = a.X - b.X;
result.Y = a.Y - b.Y;
}
#endregion
#region Mult
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static Vector2 Mult(Vector2 a, float f)
{
a.X *= f;
a.Y *= f;
return a;
}
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static void Mult(ref Vector2 a, float f, out Vector2 result)
{
result.X = a.X * f;
result.Y = a.Y * f;
}
#endregion
#region Div
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static Vector2 Div(Vector2 a, float f)
{
float mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
return a;
}
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static void Div(ref Vector2 a, float f, out Vector2 result)
{
float mult = 1.0f / f;
result.X = a.X * mult;
result.Y = a.Y * mult;
}
#endregion
#region ComponentMin
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static Vector2 ComponentMin(Vector2 a, Vector2 b)
{
a.X = a.X < b.X ? a.X : b.X;
a.Y = a.Y < b.Y ? a.Y : b.Y;
return a;
}
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static void ComponentMin(ref Vector2 a, ref Vector2 b, out Vector2 result)
{
result.X = a.X < b.X ? a.X : b.X;
result.Y = a.Y < b.Y ? a.Y : b.Y;
}
#endregion
#region ComponentMax
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static Vector2 ComponentMax(Vector2 a, Vector2 b)
{
a.X = a.X > b.X ? a.X : b.X;
a.Y = a.Y > b.Y ? a.Y : b.Y;
return a;
}
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static void ComponentMax(ref Vector2 a, ref Vector2 b, out Vector2 result)
{
result.X = a.X > b.X ? a.X : b.X;
result.Y = a.Y > b.Y ? a.Y : b.Y;
}
#endregion
#region Min
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static Vector2 Min(Vector2 left, Vector2 right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
#endregion
#region Max
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static Vector2 Max(Vector2 left, Vector2 right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static Vector2 Clamp(Vector2 vec, Vector2 min, Vector2 max)
{
vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
return vec;
}
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static void Clamp(ref Vector2 vec, ref Vector2 min, ref Vector2 max, out Vector2 result)
{
result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
}
#endregion
#region Normalize
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static Vector2 Normalize(Vector2 vec)
{
float scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
return vec;
}
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static void Normalize(ref Vector2 vec, out Vector2 result)
{
float scale = 1.0f / vec.Length;
result.X = vec.X * scale;
result.Y = vec.Y * scale;
}
#endregion
#region NormalizeFast
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static Vector2 NormalizeFast(Vector2 vec)
{
float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
vec.X *= scale;
vec.Y *= scale;
return vec;
}
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static void NormalizeFast(ref Vector2 vec, out Vector2 result)
{
float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
result.X = vec.X * scale;
result.Y = vec.Y * scale;
}
#endregion
#region Dot
///
/// Calculate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static float Dot(Vector2 left, Vector2 right)
{
return left.X * right.X + left.Y * right.Y;
}
///
/// Calculate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static void Dot( ref Vector2 left, ref Vector2 right, out float result )
{
result = left.X * right.X + left.Y * right.Y;
}
#endregion
#region Lerp
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static Vector2 Lerp(Vector2 a, Vector2 b, float blend)
{
a.X = blend * (b.X - a.X) + a.X;
a.Y = blend * (b.Y - a.Y) + a.Y;
return a;
}
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static void Lerp( ref Vector2 a, ref Vector2 b, float blend, out Vector2 result )
{
result.X = blend * ( b.X - a.X ) + a.X;
result.Y = blend * ( b.Y - a.Y ) + a.Y;
}
#endregion
#region Barycentric
///
/// Interpolate 3 Vectors using Barycentric coordinates
///
/// First input Vector
/// Second input Vector
/// Third input Vector
/// First Barycentric Coordinate
/// Second Barycentric Coordinate
/// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise
public static Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float v)
{
return a + u * (b - a) + v * (c - a);
}
/// Interpolate 3 Vectors using Barycentric coordinates
/// First input Vector.
/// Second input Vector.
/// Third input Vector.
/// First Barycentric Coordinate.
/// Second Barycentric Coordinate.
/// Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise
public static void BaryCentric( ref Vector2 a, ref Vector2 b, ref Vector2 c, float u, float v, out Vector2 result )
{
result = a; // copy
Vector2 temp = b; // copy
temp.Sub( ref a );
temp.Mult( u );
result.Add( ref temp );
temp = c; // copy
temp.Sub( ref a );
temp.Mult( v );
result.Add( ref temp );
}
#endregion
#endregion
#region Operators
public static Vector2 operator +(Vector2 left, Vector2 right)
{
left.X += right.X;
left.Y += right.Y;
return left;
}
public static Vector2 operator -(Vector2 left, Vector2 right)
{
left.X -= right.X;
left.Y -= right.Y;
return left;
}
public static Vector2 operator -(Vector2 vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
return vec;
}
public static Vector2 operator *(Vector2 vec, float f)
{
vec.X *= f;
vec.Y *= f;
return vec;
}
public static Vector2 operator *(float f, Vector2 vec)
{
vec.X *= f;
vec.Y *= f;
return vec;
}
public static Vector2 operator /(Vector2 vec, float f)
{
float mult = 1.0f / f;
vec.X *= mult;
vec.Y *= mult;
return vec;
}
public static bool operator ==(Vector2 left, Vector2 right)
{
return left.Equals(right);
}
public static bool operator !=(Vector2 left, Vector2 right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector2.
///
///
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
#endregion
#region public override int GetHashCode()
///
/// Returns the hashcode for this instance.
///
/// A System.Int32 containing the unique hashcode for this instance.
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
///
/// Indicates whether this instance and a specified object are equal.
///
/// The object to compare to.
/// True if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
if (!(obj is Vector2))
return false;
return this.Equals((Vector2)obj);
}
#endregion
#endregion
#endregion
#region IEquatable Members
/// Indicates whether the current vector is equal to another vector.
/// A vector to compare with this vector.
/// true if the current vector is equal to the vector parameter; otherwise, false.
public bool Equals(Vector2 other)
{
return
X == other.X &&
Y == other.Y;
}
#endregion
}
}