#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 Urho
{
/// Represents a 2D vector using two single-precision inting-point numbers.
///
/// The IntVector2 structure is suitable for interoperation with unmanaged code requiring two consecutive ints.
///
[StructLayout(LayoutKind.Sequential)]
public struct IntVector2 : IEquatable
{
#region Fields
///
/// The X component of the IntVector2.
///
public int X;
///
/// The Y component of the IntVector2.
///
public int Y;
#endregion
#region Constructors
///
/// Constructs a new IntVector2.
///
/// The x coordinate of the net IntVector2.
/// The y coordinate of the net IntVector2.
public IntVector2(int x, int y)
{
X = x;
Y = y;
}
///
/// Constructs a new IntVector2 from the given IntVector2.
///
/// The IntVector2 to copy components from.
[Obsolete]
public IntVector2(IntVector2 v)
{
X = v.X;
Y = v.Y;
}
///
/// Constructs a new IntVector2 from the given Vector2.
///
/// The IntVector2 to copy components from.
[Obsolete]
public IntVector2(Vector2 v)
{
X = (int) v.X;
Y = (int) v.Y;
}
#endregion
#region Public Members
#region Instance
#region public void Add()
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
[Obsolete("Use static Add() method instead.")]
public void Add(IntVector2 right)
{
this.X += right.X;
this.Y += right.Y;
}
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
[Obsolete("Use static Add() method instead.")]
public void Add(ref IntVector2 right)
{
this.X += right.X;
this.Y += right.Y;
}
#endregion public void Add()
#region public void Sub()
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
[Obsolete("Use static Subtract() method instead.")]
public void Sub(IntVector2 right)
{
this.X -= right.X;
this.Y -= right.Y;
}
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
[Obsolete("Use static Subtract() method instead.")]
public void Sub(ref IntVector2 right)
{
this.X -= right.X;
this.Y -= right.Y;
}
#endregion public void Sub()
#region public void Mult()
/// Multiply this instance by a scalar.
/// Scalar operand.
[Obsolete("Use static Multiply() method instead.")]
public void Mult(int f)
{
this.X *= f;
this.Y *= f;
}
#endregion public void Mult()
#region public void Div()
/// Divide this instance by a scalar.
/// Scalar operand.
[Obsolete("Use static Divide() method instead.")]
public void Div(int f)
{
this.X = X / f;
this.Y = Y / f;
}
#endregion public void Div()
#region public int Length
///
/// Gets the length (magnitude) of the vector.
///
///
///
public int Length
{
get
{
return (int)System.Math.Sqrt(X * X + Y * Y);
}
}
#endregion
#region public int 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 int LengthFast
{
get
{
return (int) (1.0f / MathHelper.InverseSqrtFast(X * X + Y * Y));
}
}
#endregion
#region public int 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 int LengthSquared
{
get
{
return X * X + Y * Y;
}
}
#endregion
#region public IntVector2 PerpendicularRight
///
/// Gets the perpendicular vector on the right side of this vector.
///
public IntVector2 PerpendicularRight
{
get
{
return new IntVector2(Y, -X);
}
}
#endregion
#region public IntVector2 PerpendicularLeft
///
/// Gets the perpendicular vector on the left side of this vector.
///
public IntVector2 PerpendicularLeft
{
get
{
return new IntVector2(-Y, X);
}
}
#endregion
#region public void Normalize()
///
/// Scales the IntVector2 to unit length.
///
public void Normalize()
{
X = X / this.Length;
Y *= Y / this.Length;
}
#endregion
#region public void NormalizeFast()
///
/// Scales the IntVector2 to approximately unit length.
///
public void NormalizeFast()
{
int scale = (int) MathHelper.InverseSqrtFast(X * X + Y * Y);
X *= scale;
Y *= scale;
}
#endregion
#region public void Scale()
///
/// Scales the current IntVector2 by the given amounts.
///
/// The scale of the X component.
/// The scale of the Y component.
[Obsolete("Use static Multiply() method instead.")]
public void Scale(int sx, int sy)
{
this.X = X * sx;
this.Y = Y * sy;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
[Obsolete("Use static Multiply() method instead.")]
public void Scale(IntVector2 scale)
{
this.X *= scale.X;
this.Y *= scale.Y;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
[CLSCompliant(false)]
[Obsolete("Use static Multiply() method instead.")]
public void Scale(ref IntVector2 scale)
{
this.X *= scale.X;
this.Y *= scale.Y;
}
#endregion public void Scale()
#endregion
#region Static
#region Fields
///
/// Defines a unit-length IntVector2 that points towards the X-axis.
///
public static readonly IntVector2 UnitX = new IntVector2(1, 0);
///
/// Defines a unit-length IntVector2 that points towards the Y-axis.
///
public static readonly IntVector2 UnitY = new IntVector2(0, 1);
///
/// Defines a zero-length IntVector2.
///
public static readonly IntVector2 Zero = new IntVector2(0, 0);
///
/// Defines an instance with all components set to 1.
///
public static readonly IntVector2 One = new IntVector2(1, 1);
///
/// Defines the size of the IntVector2 struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new IntVector2());
#endregion
#region Add
///
/// Adds two vectors.
///
/// Left operand.
/// Right operand.
/// Result of operation.
public static IntVector2 Add(IntVector2 a, IntVector2 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 IntVector2 a, ref IntVector2 b, out IntVector2 result)
{
result = new IntVector2(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 IntVector2 Subtract(IntVector2 a, IntVector2 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 IntVector2 a, ref IntVector2 b, out IntVector2 result)
{
result = new IntVector2(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 IntVector2 Multiply(IntVector2 vector, int 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 IntVector2 vector, int scale, out IntVector2 result)
{
result = new IntVector2(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 IntVector2 Multiply(IntVector2 vector, IntVector2 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 IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
{
result = new IntVector2(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 IntVector2 Divide(IntVector2 vector, int 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 IntVector2 vector, int scale, out IntVector2 result)
{
Multiply(ref vector, 1 / scale, out result);
}
///
/// Divides a vector by the components of a vector (scale).
///
/// Left operand.
/// Right operand.
/// Result of the operation.
public static IntVector2 Divide(IntVector2 vector, IntVector2 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 IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
{
result = new IntVector2(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 IntVector2 ComponentMin(IntVector2 a, IntVector2 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 IntVector2 a, ref IntVector2 b, out IntVector2 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 IntVector2 ComponentMax(IntVector2 a, IntVector2 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 IntVector2 a, ref IntVector2 b, out IntVector2 result)
{
result.X = a.X > b.X ? a.X : b.X;
result.Y = a.Y > b.Y ? a.Y : b.Y;
}
#endregion
#region Min
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static IntVector2 Min(IntVector2 left, IntVector2 right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
#endregion
#region Max
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static IntVector2 Max(IntVector2 left, IntVector2 right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static IntVector2 Clamp(IntVector2 vec, IntVector2 min, IntVector2 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 IntVector2 vec, ref IntVector2 min, ref IntVector2 max, out IntVector2 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 Normalize
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static IntVector2 Normalize(IntVector2 vec)
{
vec.X = vec.X / vec.Length;
vec.Y = vec.Y / vec.Length;
return vec;
}
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static void Normalize(ref IntVector2 vec, out IntVector2 result)
{
result.X = vec.X / vec.Length;
result.Y = vec.Y / vec.Length;
}
#endregion
#region NormalizeFast
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static IntVector2 NormalizeFast(IntVector2 vec)
{
int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
vec.X *= scale;
vec.Y *= scale;
return vec;
}
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static void NormalizeFast(ref IntVector2 vec, out IntVector2 result)
{
int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
result.X = vec.X * scale;
result.Y = vec.Y * scale;
}
#endregion
#region Dot
///
/// Calculate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static int Dot(IntVector2 left, IntVector2 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 IntVector2 left, ref IntVector2 right, out int result)
{
result = left.X * right.X + left.Y * right.Y;
}
#endregion
#region Lerp
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static IntVector2 Lerp(IntVector2 a, IntVector2 b, int blend)
{
a.X = blend * (b.X - a.X) + a.X;
a.Y = blend * (b.Y - a.Y) + a.Y;
return a;
}
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static void Lerp(ref IntVector2 a, ref IntVector2 b, int blend, out IntVector2 result)
{
result.X = blend * (b.X - a.X) + a.X;
result.Y = blend * (b.Y - a.Y) + a.Y;
}
#endregion
#region Barycentric
///
/// Interpolate 3 Vectors using Barycentric coordinates
///
/// First input Vector
/// Second input Vector
/// Third input Vector
/// First Barycentric Coordinate
/// Second Barycentric Coordinate
/// 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
public static IntVector2 BaryCentric(IntVector2 a, IntVector2 b, IntVector2 c, int u, int v)
{
return a + u * (b - a) + v * (c - a);
}
/// Interpolate 3 Vectors using Barycentric coordinates
/// First input Vector.
/// Second input Vector.
/// Third input Vector.
/// First Barycentric Coordinate.
/// Second Barycentric Coordinate.
/// Output Vector. 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
public static void BaryCentric(ref IntVector2 a, ref IntVector2 b, ref IntVector2 c, int u, int v, out IntVector2 result)
{
result = a; // copy
IntVector2 temp = b; // copy
Subtract(ref temp, ref a, out temp);
Multiply(ref temp, u, out temp);
Add(ref result, ref temp, out result);
temp = c; // copy
Subtract(ref temp, ref a, out temp);
Multiply(ref temp, v, out temp);
Add(ref result, ref temp, out result);
}
#endregion
#endregion
#region Operators
///
/// Adds the specified instances.
///
/// Left operand.
/// Right operand.
/// Result of addition.
public static IntVector2 operator +(IntVector2 left, IntVector2 right)
{
left.X += right.X;
left.Y += right.Y;
return left;
}
///
/// Subtracts the specified instances.
///
/// Left operand.
/// Right operand.
/// Result of subtraction.
public static IntVector2 operator -(IntVector2 left, IntVector2 right)
{
left.X -= right.X;
left.Y -= right.Y;
return left;
}
///
/// Negates the specified instance.
///
/// Operand.
/// Result of negation.
public static IntVector2 operator -(IntVector2 vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
return vec;
}
///
/// Multiplies the specified instance by a scalar.
///
/// Left operand.
/// Right operand.
/// Result of multiplication.
public static IntVector2 operator *(IntVector2 vec, int 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 IntVector2 operator *(int scale, IntVector2 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 IntVector2 operator /(IntVector2 vec, int scale)
{
vec.X = vec.X / scale;
vec.Y = 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 ==(IntVector2 left, IntVector2 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 !=(IntVector2 left, IntVector2 right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current IntVector2.
///
///
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 IntVector2))
return false;
return this.Equals((IntVector2)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(IntVector2 other)
{
return
X == other.X &&
Y == other.Y;
}
#endregion
}
}