//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Runtime.InteropServices;
namespace BansheeEngine
{
/** @addtogroup Math
* @{
*/
///
/// A two dimensional vector with integer coordinates.
///
[StructLayout(LayoutKind.Sequential)]
public struct Vector2I // Note: Must match C++ class Vector2I
{
public int x;
public int y;
///
/// Accesses a specific component of the vector.
///
/// Index of the component.
/// Value of the specific component.
public int this[int index]
{
get
{
switch (index)
{
case 0:
return x;
case 1:
return y;
default:
throw new IndexOutOfRangeException("Invalid Vector2I index.");
}
}
set
{
switch (index)
{
case 0:
x = value;
break;
case 1:
y = value;
break;
default:
throw new IndexOutOfRangeException("Invalid Vector2I index.");
}
}
}
///
/// Creates a new two dimensional vector.
///
/// X coordinate.
/// Y coordinate.
public Vector2I(int x, int y)
{
this.x = x;
this.y = y;
}
///
/// Returns length of the vector.
///
public float Length
{
get
{
return (float)MathEx.Sqrt(x * x + y * y);
}
}
///
/// Returns the squared length of the vector.
///
public float SqrdLength
{
get
{
return (x * x + y * y);
}
}
///
/// Returns the manhattan distance between two points.
///
/// First two dimensional point.
/// Second two dimensional point.
/// Manhattan distance between the two points.
public static int Distance(Vector2I a, Vector2I b)
{
return Math.Abs(b.x - a.x) + Math.Abs(b.y - a.y);
}
public static Vector2I operator +(Vector2I a, Vector2I b)
{
return new Vector2I(a.x + b.x, a.y + b.y);
}
public static Vector2I operator -(Vector2I a, Vector2I b)
{
return new Vector2I(a.x - b.x, a.y - b.y);
}
public static Vector2I operator -(Vector2I v)
{
return new Vector2I(-v.x, -v.y);
}
public static Vector2I operator *(Vector2I v, int d)
{
return new Vector2I(v.x * d, v.y * d);
}
public static Vector2I operator *(int d, Vector2I v)
{
return new Vector2I(v.x * d, v.y * d);
}
public static Vector2I operator /(Vector2I v, int d)
{
return new Vector2I(v.x / d, v.y / d);
}
public static bool operator ==(Vector2I lhs, Vector2I rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
public static bool operator !=(Vector2I lhs, Vector2I rhs)
{
return !(lhs == rhs);
}
///
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode() << 2;
}
///
public override bool Equals(object other)
{
if (!(other is Vector2I))
return false;
Vector2I vec = (Vector2I)other;
if (x.Equals(vec.x) && y.Equals(vec.y))
return true;
return false;
}
///
public override string ToString()
{
return "(" + x + ", " + y + ")";
}
}
/** @} */
}