Opentk/Source/OpenTK/Math/Vector2.cs

736 lines
21 KiB
C#
Raw Normal View History

#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
{
/// <summary>
/// Represents a 2D vector.
/// </summary>
/// <remarks>
/// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.
/// </remarks>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector2 : IEquatable<Vector2>
{
2007-09-24 20:11:44 +00:00
#region Fields
/// <summary>
2007-09-24 20:11:44 +00:00
/// The X component of the Vector2.
/// </summary>
public float X;
/// <summary>
2007-09-24 20:11:44 +00:00
/// The Y component of the Vector2.
/// </summary>
public float Y;
2007-09-24 20:11:44 +00:00
#endregion
#region Constructors
/// <summary>
/// Constructs a new Vector2.
/// </summary>
/// <param name="x">The x coordinate of the net Vector2.</param>
/// <param name="y">The y coordinate of the net Vector2.</param>
public Vector2(float x, float y)
{
2007-11-04 15:34:04 +00:00
X = x;
Y = y;
}
/// <summary>
/// Constructs a new Vector2 from the given Vector2.
/// </summary>
/// <param name="v">The Vector2 to copy components from.</param>
[Obsolete]
public Vector2(Vector2 v)
{
X = v.X;
Y = v.Y;
}
/// <summary>
/// Constructs a new Vector2 from the given Vector3.
/// </summary>
/// <param name="v">The Vector3 to copy components from. Z is discarded.</param>
[Obsolete]
public Vector2(Vector3 v)
{
X = v.X;
Y = v.Y;
}
/// <summary>
/// Constructs a new Vector2 from the given Vector4.
/// </summary>
/// <param name="v">The Vector4 to copy components from. Z and W are discarded.</param>
[Obsolete]
public Vector2(Vector4 v)
{
X = v.X;
Y = v.Y;
}
2007-09-24 20:11:44 +00:00
#endregion
#region Public Members
#region Instance
#region public float Length
/// <summary>
/// Gets the length (magnitude) of the vector.
/// </summary>
/// <see cref="FastLength"/>
/// <seealso cref="LengthSquared"/>
public float Length
{
2007-11-04 15:34:04 +00:00
get
{
return (float)System.Math.Sqrt(X * X + Y * Y);
}
}
2007-09-24 20:11:44 +00:00
#endregion
#region public float LengthFast
/// <summary>
/// Gets an approximation of the vector length (magnitude).
/// </summary>
/// <remarks>
/// This property uses an approximation of the square root function to calculate vector magnitude, with
/// an upper error bound of 0.001.
/// </remarks>
/// <see cref="Length"/>
/// <seealso cref="LengthSquared"/>
/// <seealso cref="OpenTK.Math.FastSqrt"/>
public float LengthFast
{
2007-11-04 15:34:04 +00:00
get
{
return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y);
}
}
#endregion
#region public float LengthSquared
/// <summary>
/// Gets the square of the vector length (magnitude).
/// </summary>
/// <remarks>
/// This property avoids the costly square root operation required by the Length property. This makes it more suitable
/// for comparisons.
/// </remarks>
/// <see cref="Length"/>
/// <seealso cref="FastLength"/>
public float LengthSquared
{
2007-11-04 15:34:04 +00:00
get
{
return X * X + Y * Y;
}
}
#endregion
#region public Vector2 PerpendicularRight
/// <summary>
/// Gets the perpendicular vector on the right side of this vector.
/// </summary>
public Vector2 PerpendicularRight
{
get
{
return new Vector2(Y, -X);
}
}
#endregion
#region public Vector2 PerpendicularLeft
/// <summary>
/// Gets the perpendicular vector on the left side of this vector.
/// </summary>
public Vector2 PerpendicularLeft
{
get
{
return new Vector2(-Y, X);
}
}
#endregion
2007-11-04 15:34:04 +00:00
#region public void Normalize()
/// <summary>
/// Scales the Vector2 to unit length.
/// </summary>
2007-11-04 15:34:04 +00:00
public void Normalize()
{
2007-11-04 15:34:04 +00:00
float scale = 1.0f / this.Length;
X *= scale;
Y *= scale;
}
#endregion
2007-11-04 15:34:04 +00:00
#region public void NormalizeFast()
/// <summary>
/// Scales the Vector2 to approximately unit length.
/// </summary>
2007-11-04 15:34:04 +00:00
public void NormalizeFast()
{
2007-11-04 15:34:04 +00:00
float scale = Functions.InverseSqrtFast(X * X + Y * Y);
X *= scale;
Y *= scale;
}
#endregion
2007-11-04 15:34:04 +00:00
#region public void Scale(float sx, float sy)
/// <summary>
/// Scales the current Vector2 by the given amounts.
/// </summary>
/// <param name="sx">The scale of the X component.</param>
/// <param name="sy">The scale of the Y component.</param>
2007-11-04 15:34:04 +00:00
public void Scale(float sx, float sy)
{
2007-11-04 15:34:04 +00:00
this.X = X * sx;
this.Y = Y * sy;
}
#endregion
#endregion
#region Static
#region Fields
2007-11-04 15:34:04 +00:00
/// <summary>
/// Defines a unit-length Vector2 that points towards the X-axis.
/// </summary>
public static readonly Vector2 UnitX = new Vector2(1, 0);
2007-11-04 15:34:04 +00:00
/// <summary>
/// Defines a unit-length Vector2 that points towards the Y-axis.
/// </summary>
public static readonly Vector2 UnitY = new Vector2(0, 1);
2007-11-04 15:34:04 +00:00
/// <summary>
/// Defines a zero-length Vector2.
/// </summary>
public static readonly Vector2 Zero = new Vector2(0, 0);
2007-11-04 15:34:04 +00:00
/// <summary>
/// Defines the size of the Vector2 struct in bytes.
/// </summary>
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2());
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Add
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of addition</returns>
public static Vector2 Add(Vector2 a, Vector2 b)
{
a.X += b.X;
a.Y += b.Y;
return a;
}
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of addition</param>
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
2007-11-04 15:34:04 +00:00
#region Sub
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of subtraction</returns>
public static Vector2 Sub(Vector2 a, Vector2 b)
{
a.X -= b.X;
a.Y -= b.Y;
return a;
}
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of subtraction</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
#region Mult
2007-11-04 15:34:04 +00:00
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the multiplication</returns>
public static Vector2 Mult(Vector2 a, float f)
{
a.X *= f;
a.Y *= f;
return a;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the multiplication</param>
public static void Mult(ref Vector2 a, float f, out Vector2 result)
{
result.X = a.X * f;
result.Y = a.Y * f;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Div
2007-11-04 15:34:04 +00:00
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the division</returns>
public static Vector2 Div(Vector2 a, float f)
{
float mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
return a;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the division</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region ComponentMin
2007-11-04 15:34:04 +00:00
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise minimum</returns>
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;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise minimum</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region ComponentMax
2007-11-04 15:34:04 +00:00
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise maximum</returns>
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;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise maximum</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Min
2007-11-04 15:34:04 +00:00
/// <summary>
/// Returns the Vector3 with the minimum magnitude
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>The minimum Vector3</returns>
public static Vector2 Min(Vector2 left, Vector2 right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Max
2007-11-04 15:34:04 +00:00
/// <summary>
/// Returns the Vector3 with the minimum magnitude
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>The minimum Vector3</returns>
public static Vector2 Max(Vector2 left, Vector2 right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Clamp
2007-11-04 15:34:04 +00:00
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <returns>The clamped vector</returns>
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;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <param name="result">The clamped vector</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Normalize
2007-11-04 15:34:04 +00:00
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
public static Vector2 Normalize(Vector2 vec)
{
float scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
return vec;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region NormalizeFast
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Dot
2007-11-04 15:34:04 +00:00
/// <summary>
/// Caclulate the dot (scalar) product of two vectors
/// </summary>
/// <param name="left">First operand</param>
/// <param name="right">Second operand</param>
/// <returns>The dot product of the two inputs</returns>
public static float Dot(Vector2 left, Vector2 right)
{
return left.X * right.X + left.Y * right.Y;
}
#endregion
2007-11-04 15:34:04 +00:00
#region Lerp
2007-11-04 15:34:04 +00:00
/// <summary>
/// Returns a new Vector that is the linear blend of the 2 given Vectors
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor</param>
/// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
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;
}
2007-11-04 15:34:04 +00:00
#endregion
#region Barycentric
/// <summary>
/// Interpolate 3 Vectors using Barycentric coordinates
/// </summary>
/// <param name="a">First input Vector</param>
/// <param name="b">Second input Vector</param>
/// <param name="c">Third input Vector</param>
/// <param name="u">First Barycentric Coordinate</param>
/// <param name="v">Second Barycentric Coordinate</param>
/// <returns>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</returns>
public static Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float v)
{
return a + u * (b - a) + v * (c - a);
}
#endregion
#endregion
#region Operators
public static Vector2 operator +(Vector2 left, Vector2 right)
2007-11-04 15:34:04 +00:00
{
left.X += right.X;
left.Y += right.Y;
return left;
2007-11-04 15:34:04 +00:00
}
public static Vector2 operator -(Vector2 left, Vector2 right)
{
left.X -= right.X;
left.Y -= right.Y;
return left;
}
2007-11-04 15:34:04 +00:00
public static Vector2 operator -(Vector2 vec)
2007-11-04 15:34:04 +00:00
{
vec.X = -vec.X;
vec.Y = -vec.Y;
return vec;
2007-11-04 15:34:04 +00:00
}
public static Vector2 operator *(Vector2 vec, float f)
{
vec.X *= f;
vec.Y *= f;
return vec;
}
2007-11-04 15:34:04 +00:00
public static Vector2 operator *(float f, Vector2 vec)
{
vec.X *= f;
vec.Y *= f;
return vec;
}
2007-11-04 15:34:04 +00:00
public static Vector2 operator /(Vector2 vec, float f)
2007-11-04 15:34:04 +00:00
{
float mult = 1.0f / f;
vec.X *= mult;
vec.Y *= mult;
return vec;
2007-11-04 15:34:04 +00:00
}
public static bool operator ==(Vector2 left, Vector2 right)
{
return left.Equals(right);
}
2007-11-04 15:34:04 +00:00
public static bool operator !=(Vector2 left, Vector2 right)
{
return !left.Equals(right);
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Overrides
#region public override string ToString()
/// <summary>
/// Returns a System.String that represents the current Vector2.
/// </summary>
/// <returns></returns>
2007-09-25 21:11:16 +00:00
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
#endregion
#region public override int GetHashCode()
/// <summary>
/// Returns the hashcode for this instance.
/// </summary>
/// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
/// <param name="obj">The object to compare to.</param>
/// <returns>True if the instances are equal; false otherwise.</returns>
public override bool Equals(object obj)
{
if (!(obj is Vector2))
return false;
return this.Equals((Vector2)obj);
}
#endregion
#endregion
#endregion
#region IEquatable<Vector2> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>
/// <param name="vector">A vector to compare with this vector.</param>
/// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
public bool Equals(Vector2 other)
{
return
X == other.X &&
Y == other.Y;
}
#endregion
}
}