Fix64.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. using System;
  2. using System.IO;
  3. public partial struct Fix64 : IEquatable<Fix64>, IComparable<Fix64> {
  4. readonly long m_rawValue;
  5. public static readonly decimal Precision = (decimal)(new Fix64(1L));
  6. public static readonly Fix64 One = new Fix64(ONE);
  7. public static readonly Fix64 Zero = new Fix64();
  8. public static readonly Fix64 PI = new Fix64(Pi);
  9. public static readonly Fix64 PITimes2 = new Fix64(PiTimes2);
  10. public static readonly Fix64 PIOver180 = new Fix64((long)72);
  11. public static readonly Fix64 Rad2Deg = Fix64.Pi * (Fix64)2 / (Fix64)360;
  12. public static readonly Fix64 Deg2Rad = (Fix64)360 / (Fix64.Pi * (Fix64)2);
  13. const long Pi = 12868;
  14. const long PiTimes2 = 25736;
  15. public const int FRACTIONAL_PLACES = 12;
  16. const long ONE = 1L << FRACTIONAL_PLACES;
  17. public static int Sign(Fix64 value) {
  18. return
  19. value.m_rawValue < 0 ? -1 :
  20. value.m_rawValue > 0 ? 1 :
  21. 0;
  22. }
  23. public static Fix64 Abs(Fix64 value) {
  24. return new Fix64(value.m_rawValue > 0 ? value.m_rawValue : -value.m_rawValue);
  25. }
  26. public static Fix64 Floor(Fix64 value) {
  27. return new Fix64((long)((ulong)value.m_rawValue & 0xFFFFFFFFFFFFF000));
  28. }
  29. public static Fix64 Ceiling(Fix64 value) {
  30. var hasFractionalPart = (value.m_rawValue & 0x0000000000000FFF) != 0;
  31. return hasFractionalPart ? Floor(value) + One : value;
  32. }
  33. public static Fix64 Max(Fix64 value1, Fix64 value2)
  34. {
  35. Fix64 clampvalue = value1 > value2 ? value1 : value2;
  36. return clampvalue;
  37. }
  38. public static Fix64 Clamp(Fix64 value,Fix64 Min,Fix64 Max)
  39. {
  40. Fix64 clampvalue = value < Min ? Min : value;
  41. clampvalue = value > Max ? Max : value;
  42. return clampvalue;
  43. }
  44. public static Fix64 operator +(Fix64 x, Fix64 y) {
  45. return new Fix64(x.m_rawValue + y.m_rawValue);
  46. }
  47. public static Fix64 operator +(Fix64 x, int y)
  48. {
  49. return x + (Fix64)y;
  50. }
  51. public static Fix64 operator +(int x, Fix64 y)
  52. {
  53. return (Fix64)x + y;
  54. }
  55. public static Fix64 operator +(Fix64 x, float y)
  56. {
  57. return x + (Fix64)y;
  58. }
  59. public static Fix64 operator +(float x, Fix64 y)
  60. {
  61. return (Fix64)x + y;
  62. }
  63. public static Fix64 operator +(Fix64 x, double y)
  64. {
  65. return x + (Fix64)y;
  66. }
  67. public static Fix64 operator +(double x, Fix64 y)
  68. {
  69. return (Fix64)x + y;
  70. }
  71. public static Fix64 operator -(Fix64 x, Fix64 y) {
  72. return new Fix64(x.m_rawValue - y.m_rawValue);
  73. }
  74. public static Fix64 operator -(Fix64 x, int y)
  75. {
  76. return x - (Fix64)y;
  77. }
  78. public static Fix64 operator -(int x, Fix64 y)
  79. {
  80. return (Fix64)x - y;
  81. }
  82. public static Fix64 operator -(Fix64 x, float y)
  83. {
  84. return x - (Fix64)y;
  85. }
  86. public static Fix64 operator -(float x, Fix64 y)
  87. {
  88. return (Fix64)x + y;
  89. }
  90. public static Fix64 operator -(Fix64 x, double y)
  91. {
  92. return x - (Fix64)y;
  93. }
  94. public static Fix64 operator -(double x, Fix64 y)
  95. {
  96. return (Fix64)x - y;
  97. }
  98. public static Fix64 operator *(Fix64 x, Fix64 y) {
  99. return new Fix64((x.m_rawValue * y.m_rawValue) >> FRACTIONAL_PLACES);
  100. }
  101. public static Fix64 operator *(Fix64 x, int y)
  102. {
  103. return x * (Fix64)y;
  104. }
  105. public static Fix64 operator *(int x, Fix64 y)
  106. {
  107. return (Fix64)x * y;
  108. }
  109. public static Fix64 operator *(Fix64 x, float y)
  110. {
  111. return x * (Fix64)y;
  112. }
  113. public static Fix64 operator *(float x, Fix64 y)
  114. {
  115. return (Fix64)x * y;
  116. }
  117. public static Fix64 operator *(Fix64 x, double y)
  118. {
  119. return x * (Fix64)y;
  120. }
  121. public static Fix64 operator *(double x, Fix64 y)
  122. {
  123. return (Fix64)x * y;
  124. }
  125. public static Fix64 operator /(Fix64 x, Fix64 y) {
  126. return new Fix64((x.m_rawValue << FRACTIONAL_PLACES) / y.m_rawValue);
  127. }
  128. public static Fix64 operator /(Fix64 x, int y)
  129. {
  130. return x / (Fix64)y;
  131. }
  132. public static Fix64 operator /(int x, Fix64 y)
  133. {
  134. return (Fix64)x / y;
  135. }
  136. public static Fix64 operator /(Fix64 x, float y)
  137. {
  138. return x / (Fix64)y;
  139. }
  140. public static Fix64 operator /(float x, Fix64 y)
  141. {
  142. return (Fix64)x / y;
  143. }
  144. public static Fix64 operator /(double x, Fix64 y)
  145. {
  146. return (Fix64)x / y;
  147. }
  148. public static Fix64 operator /(Fix64 x, double y)
  149. {
  150. return x / (Fix64)y;
  151. }
  152. public static Fix64 operator %(Fix64 x, Fix64 y) {
  153. return new Fix64(x.m_rawValue % y.m_rawValue);
  154. }
  155. public static Fix64 operator -(Fix64 x) {
  156. return new Fix64(-x.m_rawValue);
  157. }
  158. public static bool operator ==(Fix64 x, Fix64 y) {
  159. return x.m_rawValue == y.m_rawValue;
  160. }
  161. public static bool operator !=(Fix64 x, Fix64 y) {
  162. return x.m_rawValue != y.m_rawValue;
  163. }
  164. public static bool operator >(Fix64 x, Fix64 y) {
  165. return x.m_rawValue > y.m_rawValue;
  166. }
  167. public static bool operator >(Fix64 x, int y)
  168. {
  169. return x > (Fix64)y;
  170. }
  171. public static bool operator <(Fix64 x, int y)
  172. {
  173. return x < (Fix64)y;
  174. }
  175. public static bool operator >(Fix64 x, float y)
  176. {
  177. return x > (Fix64)y;
  178. }
  179. public static bool operator <(Fix64 x, float y)
  180. {
  181. return x < (Fix64)y;
  182. }
  183. public static bool operator <(Fix64 x, Fix64 y) {
  184. return x.m_rawValue < y.m_rawValue;
  185. }
  186. public static bool operator >=(Fix64 x, Fix64 y) {
  187. return x.m_rawValue >= y.m_rawValue;
  188. }
  189. public static bool operator <=(Fix64 x, Fix64 y) {
  190. return x.m_rawValue <= y.m_rawValue;
  191. }
  192. public static Fix64 operator >> (Fix64 x, int amount)
  193. {
  194. return new Fix64(x.RawValue >> amount);
  195. }
  196. public static Fix64 operator << (Fix64 x, int amount)
  197. {
  198. return new Fix64(x.RawValue << amount);
  199. }
  200. public static explicit operator Fix64(long value) {
  201. return new Fix64(value * ONE);
  202. }
  203. public static explicit operator long(Fix64 value) {
  204. return value.m_rawValue >> FRACTIONAL_PLACES;
  205. }
  206. public static explicit operator Fix64(float value) {
  207. return new Fix64((long)(value * ONE));
  208. }
  209. public static explicit operator float(Fix64 value) {
  210. return (float)value.m_rawValue / ONE;
  211. }
  212. public static explicit operator int(Fix64 value)
  213. {
  214. return (int)((float)value);
  215. }
  216. public static explicit operator Fix64(double value) {
  217. return new Fix64((long)(value * ONE));
  218. }
  219. public static explicit operator double(Fix64 value) {
  220. return (double)value.m_rawValue / ONE;
  221. }
  222. public static explicit operator Fix64(decimal value) {
  223. return new Fix64((long)(value * ONE));
  224. }
  225. public static explicit operator decimal(Fix64 value) {
  226. return (decimal)value.m_rawValue / ONE;
  227. }
  228. public override bool Equals(object obj)
  229. {
  230. return obj is Fix64 && ((Fix64)obj).m_rawValue == m_rawValue;
  231. }
  232. public override int GetHashCode() {
  233. return m_rawValue.GetHashCode();
  234. }
  235. public bool Equals(Fix64 other) {
  236. return m_rawValue == other.m_rawValue;
  237. }
  238. public int CompareTo(Fix64 other) {
  239. return m_rawValue.CompareTo(other.m_rawValue);
  240. }
  241. public override string ToString() {
  242. return ((decimal)this).ToString();
  243. }
  244. public string ToStringRound(int round = 2)
  245. {
  246. return (float)Math.Round((float)this, round) + "";
  247. }
  248. public static Fix64 FromRaw(long rawValue) {
  249. return new Fix64(rawValue);
  250. }
  251. public static Fix64 Pow(Fix64 x, int y)
  252. {
  253. if (y == 1) return x;
  254. Fix64 result = Fix64.Zero;
  255. Fix64 tmp = Pow(x, y / 2);
  256. if ((y & 1) != 0) //奇数
  257. {
  258. result = x * tmp * tmp;
  259. }
  260. else
  261. {
  262. result = tmp * tmp;
  263. }
  264. return result;
  265. }
  266. public long RawValue { get { return m_rawValue; } }
  267. Fix64(long rawValue) {
  268. m_rawValue = rawValue;
  269. }
  270. public Fix64(int value) {
  271. m_rawValue = value * ONE;
  272. }
  273. public static Fix64 Sqrt(Fix64 f, int numberIterations)
  274. {
  275. if (f.RawValue < 0)
  276. {
  277. throw new ArithmeticException("sqrt error");
  278. }
  279. if (f.RawValue == 0)
  280. return Fix64.Zero;
  281. Fix64 k = f + Fix64.One >> 1;
  282. for (int i = 0; i < numberIterations; i++)
  283. k = (k + (f / k)) >> 1;
  284. if (k.RawValue < 0)
  285. throw new ArithmeticException("Overflow");
  286. else
  287. return k;
  288. }
  289. public static Fix64 Lerp(Fix64 from, Fix64 to, Fix64 factor)
  290. {
  291. return from * (1 - factor) + to * factor;
  292. }
  293. public static Fix64 Sqrt(Fix64 f)
  294. {
  295. byte numberOfIterations = 8;
  296. if (f.RawValue > 0x64000)
  297. numberOfIterations = 12;
  298. if (f.RawValue > 0x3e8000)
  299. numberOfIterations = 16;
  300. return Sqrt(f, numberOfIterations);
  301. }
  302. #region Sin
  303. public static Fix64 Sin(Fix64 i)
  304. {
  305. Fix64 j = (Fix64)0;
  306. for (; i < Fix64.Zero; i += Fix64.FromRaw(PiTimes2));
  307. if (i > Fix64.FromRaw(PiTimes2))
  308. i %= Fix64.FromRaw(PiTimes2);
  309. Fix64 k = (i * Fix64.FromRaw(100000000)) / Fix64.FromRaw(7145244444);
  310. if (i != Fix64.Zero && i != Fix64.FromRaw(6434) && i != Fix64.FromRaw(Pi) &&
  311. i != Fix64.FromRaw(19302) && i != Fix64.FromRaw(PiTimes2))
  312. j = (i * Fix64.FromRaw(100000000)) / Fix64.FromRaw(7145244444) - k * Fix64.FromRaw(10);
  313. if (k <= Fix64.FromRaw(90))
  314. return sin_lookup(k, j);
  315. if (k <= Fix64.FromRaw(180))
  316. return sin_lookup(Fix64.FromRaw(180) - k, j);
  317. if (k <= Fix64.FromRaw(270))
  318. return -sin_lookup(k - Fix64.FromRaw(180), j);
  319. else
  320. return -sin_lookup(Fix64.FromRaw(360) - k, j);
  321. }
  322. private static Fix64 sin_lookup(Fix64 i, Fix64 j)
  323. {
  324. if (j > 0 && j < Fix64.FromRaw(10) && i < Fix64.FromRaw(90))
  325. return Fix64.FromRaw(SIN_TABLE[i.RawValue]) +
  326. ((Fix64.FromRaw(SIN_TABLE[i.RawValue + 1]) - Fix64.FromRaw(SIN_TABLE[i.RawValue])) /
  327. Fix64.FromRaw(10)) * j;
  328. else
  329. return Fix64.FromRaw(SIN_TABLE[i.RawValue]);
  330. }
  331. private static int[] SIN_TABLE = {
  332. 0, 71, 142, 214, 285, 357, 428, 499, 570, 641,
  333. 711, 781, 851, 921, 990, 1060, 1128, 1197, 1265, 1333,
  334. 1400, 1468, 1534, 1600, 1665, 1730, 1795, 1859, 1922, 1985,
  335. 2048, 2109, 2170, 2230, 2290, 2349, 2407, 2464, 2521, 2577,
  336. 2632, 2686, 2740, 2793, 2845, 2896, 2946, 2995, 3043, 3091,
  337. 3137, 3183, 3227, 3271, 3313, 3355, 3395, 3434, 3473, 3510,
  338. 3547, 3582, 3616, 3649, 3681, 3712, 3741, 3770, 3797, 3823,
  339. 3849, 3872, 3895, 3917, 3937, 3956, 3974, 3991, 4006, 4020,
  340. 4033, 4045, 4056, 4065, 4073, 4080, 4086, 4090, 4093, 4095,
  341. 4096
  342. };
  343. #endregion
  344. #region Cos, Tan, Asin
  345. public static Fix64 Cos(Fix64 i)
  346. {
  347. return Sin(i + Fix64.FromRaw(6435));
  348. }
  349. public static Fix64 Tan(Fix64 i)
  350. {
  351. return Sin(i) / Cos(i);
  352. }
  353. public static Fix64 Asin(Fix64 F)
  354. {
  355. bool isNegative = F < 0;
  356. F = Abs(F);
  357. if (F > Fix64.One)
  358. throw new ArithmeticException("Bad Asin Input:" + (double)F);
  359. Fix64 f1 = ((((Fix64.FromRaw(145103 >> FRACTIONAL_PLACES) * F) -
  360. Fix64.FromRaw(599880 >> FRACTIONAL_PLACES)* F) +
  361. Fix64.FromRaw(1420468 >> FRACTIONAL_PLACES)* F) -
  362. Fix64.FromRaw(3592413 >> FRACTIONAL_PLACES)* F) +
  363. Fix64.FromRaw(26353447 >> FRACTIONAL_PLACES);
  364. Fix64 f2 = PI / (Fix64)2 - (Sqrt(Fix64.One - F) * f1);
  365. return isNegative ? -f2 : f2;
  366. }
  367. #endregion
  368. #region ATan, ATan2
  369. public static Fix64 Atan(Fix64 F)
  370. {
  371. return Asin(F / Sqrt(Fix64.One + (F * F)));
  372. }
  373. public static Fix64 Atan2(Fix64 F1, Fix64 F2)
  374. {
  375. if (F2.RawValue == 0 && F1.RawValue == 0)
  376. return (Fix64)0;
  377. Fix64 result = (Fix64)0;
  378. if (F2 > 0)
  379. result = Atan(F1 / F2);
  380. else if (F2 < 0)
  381. {
  382. if (F1 >= (Fix64)0)
  383. result = (PI - Atan(Abs(F1 / F2)));
  384. else
  385. result = -(PI - Atan(Abs(F1 / F2)));
  386. }
  387. else
  388. result = (F1 >= (Fix64)0 ? PI : -PI) / (Fix64)2;
  389. return result;
  390. }
  391. #endregion
  392. }
  393. public struct FixVector3
  394. {
  395. public Fix64 x;
  396. public Fix64 y;
  397. public Fix64 z;
  398. public FixVector3(Fix64 x, Fix64 y, Fix64 z)
  399. {
  400. this.x = x;
  401. this.y = y;
  402. this.z = z;
  403. }
  404. public FixVector3(UnityEngine.Vector3 v3Pos)
  405. {
  406. this.x = (Fix64)v3Pos.x;
  407. this.y = (Fix64)v3Pos.y;
  408. this.z = (Fix64)v3Pos.z;
  409. }
  410. public FixVector3(FixVector3 v)
  411. {
  412. this.x = v.x;
  413. this.y = v.y;
  414. this.z = v.z;
  415. }
  416. public Fix64 this[int index]
  417. {
  418. get
  419. {
  420. if (index == 0)
  421. return x;
  422. else if (index == 1)
  423. return y;
  424. else
  425. return z;
  426. }
  427. set
  428. {
  429. if (index == 0)
  430. x = value;
  431. else if (index == 1)
  432. y = value;
  433. else
  434. y = value;
  435. }
  436. }
  437. public static FixVector3 Up
  438. {
  439. get { return new FixVector3(Fix64.Zero, Fix64.One, Fix64.Zero); }
  440. }
  441. public static FixVector3 Zero
  442. {
  443. get { return new FixVector3(Fix64.Zero, Fix64.Zero, Fix64.Zero); }
  444. }
  445. public static FixVector3 operator +(FixVector3 a, FixVector3 b)
  446. {
  447. Fix64 x = a.x + b.x;
  448. Fix64 y = a.y + b.y;
  449. Fix64 z = a.z + b.z;
  450. return new FixVector3(x, y, z);
  451. }
  452. public static FixVector3 operator -(FixVector3 a, FixVector3 b)
  453. {
  454. Fix64 x = a.x - b.x;
  455. Fix64 y = a.y - b.y;
  456. Fix64 z = a.z - b.z;
  457. return new FixVector3(x, y, z);
  458. }
  459. public static FixVector3 operator *(Fix64 d, FixVector3 a)
  460. {
  461. Fix64 x = a.x * d;
  462. Fix64 y = a.y * d;
  463. Fix64 z = a.z * d;
  464. return new FixVector3(x, y, z);
  465. }
  466. public static FixVector3 operator *(FixVector3 a, Fix64 d)
  467. {
  468. Fix64 x = a.x * d;
  469. Fix64 y = a.y * d;
  470. Fix64 z = a.z * d;
  471. return new FixVector3(x, y, z);
  472. }
  473. public static FixVector3 operator /(FixVector3 a, Fix64 d)
  474. {
  475. Fix64 x = a.x / d;
  476. Fix64 y = a.y / d;
  477. Fix64 z = a.z / d;
  478. return new FixVector3(x, y, z);
  479. }
  480. public static Fix64 Dot(FixVector3 a, FixVector3 b)
  481. {
  482. return a.x*b.x + a.y*b.y + a.z*b.z;
  483. }
  484. public static FixVector3 Cross(FixVector3 a, FixVector3 b)
  485. {
  486. Fix64 x = a.y * b.z - a.z * b.y;
  487. Fix64 y = a.z * b.x - a.x * b.z;
  488. Fix64 z = a.x * b.y - a.y * b.x;
  489. return new FixVector3(x, y, z);
  490. }
  491. public static bool operator ==(FixVector3 lhs, FixVector3 rhs)
  492. {
  493. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  494. }
  495. public static bool operator !=(FixVector3 lhs, FixVector3 rhs)
  496. {
  497. return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z;
  498. }
  499. public static Fix64 SqrMagnitude(FixVector3 a)
  500. {
  501. return a.x * a.x + a.y * a.y + a.z * a.z;
  502. }
  503. public static Fix64 Distance(FixVector3 a, FixVector3 b)
  504. {
  505. return Magnitude(a - b);
  506. }
  507. public static Fix64 Magnitude(FixVector3 a)
  508. {
  509. return Fix64.Sqrt(FixVector3.SqrMagnitude(a));
  510. }
  511. public void Normalize()
  512. {
  513. Fix64 n = x * x + y * y + z * z;
  514. if (n == Fix64.Zero)
  515. return;
  516. n = Fix64.Sqrt(n);
  517. if (n < (Fix64)0.0001)
  518. {
  519. return;
  520. }
  521. n = 1 / n;
  522. x *= n;
  523. y *= n;
  524. z *= n;
  525. }
  526. public FixVector3 GetNormalized()
  527. {
  528. FixVector3 v = new FixVector3(this);
  529. v.Normalize();
  530. return v;
  531. }
  532. public override string ToString()
  533. {
  534. return string.Format("x:{0} y:{1} z:{2}", x, y, z);
  535. }
  536. public override bool Equals(object obj)
  537. {
  538. return obj is FixVector2 && ((FixVector3)obj) == this;
  539. }
  540. public override int GetHashCode()
  541. {
  542. return this.x.GetHashCode() + this.y.GetHashCode() + this.z.GetHashCode();
  543. }
  544. public static FixVector3 Lerp(FixVector3 from, FixVector3 to, Fix64 factor)
  545. {
  546. return from * (1 - factor) + to * factor;
  547. }
  548. public UnityEngine.Vector3 ToVector3()
  549. {
  550. return new UnityEngine.Vector3((float)x, (float)y, (float)z);
  551. }
  552. }
  553. public struct FixVector2
  554. {
  555. public Fix64 x;
  556. public Fix64 y;
  557. public FixVector2(Fix64 x, Fix64 y)
  558. {
  559. this.x = x;
  560. this.y = y;
  561. }
  562. public FixVector2(Fix64 x, int y)
  563. {
  564. this.x = x;
  565. this.y = (Fix64)y;
  566. }
  567. public FixVector2(int x, int y)
  568. {
  569. this.x = (Fix64)x;
  570. this.y = (Fix64)y;
  571. }
  572. public FixVector2(FixVector2 v)
  573. {
  574. this.x = v.x;
  575. this.y = v.y;
  576. }
  577. public static FixVector2 operator -(FixVector2 a, int b)
  578. {
  579. Fix64 x = a.x - b;
  580. Fix64 y = a.y - b;
  581. return new FixVector2(x, y);
  582. }
  583. public Fix64 this[int index]
  584. {
  585. get { return index == 0 ? x : y; }
  586. set
  587. {
  588. if (index == 0)
  589. {
  590. x = value;
  591. }
  592. else
  593. {
  594. y = value;
  595. }
  596. }
  597. }
  598. public static FixVector2 Zero
  599. {
  600. get { return new FixVector2(Fix64.Zero, Fix64.Zero); }
  601. }
  602. public static FixVector2 operator +(FixVector2 a, FixVector2 b)
  603. {
  604. Fix64 x = a.x + b.x;
  605. Fix64 y = a.y + b.y;
  606. return new FixVector2(x, y);
  607. }
  608. public static FixVector2 operator -(FixVector2 a, FixVector2 b)
  609. {
  610. Fix64 x = a.x - b.x;
  611. Fix64 y = a.y - b.y;
  612. return new FixVector2(x, y);
  613. }
  614. public static FixVector2 operator *(Fix64 d, FixVector2 a)
  615. {
  616. Fix64 x = a.x * d;
  617. Fix64 y = a.y * d;
  618. return new FixVector2(x, y);
  619. }
  620. public static FixVector2 operator *(FixVector2 a, Fix64 d)
  621. {
  622. Fix64 x = a.x * d;
  623. Fix64 y = a.y * d;
  624. return new FixVector2(x, y);
  625. }
  626. public static FixVector2 operator /(FixVector2 a, Fix64 d)
  627. {
  628. Fix64 x = a.x / d;
  629. Fix64 y = a.y / d;
  630. return new FixVector2(x, y);
  631. }
  632. public static bool operator ==(FixVector2 lhs, FixVector2 rhs)
  633. {
  634. return lhs.x == rhs.x && lhs.y == rhs.y;
  635. }
  636. public static bool operator !=(FixVector2 lhs, FixVector2 rhs)
  637. {
  638. return lhs.x != rhs.x || lhs.y != rhs.y;
  639. }
  640. public override bool Equals(object obj)
  641. {
  642. return obj is FixVector2 && ((FixVector2)obj) == this;
  643. }
  644. public override int GetHashCode()
  645. {
  646. return this.x.GetHashCode() + this.y.GetHashCode();
  647. }
  648. public static Fix64 SqrMagnitude(FixVector2 a)
  649. {
  650. return a.x * a.x + a.y * a.y;
  651. }
  652. public static Fix64 Distance(FixVector2 a, FixVector2 b)
  653. {
  654. return Magnitude(a - b);
  655. }
  656. public static Fix64 Magnitude(FixVector2 a)
  657. {
  658. return Fix64.Sqrt(FixVector2.SqrMagnitude(a));
  659. }
  660. public void Normalize()
  661. {
  662. Fix64 n = x * x + y * y;
  663. if (n == Fix64.Zero)
  664. return;
  665. n = Fix64.Sqrt(n);
  666. if (n < (Fix64)0.0001)
  667. {
  668. return;
  669. }
  670. n = 1 / n;
  671. x *= n;
  672. y *= n;
  673. }
  674. public FixVector2 GetNormalized()
  675. {
  676. FixVector2 v = new FixVector2(this);
  677. v.Normalize();
  678. return v;
  679. }
  680. public override string ToString()
  681. {
  682. return string.Format("x:{0} y:{1}", x, y);
  683. }
  684. #if _CLIENTLOGIC_
  685. public UnityEngine.Vector2 ToVector2()
  686. {
  687. return new UnityEngine.Vector2((float)x, (float)y);
  688. }
  689. #endif
  690. }
  691. public struct NormalVector2
  692. {
  693. public float x;
  694. public float y;
  695. public NormalVector2(float x, float y)
  696. {
  697. this.x = x;
  698. this.y = y;
  699. }
  700. public NormalVector2(int x, int y)
  701. {
  702. this.x = x;
  703. this.y = y;
  704. }
  705. public NormalVector2(NormalVector2 v)
  706. {
  707. this.x = v.x;
  708. this.y = v.y;
  709. }
  710. public static NormalVector2 operator -(NormalVector2 a, int b)
  711. {
  712. float x = a.x - b;
  713. float y = a.y - b;
  714. return new NormalVector2(x, y);
  715. }
  716. public float this[int index]
  717. {
  718. get { return index == 0 ? x : y; }
  719. set
  720. {
  721. if (index == 0)
  722. {
  723. x = value;
  724. }
  725. else
  726. {
  727. y = value;
  728. }
  729. }
  730. }
  731. public static NormalVector2 Zero
  732. {
  733. get { return new NormalVector2(0, 0); }
  734. }
  735. public static NormalVector2 operator +(NormalVector2 a, NormalVector2 b)
  736. {
  737. float x = a.x + b.x;
  738. float y = a.y + b.y;
  739. return new NormalVector2(x, y);
  740. }
  741. public static NormalVector2 operator -(NormalVector2 a, NormalVector2 b)
  742. {
  743. float x = a.x - b.x;
  744. float y = a.y - b.y;
  745. return new NormalVector2(x, y);
  746. }
  747. public static NormalVector2 operator *(float d, NormalVector2 a)
  748. {
  749. float x = a.x * d;
  750. float y = a.y * d;
  751. return new NormalVector2(x, y);
  752. }
  753. public static NormalVector2 operator *(NormalVector2 a, float d)
  754. {
  755. float x = a.x * d;
  756. float y = a.y * d;
  757. return new NormalVector2(x, y);
  758. }
  759. public static NormalVector2 operator /(NormalVector2 a, float d)
  760. {
  761. float x = a.x / d;
  762. float y = a.y / d;
  763. return new NormalVector2(x, y);
  764. }
  765. public static bool operator ==(NormalVector2 lhs, NormalVector2 rhs)
  766. {
  767. return lhs.x == rhs.x && lhs.y == rhs.y;
  768. }
  769. public static bool operator !=(NormalVector2 lhs, NormalVector2 rhs)
  770. {
  771. return lhs.x != rhs.x || lhs.y != rhs.y;
  772. }
  773. public override bool Equals(object obj)
  774. {
  775. return obj is NormalVector2 && ((NormalVector2)obj) == this;
  776. }
  777. public override int GetHashCode()
  778. {
  779. return this.x.GetHashCode() + this.y.GetHashCode();
  780. }
  781. public static float SqrMagnitude(NormalVector2 a)
  782. {
  783. return a.x * a.x + a.y * a.y;
  784. }
  785. public static float Distance(NormalVector2 a, NormalVector2 b)
  786. {
  787. return Magnitude(a - b);
  788. }
  789. public static float Magnitude(NormalVector2 a)
  790. {
  791. return NormalVector2.SqrMagnitude(a);
  792. }
  793. public void Normalize()
  794. {
  795. float n = x * x + y * y;
  796. if (n == 0)
  797. return;
  798. //n = float.Sqrt(n);
  799. if (n < (float)0.0001)
  800. {
  801. return;
  802. }
  803. n = 1 / n;
  804. x *= n;
  805. y *= n;
  806. }
  807. public NormalVector2 GetNormalized()
  808. {
  809. NormalVector2 v = new NormalVector2(this);
  810. v.Normalize();
  811. return v;
  812. }
  813. public override string ToString()
  814. {
  815. return string.Format("x:{0} y:{1}", x, y);
  816. }
  817. public UnityEngine.Vector2 ToVector2()
  818. {
  819. return new UnityEngine.Vector2((float)x, (float)y);
  820. }
  821. }
  822. public struct IntVector2
  823. {
  824. public int x;
  825. public int y;
  826. public IntVector2(int x, int y)
  827. {
  828. this.x = x;
  829. this.y = y;
  830. }
  831. public IntVector2(IntVector2 v)
  832. {
  833. this.x = v.x;
  834. this.y = v.y;
  835. }
  836. public static IntVector2 operator -(IntVector2 a, int b)
  837. {
  838. int x = a.x - b;
  839. int y = a.y - b;
  840. return new IntVector2(x, y);
  841. }
  842. public int this[int index]
  843. {
  844. get { return index == 0 ? x : y; }
  845. set
  846. {
  847. if (index == 0)
  848. {
  849. x = value;
  850. }
  851. else
  852. {
  853. y = value;
  854. }
  855. }
  856. }
  857. public static IntVector2 Zero
  858. {
  859. get { return new IntVector2(0, 0); }
  860. }
  861. public static IntVector2 operator +(IntVector2 a, IntVector2 b)
  862. {
  863. int x = a.x + b.x;
  864. int y = a.y + b.y;
  865. return new IntVector2(x, y);
  866. }
  867. public static IntVector2 operator -(IntVector2 a, IntVector2 b)
  868. {
  869. int x = a.x - b.x;
  870. int y = a.y - b.y;
  871. return new IntVector2(x, y);
  872. }
  873. public static IntVector2 operator *(int d, IntVector2 a)
  874. {
  875. int x = a.x * d;
  876. int y = a.y * d;
  877. return new IntVector2(x, y);
  878. }
  879. public static IntVector2 operator *(IntVector2 a, int d)
  880. {
  881. int x = a.x * d;
  882. int y = a.y * d;
  883. return new IntVector2(x, y);
  884. }
  885. public static IntVector2 operator /(IntVector2 a, int d)
  886. {
  887. int x = a.x / d;
  888. int y = a.y / d;
  889. return new IntVector2(x, y);
  890. }
  891. public static bool operator ==(IntVector2 lhs, IntVector2 rhs)
  892. {
  893. return lhs.x == rhs.x && lhs.y == rhs.y;
  894. }
  895. public static bool operator !=(IntVector2 lhs, IntVector2 rhs)
  896. {
  897. return lhs.x != rhs.x || lhs.y != rhs.y;
  898. }
  899. public override bool Equals(object obj)
  900. {
  901. return obj is IntVector2 && ((IntVector2)obj) == this;
  902. }
  903. public override int GetHashCode()
  904. {
  905. return this.x.GetHashCode() + this.y.GetHashCode();
  906. }
  907. public static int SqrMagnitude(IntVector2 a)
  908. {
  909. return a.x * a.x + a.y * a.y;
  910. }
  911. public static int Distance(IntVector2 a, IntVector2 b)
  912. {
  913. return Magnitude(a - b);
  914. }
  915. public static int Magnitude(IntVector2 a)
  916. {
  917. return IntVector2.SqrMagnitude(a);
  918. }
  919. public void Normalize()
  920. {
  921. int n = x * x + y * y;
  922. if (n == 0)
  923. return;
  924. //n = int.Sqrt(n);
  925. if (n < (int)0.0001)
  926. {
  927. return;
  928. }
  929. n = 1 / n;
  930. x *= n;
  931. y *= n;
  932. }
  933. public IntVector2 GetNormalized()
  934. {
  935. IntVector2 v = new IntVector2(this);
  936. v.Normalize();
  937. return v;
  938. }
  939. public override string ToString()
  940. {
  941. return string.Format("x:{0} y:{1}", x, y);
  942. }
  943. public UnityEngine.Vector2 ToVector2()
  944. {
  945. return new UnityEngine.Vector2((int)x, (int)y);
  946. }
  947. }