#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. /// [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; } #endregion #region Add /// /// Adds the given Vector2 to the current Vector2. /// /// The right operand of the addition. /// A new Vector2 containing the result of the addition. 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); } #endregion #region Sub /// /// Subtracts the given Vector2 from the current Vector2. /// /// The right operand of the subtraction. /// A new Vector2 containing the result of the subtraction. 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); } #endregion #region Dot /// /// 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. 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 Operator overloads 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); } 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); } [CLSCompliant(false)] unsafe public static implicit operator float*(Vector2 v) { return &v.X; } #endregion /// /// Returns the length of the vector. /// public float Length { get { return (float)System.Math.Sqrt(this.LengthSquared); } } /// /// Returns the square of the vector length. /// public float LengthSquared { get { return X * X + Y * Y; } } /// /// Scales the Vector2 to unit length. /// /// The normalized version of the current vector. public Vector2 Normalize() { float length = this.Length; return new Vector2(X / length, Y / Length); } /// /// Scales the current Vector2 by the given amounts. /// /// The scale of the X component. /// The scale of the Y component. /// A new, scaled Vector2. public Vector2 Scale(float sx, float sy) { return new Vector2(X * sx, Y * sy); } } }