#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 { /// Represents a 2D vector using two 32-bit unsigned integer values. /// /// The Vector2ui structure is suitable for interoperation with unmanaged code requiring two consecutive unsigned integers. /// [Serializable] public struct Vector2ui : IEquatable { #region Fields /// /// The X component of the Vector2ui. /// public uint X; /// /// The Y component of the Vector2ui. /// public uint Y; #endregion #region Constructors /// /// Constructs a new Vector2ui. /// /// The x coordinate of the net Vector2ui. /// The y coordinate of the net Vector2ui. public Vector2ui(uint x, uint y) { X = x; Y = y; } #endregion #region Public Members #region Static #region Fields /// /// Defines a unit-length Vector2ui that points towards the X-axis. /// public static readonly Vector2ui UnitX = new Vector2ui(1, 0); /// /// Defines a unit-length Vector2ui that points towards the Y-axis. /// public static readonly Vector2ui UnitY = new Vector2ui(0, 1); /// /// Defines a zero-length Vector2ui. /// public static readonly Vector2ui Zero = new Vector2ui(0, 0); /// /// Defines an instance with all components set to 1. /// public static readonly Vector2ui One = new Vector2ui(1, 1); /// /// Defines the size of the Vector2ui struct in bytes. /// public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2ui()); #endregion #region Add /// /// Adds two vectors. /// /// Left operand. /// Right operand. /// Result of operation. public static Vector2ui Add(Vector2ui a, Vector2ui b) { Add(ref a, ref b, out a); return a; } /// /// Adds two vectors. /// /// Left operand. /// Right operand. /// Result of operation. public static void Add(ref Vector2ui a, ref Vector2ui b, out Vector2ui result) { result = new Vector2ui(a.X + b.X, a.Y + b.Y); } #endregion #region Subtract /// /// Subtract one Vector from another /// /// First operand /// Second operand /// Result of subtraction public static Vector2ui Subtract(Vector2ui a, Vector2ui b) { Subtract(ref a, ref b, out a); return a; } /// /// Subtract one Vector from another /// /// First operand /// Second operand /// Result of subtraction public static void Subtract(ref Vector2ui a, ref Vector2ui b, out Vector2ui result) { result = new Vector2ui(a.X - b.X, a.Y - b.Y); } #endregion #region Multiply /// /// Multiplies a vector by a scalar. /// /// Left operand. /// Right operand. /// Result of the operation. public static Vector2ui Multiply(Vector2ui vector, uint scale) { Multiply(ref vector, scale, out vector); return vector; } /// /// Multiplies a vector by a scalar. /// /// Left operand. /// Right operand. /// Result of the operation. public static void Multiply(ref Vector2ui vector, uint scale, out Vector2ui result) { result = new Vector2ui(vector.X * scale, vector.Y * scale); } /// /// Multiplies a vector by the components a vector (scale). /// /// Left operand. /// Right operand. /// Result of the operation. public static Vector2ui Multiply(Vector2ui vector, Vector2ui scale) { Multiply(ref vector, ref scale, out vector); return vector; } /// /// Multiplies a vector by the components of a vector (scale). /// /// Left operand. /// Right operand. /// Result of the operation. public static void Multiply(ref Vector2ui vector, ref Vector2ui scale, out Vector2ui result) { result = new Vector2ui(vector.X * scale.X, vector.Y * scale.Y); } #endregion #region Divide /// /// Divides a vector by a scalar. /// /// Left operand. /// Right operand. /// Result of the operation. public static Vector2ui Divide(Vector2ui vector, uint scale) { Divide(ref vector, scale, out vector); return vector; } /// /// Divides a vector by a scalar. /// /// Left operand. /// Right operand. /// Result of the operation. public static void Divide(ref Vector2ui vector, uint scale, out Vector2ui result) { result = new Vector2ui(vector.X / scale, vector.Y / scale); } /// /// Divides a vector by the components of a vector (scale). /// /// Left operand. /// Right operand. /// Result of the operation. public static Vector2ui Divide(Vector2ui vector, Vector2ui scale) { Divide(ref vector, ref scale, out vector); return vector; } /// /// Divide a vector by the components of a vector (scale). /// /// Left operand. /// Right operand. /// Result of the operation. public static void Divide(ref Vector2ui vector, ref Vector2ui scale, out Vector2ui result) { result = new Vector2ui(vector.X / scale.X, vector.Y / scale.Y); } #endregion #region ComponentMin /// /// Calculate the component-wise minimum of two vectors /// /// First operand /// Second operand /// The component-wise minimum public static Vector2ui ComponentMin(Vector2ui a, Vector2ui 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 Vector2ui a, ref Vector2ui b, out Vector2ui 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 Vector2ui ComponentMax(Vector2ui a, Vector2ui 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 Vector2ui a, ref Vector2ui b, out Vector2ui result) { result.X = a.X > b.X ? a.X : b.X; result.Y = a.Y > b.Y ? a.Y : b.Y; } #endregion #region Clamp /// /// Clamp a vector to the given minimum and maximum vectors /// /// Input vector /// Minimum vector /// Maximum vector /// The clamped vector public static Vector2ui Clamp(Vector2ui vec, Vector2ui min, Vector2ui 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 Vector2ui vec, ref Vector2ui min, ref Vector2ui max, out Vector2ui 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 Dot /// /// Calculate the dot (scalar) product of two vectors /// /// First operand /// Second operand /// The dot product of the two inputs public static uint Dot(Vector2ui left, Vector2ui 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 Vector2ui left, ref Vector2ui right, out uint result) { result = left.X * right.X + left.Y * right.Y; } #endregion #endregion #region Operators /// /// Adds the specified instances. /// /// Left operand. /// Right operand. /// Result of addition. public static Vector2ui operator +(Vector2ui left, Vector2ui right) { left.X += right.X; left.Y += right.Y; return left; } /// /// Subtracts the specified instances. /// /// Left operand. /// Right operand. /// Result of subtraction. public static Vector2ui operator -(Vector2ui left, Vector2ui right) { left.X -= right.X; left.Y -= right.Y; return left; } /// /// Multiplies the specified instance by a scalar. /// /// Left operand. /// Right operand. /// Result of multiplication. public static Vector2ui operator *(Vector2ui vec, uint scale) { vec.X *= scale; vec.Y *= scale; return vec; } /// /// Multiplies the specified instance by a scalar. /// /// Left operand. /// Right operand. /// Result of multiplication. public static Vector2ui operator *(uint scale, Vector2ui vec) { vec.X *= scale; vec.Y *= scale; return vec; } /// /// Divides the specified instance by a scalar. /// /// Left operand /// Right operand /// Result of the division. public static Vector2ui operator /(Vector2ui vec, uint scale) { vec.X /= scale; vec.Y /= scale; return vec; } /// /// Compares the specified instances for equality. /// /// Left operand. /// Right operand. /// True if both instances are equal; false otherwise. public static bool operator ==(Vector2ui left, Vector2ui right) { return left.Equals(right); } /// /// Compares the specified instances for inequality. /// /// Left operand. /// Right operand. /// True if both instances are not equal; false otherwise. public static bool operator !=(Vector2ui left, Vector2ui right) { return !left.Equals(right); } #endregion #region Overrides #region public override string ToString() /// /// Returns a System.String that represents the current Vector2ui. /// /// 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 Vector2ui)) return false; return this.Equals((Vector2ui)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(Vector2ui other) { return X == other.X && Y == other.Y; } #endregion } }