Vector2.js 4.4 KB

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