Vector2.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct Vector2
  7. {
  8. public float x;
  9. public float y;
  10. public static Vector2 zero
  11. {
  12. get
  13. {
  14. return new Vector2(0.0f, 0.0f);
  15. }
  16. }
  17. public static Vector2 one
  18. {
  19. get
  20. {
  21. return new Vector2(1.0f, 1.0f);
  22. }
  23. }
  24. public float this[int index]
  25. {
  26. get
  27. {
  28. switch (index)
  29. {
  30. case 0:
  31. return x;
  32. case 1:
  33. return y;
  34. default:
  35. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  36. }
  37. }
  38. set
  39. {
  40. switch (index)
  41. {
  42. case 0:
  43. x = value;
  44. break;
  45. case 1:
  46. y = value;
  47. break;
  48. default:
  49. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  50. }
  51. }
  52. }
  53. public Vector2 normalized
  54. {
  55. get
  56. {
  57. return Normalize(this);
  58. }
  59. }
  60. public float magnitude
  61. {
  62. get
  63. {
  64. return MathEx.Sqrt(x * x + y * y);
  65. }
  66. }
  67. public float sqrdMagnitude
  68. {
  69. get
  70. {
  71. return (x * x + y * y);
  72. }
  73. }
  74. public Vector2(float x, float y)
  75. {
  76. this.x = x;
  77. this.y = y;
  78. }
  79. public static Vector2 operator+ (Vector2 a, Vector2 b)
  80. {
  81. return new Vector2(a.x + b.x, a.y + b.y);
  82. }
  83. public static Vector2 operator- (Vector2 a, Vector2 b)
  84. {
  85. return new Vector2(a.x - b.x, a.y - b.y);
  86. }
  87. public static Vector2 operator- (Vector2 v)
  88. {
  89. return new Vector2(-v.x, -v.y);
  90. }
  91. public static Vector2 operator* (Vector2 v, float d)
  92. {
  93. return new Vector2(v.x * d, v.y * d);
  94. }
  95. public static Vector2 operator* (float d, Vector2 v)
  96. {
  97. return new Vector2(v.x * d, v.y * d);
  98. }
  99. public static Vector2 operator/ (Vector2 v, float d)
  100. {
  101. return new Vector2(v.x / d, v.y / d);
  102. }
  103. public static Vector2 Scale(Vector2 a, Vector2 b)
  104. {
  105. return new Vector2(a.x * b.x, a.y * b.y);
  106. }
  107. public static Vector2 Normalize(Vector2 value)
  108. {
  109. float num = Magnitude(value);
  110. if (num > 9.999999E-06f)
  111. return value / num;
  112. return zero;
  113. }
  114. public static float Dot(Vector2 lhs, Vector2 rhs)
  115. {
  116. return lhs.x * rhs.x + lhs.y * rhs.y;
  117. }
  118. public static float Distance(Vector2 a, Vector2 b)
  119. {
  120. Vector2 vector2 = new Vector2(a.x - b.x, a.y - b.y);
  121. return MathEx.Sqrt(vector2.x * vector2.x + vector2.y * vector2.y);
  122. }
  123. public static float Magnitude(Vector2 v)
  124. {
  125. return MathEx.Sqrt(v.x * v.x + v.y * v.y);
  126. }
  127. public static float SqrMagnitude(Vector2 v)
  128. {
  129. return (v.x * v.x + v.y * v.y);
  130. }
  131. public void Scale(Vector2 scale)
  132. {
  133. x *= scale.x;
  134. y *= scale.y;
  135. }
  136. public void Normalize()
  137. {
  138. float num = Magnitude(this);
  139. if (num > 9.999999E-06f)
  140. this /= num;
  141. else
  142. this = zero;
  143. }
  144. }
  145. }