From 9e5de1161650aaf05fb1cd3d6654f4c99154dccc Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Wed, 24 Oct 2007 17:26:05 +0000 Subject: [PATCH] Improved the Vector2 interface. Vector2.Add/Sub/Dot/Scale no longer create temporary objects. Removed Vector3/4 overloads. --- Source/OpenTK/Math/Vector2.cs | 175 +++++++++++++++------------------- 1 file changed, 79 insertions(+), 96 deletions(-) diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index d502b4da..0b6f798a 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -14,6 +14,9 @@ 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 { @@ -23,7 +26,7 @@ namespace OpenTK.Math /// The X component of the Vector2. /// public float X; - + /// /// The Y component of the Vector2. /// @@ -78,118 +81,86 @@ namespace OpenTK.Math #region Functions - #region Add + #region public Vector2 Add(Vector2 right) /// /// Adds the given Vector2 to the current Vector2. /// /// The right operand of the addition. - /// A new Vector2 containing the result of the addition. + /// The current Vector2, modified by the operation. public Vector2 Add(Vector2 right) { - return new Vector2(X + right.X, Y + right.Y); - } - - /// - /// Adds the given Vector3 to the current Vector2. - /// - /// The right operand of the addition. - /// A new Vector3 containing the result of the addition. - public Vector3 Add(Vector3 right) - { - return new Vector3(X + right.X, Y + right.Y, right.Z); - } - - /// - /// Adds the given Vector4 to the current Vector2. - /// - /// The right operand of the addition. - /// A new Vector4 containing the result of the addition. - public Vector4 Add(Vector4 right) - { - return new Vector4(X + right.X, Y + right.Y, right.Z, right.W); + this.X = X + right.X; + this.Y = Y + right.Y; + return this; } #endregion - #region Sub + #region public Vector2 Sub(Vector2 right) /// /// Subtracts the given Vector2 from the current Vector2. /// /// The right operand of the subtraction. - /// A new Vector2 containing the result of the subtraction. + /// The current Vector2, modified by the operation. public Vector2 Sub(Vector2 right) { - return new Vector2(X - right.X, Y - right.Y); - } - - /// - /// Subtracts the given Vector3 from the current Vector2. - /// - /// The right operand of the subtraction. - /// A new Vector3 containing the result of the subtraction. - public Vector3 Sub(Vector3 right) - { - return new Vector3(X - right.X, Y - right.Y, -right.Z); - } - - /// - /// Subtracts the given Vector4 from the current Vector2. - /// - /// The right operand of the subtraction. - /// A new Vector4 containing the result of the subtraction. - public Vector4 Sub(Vector4 right) - { - return new Vector4(X - right.X, Y - right.Y, -right.Z, -right.W); + this.X = X - right.X; + this.Y = Y - right.Y; + return this; } #endregion - #region Dot + #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 dot product. + /// A float containing the result of the operation. public float Dot(Vector2 right) { return X * right.X + Y * right.Y; } - /// - /// Computes the dot product between the current Vector2 and the given Vector3. - /// - /// The right operand of the dot product. - /// A float containing the result of the dot product. - public float Dot(Vector3 right) - { - return X * right.X + Y * right.Y; - } - - /// - /// Computes the dot product between the current Vector2 and the given Vector4. - /// - /// The right operand of the dot product. - /// A float containing the result of the dot product. - public float Dot(Vector4 right) - { - return X * right.X + Y * right.Y; - } - #endregion #region public float Length /// - /// Gets the length of the vector. + /// Gets the length (magnitude) of the vector. /// + /// + /// public float Length { get { - return (float)System.Math.Sqrt(this.LengthSquared); + 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 OpenTK.Math.SqrtFast(X * X + Y * Y); } } @@ -198,8 +169,14 @@ namespace OpenTK.Math #region public float LengthSquared /// - /// Gets the square of the vector length. + /// 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 @@ -224,6 +201,22 @@ namespace OpenTK.Math #endregion + #region public Vector2 NormalizeFast() + + /// + /// Scales the Vector2 to unit length. + /// + /// The normalized version of the current vector. + public Vector2 NormalizeFast() + { + float length = this.LengthFast; + this.X = X / length; + this.Y = Y / length; + return this; + } + + #endregion + #region public Vector2 Scale(float sx, float sy) /// @@ -231,10 +224,12 @@ namespace OpenTK.Math /// /// The scale of the X component. /// The scale of the Y component. - /// A new, scaled Vector2. + /// The current Vector2, scaled. public Vector2 Scale(float sx, float sy) { - return new Vector2(X * sx, Y * sy); + this.X = X * sx; + this.Y = Y * sy; + return this; } #endregion @@ -245,32 +240,12 @@ namespace OpenTK.Math public static Vector2 operator +(Vector2 left, Vector2 right) { - return left.Add(right); - } - - public static Vector3 operator +(Vector2 left, Vector3 right) - { - return left.Add(right); - } - - public static Vector4 operator +(Vector2 left, Vector4 right) - { - return left.Add(right); + return new Vector2(left).Add(right); } public static Vector2 operator -(Vector2 left, Vector2 right) { - return left.Sub(right); - } - - public static Vector3 operator -(Vector2 left, Vector3 right) - { - return left.Sub(right); - } - - public static Vector4 operator -(Vector2 left, Vector4 right) - { - return left.Sub(right); + return new Vector2(left).Sub(right); } [CLSCompliant(false)] @@ -281,9 +256,17 @@ namespace OpenTK.Math #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 } }