Vector2.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using System;
  2. using System.Globalization;
  3. namespace PF
  4. {
  5. [Serializable]
  6. public struct Vector2: IEquatable<Vector2>
  7. {
  8. public static readonly Vector2 zero = new Vector2();
  9. public static readonly Vector2 one = new Vector2(1f, 1f);
  10. public float x;
  11. public float y;
  12. #if !SERVER
  13. public static implicit operator UnityEngine.Vector2(Vector2 v2)
  14. {
  15. return new UnityEngine.Vector3(v2.x, v2.y);
  16. }
  17. public static implicit operator Vector2(UnityEngine.Vector2 v2)
  18. {
  19. return new Vector2(v2.x, v2.y);
  20. }
  21. #endif
  22. public static implicit operator Vector2(Vector3 v)
  23. {
  24. return new Vector2(v.x, v.y);
  25. }
  26. public static implicit operator Vector3(Vector2 v)
  27. {
  28. return new Vector3(v.x, v.y, 0.0f);
  29. }
  30. public Vector2(float x, float y)
  31. {
  32. this.x = x;
  33. this.y = y;
  34. }
  35. public Vector2(float value)
  36. {
  37. this.x = this.y = value;
  38. }
  39. public float this[int index]
  40. {
  41. get
  42. {
  43. if (index == 0)
  44. return this.x;
  45. if (index == 1)
  46. return this.y;
  47. throw new IndexOutOfRangeException("Invalid Vector2 index!");
  48. }
  49. set
  50. {
  51. if (index != 0)
  52. {
  53. if (index != 1)
  54. throw new IndexOutOfRangeException("Invalid Vector2 index!");
  55. this.y = value;
  56. }
  57. else
  58. this.x = value;
  59. }
  60. }
  61. public override string ToString()
  62. {
  63. CultureInfo currentCulture = CultureInfo.CurrentCulture;
  64. return string.Format((IFormatProvider) currentCulture, "{0}, {1}",
  65. new object[2]
  66. {
  67. (object) this.x.ToString((IFormatProvider) currentCulture),
  68. (object) this.y.ToString((IFormatProvider) currentCulture)
  69. });
  70. }
  71. public bool Equals(Vector2 other)
  72. {
  73. return this == other;
  74. }
  75. public override bool Equals(object obj)
  76. {
  77. bool flag = false;
  78. if (obj is Vector2)
  79. flag = this.Equals((Vector2) obj);
  80. return flag;
  81. }
  82. public override int GetHashCode()
  83. {
  84. return this.x.GetHashCode() + this.y.GetHashCode();
  85. }
  86. public float Length()
  87. {
  88. return (float) Math.Sqrt((double) this.x * (double) this.x + (double) this.y * (double) this.y);
  89. }
  90. public float LengthSquared()
  91. {
  92. return (float) ((double) this.x * (double) this.x + (double) this.y * (double) this.y);
  93. }
  94. public float magnitude
  95. {
  96. get
  97. {
  98. return this.Length();
  99. }
  100. }
  101. public float sqrMagnitude
  102. {
  103. get
  104. {
  105. return this.LengthSquared();
  106. }
  107. }
  108. public static float Distance(Vector2 value1, Vector2 value2)
  109. {
  110. float num1 = value1.x - value2.x;
  111. float num2 = value1.y - value2.y;
  112. return (float) Math.Sqrt((double) num1 * (double) num1 + (double) num2 * (double) num2);
  113. }
  114. public static void Distance(ref Vector2 value1, ref Vector2 value2, out float result)
  115. {
  116. float num1 = value1.x - value2.x;
  117. float num2 = value1.y - value2.y;
  118. float num3 = (float) ((double) num1 * (double) num1 + (double) num2 * (double) num2);
  119. result = (float) Math.Sqrt((double) num3);
  120. }
  121. public static float DistanceSquared(Vector2 value1, Vector2 value2)
  122. {
  123. float num1 = value1.x - value2.x;
  124. float num2 = value1.y - value2.y;
  125. return (float) ((double) num1 * (double) num1 + (double) num2 * (double) num2);
  126. }
  127. public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
  128. {
  129. float num1 = value1.x - value2.x;
  130. float num2 = value1.y - value2.y;
  131. result = (float) ((double) num1 * (double) num1 + (double) num2 * (double) num2);
  132. }
  133. public void Normalize()
  134. {
  135. float num1 = (float) ((double) this.x * (double) this.x + (double) this.y * (double) this.y);
  136. if ((double) num1 < 9.99999974737875E-06)
  137. return;
  138. float num2 = 1f / (float) Math.Sqrt((double) num1);
  139. this.x *= num2;
  140. this.y *= num2;
  141. }
  142. public static Vector2 Normalize(Vector2 value)
  143. {
  144. float num1 = (float) ((double) value.x * (double) value.x + (double) value.y * (double) value.y);
  145. if ((double) num1 < 9.99999974737875E-06)
  146. return value;
  147. float num2 = 1f / (float) Math.Sqrt((double) num1);
  148. Vector2 vector2;
  149. vector2.x = value.x * num2;
  150. vector2.y = value.y * num2;
  151. return vector2;
  152. }
  153. public static void Normalize(ref Vector2 value, out Vector2 result)
  154. {
  155. float num1 = (float) ((double) value.x * (double) value.x + (double) value.y * (double) value.y);
  156. if ((double) num1 < 9.99999974737875E-06)
  157. {
  158. result = value;
  159. }
  160. else
  161. {
  162. float num2 = 1f / (float) Math.Sqrt((double) num1);
  163. result.x = value.x * num2;
  164. result.y = value.y * num2;
  165. }
  166. }
  167. public Vector2 normalized
  168. {
  169. get
  170. {
  171. return Vector2.Normalize(this);
  172. }
  173. }
  174. public static Vector2 Reflect(Vector2 vector, Vector2 normal)
  175. {
  176. float num = (float) ((double) vector.x * (double) normal.x + (double) vector.y * (double) normal.y);
  177. Vector2 vector2;
  178. vector2.x = vector.x - 2f * num * normal.x;
  179. vector2.y = vector.y - 2f * num * normal.y;
  180. return vector2;
  181. }
  182. public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
  183. {
  184. float num = (float) ((double) vector.x * (double) normal.x + (double) vector.y * (double) normal.y);
  185. result.x = vector.x - 2f * num * normal.x;
  186. result.y = vector.y - 2f * num * normal.y;
  187. }
  188. public static Vector2 Min(Vector2 value1, Vector2 value2)
  189. {
  190. Vector2 vector2;
  191. vector2.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
  192. vector2.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
  193. return vector2;
  194. }
  195. public static void Min(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  196. {
  197. result.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
  198. result.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
  199. }
  200. public static Vector2 Max(Vector2 value1, Vector2 value2)
  201. {
  202. Vector2 vector2;
  203. vector2.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
  204. vector2.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
  205. return vector2;
  206. }
  207. public static void Max(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  208. {
  209. result.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
  210. result.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
  211. }
  212. public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
  213. {
  214. float x = value1.x;
  215. float num1 = (double) x > (double) max.x? max.x : x;
  216. float num2 = (double) num1 < (double) min.x? min.x : num1;
  217. float y = value1.y;
  218. float num3 = (double) y > (double) max.y? max.y : y;
  219. float num4 = (double) num3 < (double) min.y? min.y : num3;
  220. Vector2 vector2;
  221. vector2.x = num2;
  222. vector2.y = num4;
  223. return vector2;
  224. }
  225. public static void Clamp(ref Vector2 value1, ref Vector2 min, ref Vector2 max, out Vector2 result)
  226. {
  227. float x = value1.x;
  228. float num1 = (double) x > (double) max.x? max.x : x;
  229. float num2 = (double) num1 < (double) min.x? min.x : num1;
  230. float y = value1.y;
  231. float num3 = (double) y > (double) max.y? max.y : y;
  232. float num4 = (double) num3 < (double) min.y? min.y : num3;
  233. result.x = num2;
  234. result.y = num4;
  235. }
  236. public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
  237. {
  238. Vector2 vector2;
  239. vector2.x = value1.x + (value2.x - value1.x) * amount;
  240. vector2.y = value1.y + (value2.y - value1.y) * amount;
  241. return vector2;
  242. }
  243. public static void Lerp(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
  244. {
  245. result.x = value1.x + (value2.x - value1.x) * amount;
  246. result.y = value1.y + (value2.y - value1.y) * amount;
  247. }
  248. public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, float amount)
  249. {
  250. amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
  251. amount = (float) ((double) amount * (double) amount * (3.0 - 2.0 * (double) amount));
  252. Vector2 vector2;
  253. vector2.x = value1.x + (value2.x - value1.x) * amount;
  254. vector2.y = value1.y + (value2.y - value1.y) * amount;
  255. return vector2;
  256. }
  257. public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
  258. {
  259. amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
  260. amount = (float) ((double) amount * (double) amount * (3.0 - 2.0 * (double) amount));
  261. result.x = value1.x + (value2.x - value1.x) * amount;
  262. result.y = value1.y + (value2.y - value1.y) * amount;
  263. }
  264. public static Vector2 Negate(Vector2 value)
  265. {
  266. Vector2 vector2;
  267. vector2.x = -value.x;
  268. vector2.y = -value.y;
  269. return vector2;
  270. }
  271. public static void Negate(ref Vector2 value, out Vector2 result)
  272. {
  273. result.x = -value.x;
  274. result.y = -value.y;
  275. }
  276. public static float Dot(Vector2 value1, Vector2 value2)
  277. {
  278. return (float) ((double) value1.x * (double) value2.x + (double) value1.y * (double) value2.y);
  279. }
  280. public static void Dot(ref Vector2 value1, ref Vector2 value2, out float result)
  281. {
  282. result = (float) ((double) value1.x * (double) value2.x + (double) value1.y * (double) value2.y);
  283. }
  284. public static float Angle(Vector2 from, Vector2 to)
  285. {
  286. from.Normalize();
  287. to.Normalize();
  288. float result;
  289. Vector2.Dot(ref from, ref to, out result);
  290. return Mathf.Cos(Mathf.Clamp(result, -1f, 1f)) * 57.29578f;
  291. }
  292. public static void Angle(ref Vector2 from, ref Vector2 to, out float result)
  293. {
  294. from.Normalize();
  295. to.Normalize();
  296. float result1;
  297. Vector2.Dot(ref from, ref to, out result1);
  298. result = Mathf.Cos(Mathf.Clamp(result1, -1f, 1f)) * 57.29578f;
  299. }
  300. public static Vector2 Add(Vector2 value1, Vector2 value2)
  301. {
  302. Vector2 vector2;
  303. vector2.x = value1.x + value2.x;
  304. vector2.y = value1.y + value2.y;
  305. return vector2;
  306. }
  307. public static void Add(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  308. {
  309. result.x = value1.x + value2.x;
  310. result.y = value1.y + value2.y;
  311. }
  312. public static Vector2 Sub(Vector2 value1, Vector2 value2)
  313. {
  314. Vector2 vector2;
  315. vector2.x = value1.x - value2.x;
  316. vector2.y = value1.y - value2.y;
  317. return vector2;
  318. }
  319. public static void Sub(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  320. {
  321. result.x = value1.x - value2.x;
  322. result.y = value1.y - value2.y;
  323. }
  324. public static Vector2 Multiply(Vector2 value1, Vector2 value2)
  325. {
  326. Vector2 vector2;
  327. vector2.x = value1.x * value2.x;
  328. vector2.y = value1.y * value2.y;
  329. return vector2;
  330. }
  331. public static void Multiply(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  332. {
  333. result.x = value1.x * value2.x;
  334. result.y = value1.y * value2.y;
  335. }
  336. public static Vector2 Multiply(Vector2 value1, float scaleFactor)
  337. {
  338. Vector2 vector2;
  339. vector2.x = value1.x * scaleFactor;
  340. vector2.y = value1.y * scaleFactor;
  341. return vector2;
  342. }
  343. public static void Multiply(ref Vector2 value1, float scaleFactor, out Vector2 result)
  344. {
  345. result.x = value1.x * scaleFactor;
  346. result.y = value1.y * scaleFactor;
  347. }
  348. public static Vector2 Divide(Vector2 value1, Vector2 value2)
  349. {
  350. Vector2 vector2;
  351. vector2.x = value1.x / value2.x;
  352. vector2.y = value1.y / value2.y;
  353. return vector2;
  354. }
  355. public static void Divide(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
  356. {
  357. result.x = value1.x / value2.x;
  358. result.y = value1.y / value2.y;
  359. }
  360. public static Vector2 Divide(Vector2 value1, float divider)
  361. {
  362. float num = 1f / divider;
  363. Vector2 vector2;
  364. vector2.x = value1.x * num;
  365. vector2.y = value1.y * num;
  366. return vector2;
  367. }
  368. public static void Divide(ref Vector2 value1, float divider, out Vector2 result)
  369. {
  370. float num = 1f / divider;
  371. result.x = value1.x * num;
  372. result.y = value1.y * num;
  373. }
  374. public static Vector2 operator -(Vector2 value)
  375. {
  376. Vector2 vector2;
  377. vector2.x = -value.x;
  378. vector2.y = -value.y;
  379. return vector2;
  380. }
  381. public static bool operator ==(Vector2 lhs, Vector2 rhs)
  382. {
  383. return (double) (lhs - rhs).sqrMagnitude < 9.99999943962493E-11;
  384. }
  385. public static bool operator !=(Vector2 lhs, Vector2 rhs)
  386. {
  387. return !(lhs == rhs);
  388. }
  389. public static Vector2 operator +(Vector2 value1, Vector2 value2)
  390. {
  391. Vector2 vector2;
  392. vector2.x = value1.x + value2.x;
  393. vector2.y = value1.y + value2.y;
  394. return vector2;
  395. }
  396. public static Vector2 operator -(Vector2 value1, Vector2 value2)
  397. {
  398. Vector2 vector2;
  399. vector2.x = value1.x - value2.x;
  400. vector2.y = value1.y - value2.y;
  401. return vector2;
  402. }
  403. public static Vector2 operator *(Vector2 value1, Vector2 value2)
  404. {
  405. Vector2 vector2;
  406. vector2.x = value1.x * value2.x;
  407. vector2.y = value1.y * value2.y;
  408. return vector2;
  409. }
  410. public static Vector2 operator *(Vector2 value, float scaleFactor)
  411. {
  412. Vector2 vector2;
  413. vector2.x = value.x * scaleFactor;
  414. vector2.y = value.y * scaleFactor;
  415. return vector2;
  416. }
  417. public static Vector2 operator *(float scaleFactor, Vector2 value)
  418. {
  419. Vector2 vector2;
  420. vector2.x = value.x * scaleFactor;
  421. vector2.y = value.y * scaleFactor;
  422. return vector2;
  423. }
  424. public static Vector2 operator /(Vector2 value1, Vector2 value2)
  425. {
  426. Vector2 vector2;
  427. vector2.x = value1.x / value2.x;
  428. vector2.y = value1.y / value2.y;
  429. return vector2;
  430. }
  431. public static Vector2 operator /(Vector2 value1, float divider)
  432. {
  433. float num = 1f / divider;
  434. Vector2 vector2;
  435. vector2.x = value1.x * num;
  436. vector2.y = value1.y * num;
  437. return vector2;
  438. }
  439. }
  440. }