#region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math { /// /// Represents a 2D vector. /// /// /// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats. /// [StructLayout(LayoutKind.Sequential)] public struct Vector2 { #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. 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. 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. public Vector2(Vector4 v) { X = v.X; Y = v.Y; } #endregion #region Functions #region public Vector2 Add(Vector2 right) /// /// Adds the given Vector2 to the current Vector2. /// /// The right operand of the addition. /// The current Vector2, modified by the operation. public Vector2 Add(Vector2 right) { this.X = X + right.X; this.Y = Y + right.Y; return this; } #endregion #region public Vector2 Sub(Vector2 right) /// /// Subtracts the given Vector2 from the current Vector2. /// /// The right operand of the subtraction. /// The current Vector2, modified by the operation. public Vector2 Sub(Vector2 right) { this.X = X - right.X; this.Y = Y - right.Y; return this; } #endregion #region public float Dot(Vector2 right) /// /// Computes the dot product between the current Vector2 and the given Vector2. /// /// The right operand of the dot product. /// A float containing the result of the operation. public float Dot(Vector2 right) { return X * right.X + Y * right.Y; } #endregion #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 Normalize() /// /// Scales the Vector2 to unit length. /// /// The normalized version of the current vector. public Vector2 Normalize() { float scale = 1.0f / this.Length; X *= scale; Y *= scale; return this; } #endregion #region public Vector2 NormalizeFast() /// /// Scales the Vector2 to approximately unit length. /// /// The normalized version of the current vector. public Vector2 NormalizeFast() { float scale = Functions.InverseSqrtFast(X * X + Y * Y); X *= scale; Y *= scale; return this; } #endregion #region public Vector2 Scale(float sx, float sy) /// /// Scales the current Vector2 by the given amounts. /// /// The scale of the X component. /// The scale of the Y component. /// The current Vector2, scaled. public Vector2 Scale(float sx, float sy) { this.X = X * sx; this.Y = Y * sy; return this; } #endregion #endregion #region Operator overloads public static Vector2 operator +(Vector2 left, Vector2 right) { return new Vector2(left).Add(right); } public static Vector2 operator -(Vector2 left, Vector2 right) { return new Vector2(left).Sub(right); } [CLSCompliant(false)] unsafe public static explicit operator float*(Vector2 v) { return &v.X; } public static explicit operator IntPtr(Vector2 v) { unsafe { return (IntPtr)(&v.X); } } #endregion #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 } }