#if REAL_T_IS_DOUBLE using real_t = System.Double; #else using real_t = System.Single; #endif using System; using System.Runtime.InteropServices; namespace Godot { /// /// 2-element structure that can be used to represent 2D grid coordinates or pairs of integers. /// [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Vector2i : IEquatable { /// /// Enumerated index values for the axes. /// Returned by and . /// public enum Axis { X = 0, Y } /// /// The vector's X component. Also accessible by using the index position `[0]`. /// public int x; /// /// The vector's Y component. Also accessible by using the index position `[1]`. /// public int y; /// /// Access vector components using their index. /// /// `[0]` is equivalent to `.x`, `[1]` is equivalent to `.y`. public int this[int index] { get { switch (index) { case 0: return x; case 1: return y; default: throw new IndexOutOfRangeException(); } } set { switch (index) { case 0: x = value; return; case 1: y = value; return; default: throw new IndexOutOfRangeException(); } } } /// /// Returns a new vector with all components in absolute values (i.e. positive). /// /// A vector with called on each component. public Vector2i Abs() { return new Vector2i(Mathf.Abs(x), Mathf.Abs(y)); } /// /// Returns this vector's angle with respect to the X axis, or (1, 0) vector, in radians. /// /// Equivalent to the result of when /// called with the vector's `y` and `x` as parameters: `Mathf.Atan2(v.y, v.x)`. /// /// The angle of this vector, in radians. public real_t Angle() { return Mathf.Atan2(y, x); } /// /// Returns the angle to the given vector, in radians. /// /// The other vector to compare this vector to. /// The angle between the two vectors, in radians. public real_t AngleTo(Vector2i to) { return Mathf.Atan2(Cross(to), Dot(to)); } /// /// Returns the angle between the line connecting the two points and the X axis, in radians. /// /// The other vector to compare this vector to. /// The angle between the two vectors, in radians. public real_t AngleToPoint(Vector2i to) { return Mathf.Atan2(y - to.y, x - to.x); } /// /// Returns the aspect ratio of this vector, the ratio of `x` to `y`. /// /// The `x` component divided by the `y` component. public real_t Aspect() { return x / (real_t)y; } /// /// Returns a new vector with all components clamped between the /// components of `min` and `max` using /// . /// /// The vector with minimum allowed values. /// The vector with maximum allowed values. /// The vector with all components clamped. public Vector2i Clamp(Vector2i min, Vector2i max) { return new Vector2i ( Mathf.Clamp(x, min.x, max.x), Mathf.Clamp(y, min.y, max.y) ); } /// /// Returns the cross product of this vector and `b`. /// /// The other vector. /// The cross product vector. public int Cross(Vector2i b) { return x * b.y - y * b.x; } /// /// Returns the squared distance between this vector and `b`. /// This method runs faster than , so prefer it if /// you need to compare vectors or need the squared distance for some formula. /// /// The other vector to use. /// The squared distance between the two vectors. public int DistanceSquaredTo(Vector2i b) { return (b - this).LengthSquared(); } /// /// Returns the distance between this vector and `b`. /// /// The other vector to use. /// The distance between the two vectors. public real_t DistanceTo(Vector2i b) { return (b - this).Length(); } /// /// Returns the dot product of this vector and `b`. /// /// The other vector to use. /// The dot product of the two vectors. public int Dot(Vector2i b) { return x * b.x + y * b.y; } /// /// Returns the length (magnitude) of this vector. /// /// The length of this vector. public real_t Length() { int x2 = x * x; int y2 = y * y; return Mathf.Sqrt(x2 + y2); } /// /// Returns the squared length (squared magnitude) of this vector. /// This method runs faster than , so prefer it if /// you need to compare vectors or need the squared length for some formula. /// /// The squared length of this vector. public int LengthSquared() { int x2 = x * x; int y2 = y * y; return x2 + y2; } /// /// Returns the axis of the vector's largest value. See . /// If both components are equal, this method returns . /// /// The index of the largest axis. public Axis MaxAxis() { return x < y ? Axis.Y : Axis.X; } /// /// Returns the axis of the vector's smallest value. See . /// If both components are equal, this method returns . /// /// The index of the smallest axis. public Axis MinAxis() { return x < y ? Axis.X : Axis.Y; } /// /// Returns a vector composed of the of this vector's components and `mod`. /// /// A value representing the divisor of the operation. /// A vector with each component by `mod`. public Vector2i PosMod(int mod) { Vector2i v = this; v.x = Mathf.PosMod(v.x, mod); v.y = Mathf.PosMod(v.y, mod); return v; } /// /// Returns a vector composed of the of this vector's components and `modv`'s components. /// /// A vector representing the divisors of the operation. /// A vector with each component by `modv`'s components. public Vector2i PosMod(Vector2i modv) { Vector2i v = this; v.x = Mathf.PosMod(v.x, modv.x); v.y = Mathf.PosMod(v.y, modv.y); return v; } /// /// Returns a vector with each component set to one or negative one, depending /// on the signs of this vector's components, or zero if the component is zero, /// by calling on each component. /// /// A vector with all components as either `1`, `-1`, or `0`. public Vector2i Sign() { Vector2i v = this; v.x = Mathf.Sign(v.x); v.y = Mathf.Sign(v.y); return v; } /// /// Returns a perpendicular vector rotated 90 degrees counter-clockwise /// compared to the original, with the same length. /// /// The perpendicular vector. public Vector2i Orthogonal() { return new Vector2i(y, -x); } // Constants private static readonly Vector2i _zero = new Vector2i(0, 0); private static readonly Vector2i _one = new Vector2i(1, 1); private static readonly Vector2i _up = new Vector2i(0, -1); private static readonly Vector2i _down = new Vector2i(0, 1); private static readonly Vector2i _right = new Vector2i(1, 0); private static readonly Vector2i _left = new Vector2i(-1, 0); /// /// Zero vector, a vector with all components set to `0`. /// /// Equivalent to `new Vector2i(0, 0)` public static Vector2i Zero { get { return _zero; } } /// /// One vector, a vector with all components set to `1`. /// /// Equivalent to `new Vector2i(1, 1)` public static Vector2i One { get { return _one; } } /// /// Up unit vector. Y is down in 2D, so this vector points -Y. /// /// Equivalent to `new Vector2i(0, -1)` public static Vector2i Up { get { return _up; } } /// /// Down unit vector. Y is down in 2D, so this vector points +Y. /// /// Equivalent to `new Vector2i(0, 1)` public static Vector2i Down { get { return _down; } } /// /// Right unit vector. Represents the direction of right. /// /// Equivalent to `new Vector2i(1, 0)` public static Vector2i Right { get { return _right; } } /// /// Left unit vector. Represents the direction of left. /// /// Equivalent to `new Vector2i(-1, 0)` public static Vector2i Left { get { return _left; } } /// /// Constructs a new with the given components. /// /// The vector's X component. /// The vector's Y component. public Vector2i(int x, int y) { this.x = x; this.y = y; } /// /// Constructs a new from an existing . /// /// The existing . public Vector2i(Vector2i vi) { this.x = vi.x; this.y = vi.y; } /// /// Constructs a new from an existing /// by rounding the components via . /// /// The to convert. public Vector2i(Vector2 v) { this.x = Mathf.RoundToInt(v.x); this.y = Mathf.RoundToInt(v.y); } public static Vector2i operator +(Vector2i left, Vector2i right) { left.x += right.x; left.y += right.y; return left; } public static Vector2i operator -(Vector2i left, Vector2i right) { left.x -= right.x; left.y -= right.y; return left; } public static Vector2i operator -(Vector2i vec) { vec.x = -vec.x; vec.y = -vec.y; return vec; } public static Vector2i operator *(Vector2i vec, int scale) { vec.x *= scale; vec.y *= scale; return vec; } public static Vector2i operator *(int scale, Vector2i vec) { vec.x *= scale; vec.y *= scale; return vec; } public static Vector2i operator *(Vector2i left, Vector2i right) { left.x *= right.x; left.y *= right.y; return left; } public static Vector2i operator /(Vector2i vec, int divisor) { vec.x /= divisor; vec.y /= divisor; return vec; } public static Vector2i operator /(Vector2i vec, Vector2i divisorv) { vec.x /= divisorv.x; vec.y /= divisorv.y; return vec; } public static Vector2i operator %(Vector2i vec, int divisor) { vec.x %= divisor; vec.y %= divisor; return vec; } public static Vector2i operator %(Vector2i vec, Vector2i divisorv) { vec.x %= divisorv.x; vec.y %= divisorv.y; return vec; } public static Vector2i operator &(Vector2i vec, int and) { vec.x &= and; vec.y &= and; return vec; } public static Vector2i operator &(Vector2i vec, Vector2i andv) { vec.x &= andv.x; vec.y &= andv.y; return vec; } public static bool operator ==(Vector2i left, Vector2i right) { return left.Equals(right); } public static bool operator !=(Vector2i left, Vector2i right) { return !left.Equals(right); } public static bool operator <(Vector2i left, Vector2i right) { if (left.x.Equals(right.x)) { return left.y < right.y; } return left.x < right.x; } public static bool operator >(Vector2i left, Vector2i right) { if (left.x.Equals(right.x)) { return left.y > right.y; } return left.x > right.x; } public static bool operator <=(Vector2i left, Vector2i right) { if (left.x.Equals(right.x)) { return left.y <= right.y; } return left.x <= right.x; } public static bool operator >=(Vector2i left, Vector2i right) { if (left.x.Equals(right.x)) { return left.y >= right.y; } return left.x >= right.x; } public static implicit operator Vector2(Vector2i value) { return new Vector2(value.x, value.y); } public static explicit operator Vector2i(Vector2 value) { return new Vector2i(value); } public override bool Equals(object obj) { if (obj is Vector2i) { return Equals((Vector2i)obj); } return false; } public bool Equals(Vector2i other) { return x == other.x && y == other.y; } public override int GetHashCode() { return y.GetHashCode() ^ x.GetHashCode(); } public override string ToString() { return String.Format("({0}, {1})", new object[] { this.x.ToString(), this.y.ToString() }); } public string ToString(string format) { return String.Format("({0}, {1})", new object[] { this.x.ToString(format), this.y.ToString(format) }); } } }