VInt2.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using UnityEngine;
  3. [Serializable]
  4. public struct VInt2
  5. {
  6. public int x;
  7. public int y;
  8. public static VInt2 zero = default(VInt2);
  9. private static readonly int[] Rotations = new int[]
  10. {
  11. 1,
  12. 0,
  13. 0,
  14. 1,
  15. 0,
  16. 1,
  17. -1,
  18. 0,
  19. -1,
  20. 0,
  21. 0,
  22. -1,
  23. 0,
  24. -1,
  25. 1,
  26. 0
  27. };
  28. public int sqrMagnitude
  29. {
  30. get
  31. {
  32. return this.x * this.x + this.y * this.y;
  33. }
  34. }
  35. public long sqrMagnitudeLong
  36. {
  37. get
  38. {
  39. long num = (long)this.x;
  40. long num2 = (long)this.y;
  41. return num * num + num2 * num2;
  42. }
  43. }
  44. public int magnitude
  45. {
  46. get
  47. {
  48. long num = (long)this.x;
  49. long num2 = (long)this.y;
  50. return IntMath.Sqrt(num * num + num2 * num2);
  51. }
  52. }
  53. public VInt2 normalized
  54. {
  55. get
  56. {
  57. VInt2 result = new VInt2(this.x, this.y);
  58. result.Normalize();
  59. return result;
  60. }
  61. }
  62. public VInt2(int x, int y)
  63. {
  64. this.x = x;
  65. this.y = y;
  66. }
  67. public static int Dot(VInt2 a, VInt2 b)
  68. {
  69. return a.x * b.x + a.y * b.y;
  70. }
  71. public static long DotLong(ref VInt2 a, ref VInt2 b)
  72. {
  73. return (long)a.x * (long)b.x + (long)a.y * (long)b.y;
  74. }
  75. public static long DotLong(VInt2 a, VInt2 b)
  76. {
  77. return (long)a.x * (long)b.x + (long)a.y * (long)b.y;
  78. }
  79. public static long DetLong(ref VInt2 a, ref VInt2 b)
  80. {
  81. return (long)a.x * (long)b.y - (long)a.y * (long)b.x;
  82. }
  83. public static long DetLong(VInt2 a, VInt2 b)
  84. {
  85. return (long)a.x * (long)b.y - (long)a.y * (long)b.x;
  86. }
  87. public override bool Equals(object o)
  88. {
  89. if (o == null)
  90. {
  91. return false;
  92. }
  93. VInt2 vInt = (VInt2)o;
  94. return this.x == vInt.x && this.y == vInt.y;
  95. }
  96. public override int GetHashCode()
  97. {
  98. return this.x * 49157 + this.y * 98317;
  99. }
  100. public static VInt2 Rotate(VInt2 v, int r)
  101. {
  102. r %= 4;
  103. return new VInt2(v.x * VInt2.Rotations[r * 4] + v.y * VInt2.Rotations[r * 4 + 1], v.x * VInt2.Rotations[r * 4 + 2] + v.y * VInt2.Rotations[r * 4 + 3]);
  104. }
  105. public static VInt2 Min(VInt2 a, VInt2 b)
  106. {
  107. return new VInt2(Math.Min(a.x, b.x), Math.Min(a.y, b.y));
  108. }
  109. public static VInt2 Max(VInt2 a, VInt2 b)
  110. {
  111. return new VInt2(Math.Max(a.x, b.x), Math.Max(a.y, b.y));
  112. }
  113. public static VInt2 FromInt3XZ(VInt3 o)
  114. {
  115. return new VInt2(o.x, o.z);
  116. }
  117. public static VInt3 ToInt3XZ(VInt2 o)
  118. {
  119. return new VInt3(o.x, 0, o.y);
  120. }
  121. public override string ToString()
  122. {
  123. return string.Concat(new object[]
  124. {
  125. "(",
  126. this.x,
  127. ", ",
  128. this.y,
  129. ")"
  130. });
  131. }
  132. public void Min(ref VInt2 r)
  133. {
  134. this.x = Mathf.Min(this.x, r.x);
  135. this.y = Mathf.Min(this.y, r.y);
  136. }
  137. public void Max(ref VInt2 r)
  138. {
  139. this.x = Mathf.Max(this.x, r.x);
  140. this.y = Mathf.Max(this.y, r.y);
  141. }
  142. public void Normalize()
  143. {
  144. long num = (long)(this.x * 100);
  145. long num2 = (long)(this.y * 100);
  146. long num3 = num * num + num2 * num2;
  147. if (num3 == 0L)
  148. {
  149. return;
  150. }
  151. long b = (long)IntMath.Sqrt(num3);
  152. this.x = (int)IntMath.Divide(num * 1000L, b);
  153. this.y = (int)IntMath.Divide(num2 * 1000L, b);
  154. }
  155. public static VInt2 ClampMagnitude(VInt2 v, int maxLength)
  156. {
  157. long sqrMagnitudeLong = v.sqrMagnitudeLong;
  158. long num = (long)maxLength;
  159. if (sqrMagnitudeLong > num * num)
  160. {
  161. long b = (long)IntMath.Sqrt(sqrMagnitudeLong);
  162. int num2 = (int)IntMath.Divide((long)(v.x * maxLength), b);
  163. int num3 = (int)IntMath.Divide((long)(v.x * maxLength), b);
  164. return new VInt2(num2, num3);
  165. }
  166. return v;
  167. }
  168. public static explicit operator Vector2(VInt2 ob)
  169. {
  170. return new Vector2((float)ob.x * 0.001f, (float)ob.y * 0.001f);
  171. }
  172. public static explicit operator VInt2(Vector2 ob)
  173. {
  174. return new VInt2((int)Math.Round((double)(ob.x * 1000f)), (int)Math.Round((double)(ob.y * 1000f)));
  175. }
  176. public static VInt2 operator +(VInt2 a, VInt2 b)
  177. {
  178. return new VInt2(a.x + b.x, a.y + b.y);
  179. }
  180. public static VInt2 operator -(VInt2 a, VInt2 b)
  181. {
  182. return new VInt2(a.x - b.x, a.y - b.y);
  183. }
  184. public static bool operator ==(VInt2 a, VInt2 b)
  185. {
  186. return a.x == b.x && a.y == b.y;
  187. }
  188. public static bool operator !=(VInt2 a, VInt2 b)
  189. {
  190. return a.x != b.x || a.y != b.y;
  191. }
  192. public static VInt2 operator -(VInt2 lhs)
  193. {
  194. lhs.x = -lhs.x;
  195. lhs.y = -lhs.y;
  196. return lhs;
  197. }
  198. public static VInt2 operator *(VInt2 lhs, int rhs)
  199. {
  200. lhs.x *= rhs;
  201. lhs.y *= rhs;
  202. return lhs;
  203. }
  204. }