a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (function (factory) {
  2. if (typeof module === "object" && typeof module.exports === "object") {
  3. var v = factory(require, exports);
  4. if (v !== undefined) module.exports = v;
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. define(["require", "exports"], factory);
  8. }
  9. })(function (require, exports) {
  10. "use strict";
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var Simulate = (function () {
  13. function Simulate() {
  14. this.index = 0;
  15. this.points = [];
  16. this.timedelta = 1 / 60;
  17. }
  18. Simulate.from = function (x, y) {
  19. var s = new Simulate();
  20. return s.start(x, y);
  21. };
  22. Simulate.prototype.reset = function () {
  23. this.index = 0;
  24. return this;
  25. };
  26. Simulate.prototype.start = function (x, y) {
  27. this.points = [];
  28. return this.to(x, y);
  29. };
  30. Simulate.prototype.to = function (x, y) {
  31. this.newPoint(parseCoordinates(x, y), 1);
  32. return this;
  33. };
  34. Simulate.prototype.delta = function (x, y) {
  35. var newPoint = parseCoordinates(x, y);
  36. var prevCoord = this.getLastPoint().coord;
  37. newPoint.x += prevCoord.x;
  38. newPoint.y += prevCoord.y;
  39. this.newPoint(newPoint, 1);
  40. return this;
  41. };
  42. Simulate.prototype.deltaPolar = function (angle, distance) {
  43. angle *= Math.PI / 180;
  44. var prevCoord = this.getLastPoint().coord;
  45. var coord = {
  46. x: prevCoord.x + (Math.cos(angle) * distance),
  47. y: prevCoord.y + (Math.sin(angle) * distance)
  48. };
  49. this.newPoint(coord, 1);
  50. return this;
  51. };
  52. Simulate.prototype.toPolar = function (angle, distance) {
  53. angle *= Math.PI / 180;
  54. var coord = {
  55. x: Math.cos(angle) * distance,
  56. y: Math.sin(angle) * distance
  57. };
  58. this.newPoint(coord, 1);
  59. return this;
  60. };
  61. Simulate.prototype.duration = function (duration) {
  62. this.getLastPoint().duration = duration;
  63. return this;
  64. };
  65. Simulate.prototype.velocity = function (vel) {
  66. var p1 = this.getLastPoint();
  67. var p2 = this.getPreviousPoint();
  68. var d = distance(p1.coord, p2.coord);
  69. return this.duration(d / vel);
  70. };
  71. Simulate.prototype.swipeRight = function (maxAngle, distance) {
  72. // x------>
  73. var angle = randomAngle(maxAngle);
  74. return this.deltaPolar(angle, distance);
  75. };
  76. Simulate.prototype.swipeLeft = function (maxAngle, distance) {
  77. // <------x
  78. var angle = randomAngle(maxAngle) + 180;
  79. return this.deltaPolar(angle, distance);
  80. };
  81. Simulate.prototype.swipeTop = function (maxAngle, distance) {
  82. var angle = randomAngle(maxAngle) + 90;
  83. return this.deltaPolar(angle, distance);
  84. };
  85. Simulate.prototype.swipeBottom = function (maxAngle, distance) {
  86. var angle = randomAngle(maxAngle) - 90;
  87. return this.deltaPolar(angle, distance);
  88. };
  89. Simulate.prototype.run = function (callback) {
  90. var points = this.points;
  91. var len = points.length - 1;
  92. var i = 0;
  93. for (; i < len; i++) {
  94. var p1 = points[i].coord;
  95. var p2 = points[i + 1].coord;
  96. var duration = points[i + 1].duration;
  97. var vectorX = p2.x - p1.x;
  98. var vectorY = p2.y - p1.y;
  99. var nuSteps = Math.ceil(duration / this.timedelta);
  100. vectorX /= nuSteps;
  101. vectorY /= nuSteps;
  102. for (var j = 0; j < nuSteps; j++) {
  103. callback({
  104. x: p1.x + vectorX * j,
  105. y: p1.y + vectorY * j
  106. });
  107. }
  108. }
  109. this.index = i;
  110. return this;
  111. };
  112. Simulate.prototype.newPoint = function (coord, duration) {
  113. this.points.push({
  114. coord: coord,
  115. duration: duration,
  116. });
  117. };
  118. Simulate.prototype.getLastPoint = function () {
  119. var len = this.points.length;
  120. if (len > 0) {
  121. return this.points[len - 1];
  122. }
  123. throw new Error('can not call point');
  124. };
  125. Simulate.prototype.getPreviousPoint = function () {
  126. var len = this.points.length;
  127. if (len > 1) {
  128. return this.points[len - 2];
  129. }
  130. throw new Error('can not call point');
  131. };
  132. return Simulate;
  133. }());
  134. exports.Simulate = Simulate;
  135. function randomAngle(maxAngle) {
  136. return (Math.random() * maxAngle * 2) - maxAngle;
  137. }
  138. function distance(a, b) {
  139. var deltaX = a.x - b.x;
  140. var deltaY = a.y - a.y;
  141. return Math.hypot(deltaX, deltaY);
  142. }
  143. function parseCoordinates(coord, y) {
  144. if (typeof coord === 'number') {
  145. return { x: coord, y: y };
  146. }
  147. return coord;
  148. }
  149. });
  150. //# sourceMappingURL=simulator.js.map