Vector2.cs 17 KB

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