Vector2.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. applyMatrix3: function(m)
  83. {
  84. var x = this.x,
  85. y = this.y;
  86. var e = m.elements;
  87. this.x = e[0] * x + e[3] * y + e[6];
  88. this.y = e[1] * x + e[4] * y + e[7];
  89. },
  90. min: function(v)
  91. {
  92. this.x = Math.min(this.x, v.x);
  93. this.y = Math.min(this.y, v.y);
  94. },
  95. max: function(v)
  96. {
  97. this.x = Math.max(this.x, v.x);
  98. this.y = Math.max(this.y, v.y);
  99. },
  100. clamp: function(min, max)
  101. {
  102. // assumes min < max, componentwise
  103. this.x = Math.max(min.x, Math.min(max.x, this.x));
  104. this.y = Math.max(min.y, Math.min(max.y, this.y));
  105. },
  106. clampScalar: function(minVal, maxVal)
  107. {
  108. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  109. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  110. },
  111. clampLength: function(min, max)
  112. {
  113. var length = this.length();
  114. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  115. },
  116. floor: function()
  117. {
  118. this.x = Math.floor(this.x);
  119. this.y = Math.floor(this.y);
  120. },
  121. ceil: function()
  122. {
  123. this.x = Math.ceil(this.x);
  124. this.y = Math.ceil(this.y);
  125. },
  126. round: function()
  127. {
  128. this.x = Math.round(this.x);
  129. this.y = Math.round(this.y);
  130. },
  131. roundToZero: function()
  132. {
  133. this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
  134. this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
  135. },
  136. negate: function()
  137. {
  138. this.x = -this.x;
  139. this.y = -this.y;
  140. return this;
  141. },
  142. dot: function(v)
  143. {
  144. return this.x * v.x + this.y * v.y;
  145. },
  146. cross: function(v)
  147. {
  148. return this.x * v.y - this.y * v.x;
  149. },
  150. lengthSq: function()
  151. {
  152. return this.x * this.x + this.y * this.y;
  153. },
  154. length: function()
  155. {
  156. return Math.sqrt(this.x * this.x + this.y * this.y);
  157. },
  158. manhattanLength: function()
  159. {
  160. return Math.abs(this.x) + Math.abs(this.y);
  161. },
  162. normalize: function()
  163. {
  164. return this.divideScalar(this.length() || 1);
  165. },
  166. /**
  167. * Computes the angle in radians with respect to the positive x-axis
  168. */
  169. angle: function()
  170. {
  171. var angle = Math.atan2(this.y, this.x);
  172. if(angle < 0) angle += 2 * Math.PI;
  173. return angle;
  174. },
  175. distanceTo: function(v)
  176. {
  177. return Math.sqrt(this.distanceToSquared(v));
  178. },
  179. distanceToSquared: function(v)
  180. {
  181. var dx = this.x - v.x,
  182. dy = this.y - v.y;
  183. return dx * dx + dy * dy;
  184. },
  185. manhattanDistanceTo: function(v)
  186. {
  187. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  188. },
  189. setLength: function(length)
  190. {
  191. return this.normalize().multiplyScalar(length);
  192. },
  193. lerp: function(v, alpha)
  194. {
  195. this.x += (v.x - this.x) * alpha;
  196. this.y += (v.y - this.y) * alpha;
  197. },
  198. lerpVectors: function(v1, v2, alpha)
  199. {
  200. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  201. },
  202. equals: function(v)
  203. {
  204. return ((v.x === this.x) && (v.y === this.y));
  205. },
  206. fromArray: function(array, offset)
  207. {
  208. if(offset === undefined) offset = 0;
  209. this.x = array[offset];
  210. this.y = array[offset + 1];
  211. },
  212. toArray: function(array, offset)
  213. {
  214. if(array === undefined) array = [];
  215. if(offset === undefined) offset = 0;
  216. array[offset] = this.x;
  217. array[offset + 1] = this.y;
  218. return array;
  219. },
  220. rotateAround: function(center, angle)
  221. {
  222. var c = Math.cos(angle),
  223. s = Math.sin(angle);
  224. var x = this.x - center.x;
  225. var y = this.y - center.y;
  226. this.x = x * c - y * s + center.x;
  227. this.y = x * s + y * c + center.y;
  228. }
  229. });
  230. //export {Vector2};