MathUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var a;
  5. function r(t) {
  6. return -1 < "+-*/()".indexOf(t);
  7. }
  8. function l(t) {
  9. switch (t) {
  10. case "+":
  11. case "-":
  12. return 1;
  13. case "*":
  14. case "/":
  15. return 2;
  16. default:
  17. return 0;
  18. }
  19. }
  20. Object.defineProperty(i, "__esModule", {value: !0}),
  21. (i.MathUtils = i.Dir = void 0),
  22. ((e = a = i.Dir || (i.Dir = {}))[(e.Top = 0)] = "Top"),
  23. (e[(e.TopRight = 1)] = "TopRight"),
  24. (e[(e.Right = 2)] = "Right"),
  25. (e[(e.BottomRight = 3)] = "BottomRight"),
  26. (e[(e.Bottom = 4)] = "Bottom"),
  27. (e[(e.BottomLeft = 5)] = "BottomLeft"),
  28. (e[(e.Left = 6)] = "Left"),
  29. (e[(e.TopLeft = 7)] = "TopLeft"),
  30. (i.MathUtils = class {
  31. getAngle(t) {
  32. return (180 * t) / Math.PI;
  33. }
  34. getRadian(t) {
  35. return (t / 180) * Math.PI;
  36. }
  37. getRadian2(t, e, i, s) {
  38. return Math.atan2(s - e, i - t);
  39. }
  40. getDistance(t, e, i, s) {
  41. (t = i - t), (e = s - e);
  42. return Math.sqrt(t * t + e * e);
  43. }
  44. computeGameObjDir(t, e, i, s) {
  45. var o,
  46. s = this.getRadian2(t, e, i, s),
  47. s = this.getAngle(s);
  48. return (
  49. 0 == s
  50. ? (o = a.Right)
  51. : 90 == s
  52. ? (o = a.Bottom)
  53. : 180 == s
  54. ? (o = a.Left)
  55. : -90 == s
  56. ? (o = a.Top)
  57. : 0 < s && s < 90
  58. ? (o = a.BottomRight)
  59. : 90 < s && s < 180
  60. ? (o = a.BottomLeft)
  61. : -180 < s && s < -90
  62. ? (o = a.TopLeft)
  63. : -90 < s && s < 0 && (o = a.TopRight),
  64. o
  65. );
  66. }
  67. evalRpn(t) {
  68. let e = (function (t) {
  69. for (var e, i = t.match(/(\(|\)|\*|\+|\-|\/|\d+\.?\d*)/g), s = [], o = []; 0 < i.length; ) {
  70. var a = i.shift();
  71. if (r(a)) {
  72. if ("(" == a) s.push(a);
  73. else if (")" == a) {
  74. for (var n = s.pop(); "(" != n && 0 < s.length; ) o.push(n), (n = s.pop());
  75. if ("(" != n) throw "error: unmatched ()";
  76. } else {
  77. for (; (e = s[s.length - 1]), l(a) <= l(e) && 0 < s.length; ) o.push(s.pop());
  78. s.push(a);
  79. }
  80. } else o.push(parseFloat(a));
  81. }
  82. if (0 < s.length) {
  83. if (")" == s[s.length - 1] || "(" == s[s.length - 1]) throw "error: unmatched ()";
  84. for (; 0 < s.length; ) o.push(s.pop());
  85. }
  86. return o;
  87. })(t);
  88. for (var i = []; 0 < e.length; ) {
  89. var s = e.shift();
  90. if (r(s)) {
  91. if (i.length < 2) throw "unvalid stack length";
  92. var o = i.pop(),
  93. a = i.pop();
  94. i.push(
  95. (function (t, e, i) {
  96. switch (i) {
  97. case "+":
  98. return t + e;
  99. case "-":
  100. return t - e;
  101. case "*":
  102. return t * e;
  103. case "/":
  104. return t / e;
  105. }
  106. })(a, o, s)
  107. );
  108. } else i.push(s);
  109. }
  110. if (1 != i.length) throw "unvalid expression";
  111. return i[0];
  112. }
  113. });