Vector2.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. "use strict";
  2. function Vector2(x, y)
  3. {
  4. this.x = x || 0;
  5. this.y = y || 0;
  6. }
  7. Object.assign(Vector2.prototype,
  8. {
  9. set: function(x, y)
  10. {
  11. this.x = x;
  12. this.y = y;
  13. },
  14. setScalar: function(scalar)
  15. {
  16. this.x = scalar;
  17. this.y = scalar;
  18. },
  19. clone: function()
  20. {
  21. return new Vector2(this.x, this.y);
  22. },
  23. copy: function(v)
  24. {
  25. this.x = v.x;
  26. this.y = v.y;
  27. },
  28. add: function(v, w)
  29. {
  30. this.x += v.x;
  31. this.y += v.y;
  32. },
  33. addScalar: function(s)
  34. {
  35. this.x += s;
  36. this.y += s;
  37. },
  38. addVectors: function(a, b)
  39. {
  40. this.x = a.x + b.x;
  41. this.y = a.y + b.y;
  42. },
  43. addScaledVector: function(v, s)
  44. {
  45. this.x += v.x * s;
  46. this.y += v.y * s;
  47. },
  48. sub: function(v, w)
  49. {
  50. this.x -= v.x;
  51. this.y -= v.y;
  52. },
  53. subScalar: function(s)
  54. {
  55. this.x -= s;
  56. this.y -= s;
  57. },
  58. subVectors: function(a, b)
  59. {
  60. this.x = a.x - b.x;
  61. this.y = a.y - b.y;
  62. },
  63. multiply: function(v)
  64. {
  65. this.x *= v.x;
  66. this.y *= v.y;
  67. },
  68. multiplyScalar: function(scalar)
  69. {
  70. this.x *= scalar;
  71. this.y *= scalar;
  72. },
  73. divide: function(v)
  74. {
  75. this.x /= v.x;
  76. this.y /= v.y;
  77. },
  78. divideScalar: function(scalar)
  79. {
  80. return this.multiplyScalar(1 / scalar);
  81. },
  82. min: function(v)
  83. {
  84. this.x = Math.min(this.x, v.x);
  85. this.y = Math.min(this.y, v.y);
  86. },
  87. max: function(v)
  88. {
  89. this.x = Math.max(this.x, v.x);
  90. this.y = Math.max(this.y, v.y);
  91. },
  92. clamp: function(min, max)
  93. {
  94. // assumes min < max, componentwise
  95. this.x = Math.max(min.x, Math.min(max.x, this.x));
  96. this.y = Math.max(min.y, Math.min(max.y, this.y));
  97. },
  98. clampScalar: function(minVal, maxVal)
  99. {
  100. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  101. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  102. },
  103. clampLength: function(min, max)
  104. {
  105. var length = this.length();
  106. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  107. },
  108. floor: function()
  109. {
  110. this.x = Math.floor(this.x);
  111. this.y = Math.floor(this.y);
  112. },
  113. ceil: function()
  114. {
  115. this.x = Math.ceil(this.x);
  116. this.y = Math.ceil(this.y);
  117. },
  118. round: function()
  119. {
  120. this.x = Math.round(this.x);
  121. this.y = Math.round(this.y);
  122. },
  123. roundToZero: function()
  124. {
  125. this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
  126. this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
  127. },
  128. negate: function()
  129. {
  130. this.x = -this.x;
  131. this.y = -this.y;
  132. return this;
  133. },
  134. dot: function(v)
  135. {
  136. return this.x * v.x + this.y * v.y;
  137. },
  138. cross: function(v)
  139. {
  140. return this.x * v.y - this.y * v.x;
  141. },
  142. lengthSq: function()
  143. {
  144. return this.x * this.x + this.y * this.y;
  145. },
  146. length: function()
  147. {
  148. return Math.sqrt(this.x * this.x + this.y * this.y);
  149. },
  150. manhattanLength: function()
  151. {
  152. return Math.abs(this.x) + Math.abs(this.y);
  153. },
  154. normalize: function()
  155. {
  156. return this.divideScalar(this.length() || 1);
  157. },
  158. /**
  159. * Computes the angle in radians with respect to the positive x-axis
  160. */
  161. angle: function()
  162. {
  163. var angle = Math.atan2(this.y, this.x);
  164. if(angle < 0) angle += 2 * Math.PI;
  165. return angle;
  166. },
  167. distanceTo: function(v)
  168. {
  169. return Math.sqrt(this.distanceToSquared(v));
  170. },
  171. distanceToSquared: function(v)
  172. {
  173. var dx = this.x - v.x,
  174. dy = this.y - v.y;
  175. return dx * dx + dy * dy;
  176. },
  177. manhattanDistanceTo: function(v)
  178. {
  179. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  180. },
  181. setLength: function(length)
  182. {
  183. return this.normalize().multiplyScalar(length);
  184. },
  185. lerp: function(v, alpha)
  186. {
  187. this.x += (v.x - this.x) * alpha;
  188. this.y += (v.y - this.y) * alpha;
  189. },
  190. lerpVectors: function(v1, v2, alpha)
  191. {
  192. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  193. },
  194. equals: function(v)
  195. {
  196. return ((v.x === this.x) && (v.y === this.y));
  197. },
  198. fromArray: function(array, offset)
  199. {
  200. if(offset === undefined) offset = 0;
  201. this.x = array[offset];
  202. this.y = array[offset + 1];
  203. },
  204. toArray: function(array, offset)
  205. {
  206. if(array === undefined) array = [];
  207. if(offset === undefined) offset = 0;
  208. array[offset] = this.x;
  209. array[offset + 1] = this.y;
  210. return array;
  211. },
  212. rotateAround: function(center, angle)
  213. {
  214. var c = Math.cos(angle),
  215. s = Math.sin(angle);
  216. var x = this.x - center.x;
  217. var y = this.y - center.y;
  218. this.x = x * c - y * s + center.x;
  219. this.y = x * s + y * c + center.y;
  220. }
  221. });
  222. export {Vector2};