Vector2I.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct Vector2I
  7. {
  8. public int x;
  9. public int y;
  10. public int this[int index]
  11. {
  12. get
  13. {
  14. switch (index)
  15. {
  16. case 0:
  17. return x;
  18. case 1:
  19. return y;
  20. default:
  21. throw new IndexOutOfRangeException("Invalid Vector2I index.");
  22. }
  23. }
  24. set
  25. {
  26. switch (index)
  27. {
  28. case 0:
  29. x = value;
  30. break;
  31. case 1:
  32. y = value;
  33. break;
  34. default:
  35. throw new IndexOutOfRangeException("Invalid Vector2I index.");
  36. }
  37. }
  38. }
  39. public Vector2I(int x, int y)
  40. {
  41. this.x = x;
  42. this.y = y;
  43. }
  44. public static Vector2I operator +(Vector2I a, Vector2I b)
  45. {
  46. return new Vector2I(a.x + b.x, a.y + b.y);
  47. }
  48. public static Vector2I operator -(Vector2I a, Vector2I b)
  49. {
  50. return new Vector2I(a.x - b.x, a.y - b.y);
  51. }
  52. public static Vector2I operator -(Vector2I v)
  53. {
  54. return new Vector2I(-v.x, -v.y);
  55. }
  56. public static Vector2I operator *(Vector2I v, int d)
  57. {
  58. return new Vector2I(v.x * d, v.y * d);
  59. }
  60. public static Vector2I operator *(int d, Vector2I v)
  61. {
  62. return new Vector2I(v.x * d, v.y * d);
  63. }
  64. public static Vector2I operator /(Vector2I v, int d)
  65. {
  66. return new Vector2I(v.x / d, v.y / d);
  67. }
  68. public static bool operator ==(Vector2I lhs, Vector2I rhs)
  69. {
  70. return lhs.x == rhs.x && lhs.y == rhs.y;
  71. }
  72. public static bool operator !=(Vector2I lhs, Vector2I rhs)
  73. {
  74. return !(lhs == rhs);
  75. }
  76. public override int GetHashCode()
  77. {
  78. return x.GetHashCode() ^ y.GetHashCode() << 2;
  79. }
  80. public override bool Equals(object other)
  81. {
  82. if (!(other is Vector2I))
  83. return false;
  84. Vector2I vec = (Vector2I)other;
  85. if (x.Equals(vec.x) && y.Equals(vec.y))
  86. return true;
  87. return false;
  88. }
  89. public override string ToString()
  90. {
  91. return "(" + x + ", " + y + ")";
  92. }
  93. }
  94. }