vec2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview Definition of 2 element vectors. This follows the same design
  8. * patterns as Vec3 and Vec4.
  9. */
  10. goog.provide('goog.vec.Vec2');
  11. /** @suppress {extraRequire} */
  12. goog.require('goog.vec');
  13. /** @typedef {!goog.vec.Float32} */ goog.vec.Vec2.Float32;
  14. /** @typedef {!goog.vec.Float64} */ goog.vec.Vec2.Float64;
  15. /** @typedef {!goog.vec.Number} */ goog.vec.Vec2.Number;
  16. /** @typedef {!goog.vec.AnyType} */ goog.vec.Vec2.AnyType;
  17. /**
  18. * Creates a 2 element vector of Float32. The array is initialized to zero.
  19. *
  20. * @return {!goog.vec.Vec2.Float32} The new 2 element array.
  21. */
  22. goog.vec.Vec2.createFloat32 = function() {
  23. return new Float32Array(2);
  24. };
  25. /**
  26. * Creates a 2 element vector of Float64. The array is initialized to zero.
  27. *
  28. * @return {!goog.vec.Vec2.Float64} The new 2 element array.
  29. */
  30. goog.vec.Vec2.createFloat64 = function() {
  31. return new Float64Array(2);
  32. };
  33. /**
  34. * Creates a 2 element vector of Number. The array is initialized to zero.
  35. *
  36. * @return {!goog.vec.Vec2.Number} The new 2 element array.
  37. */
  38. goog.vec.Vec2.createNumber = function() {
  39. var a = new Array(2);
  40. goog.vec.Vec2.setFromValues(a, 0, 0);
  41. return a;
  42. };
  43. /**
  44. * Creates a new 2 element FLoat32 vector initialized with the value from the
  45. * given array.
  46. *
  47. * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
  48. * @return {!goog.vec.Vec2.Float32} The new 2 element array.
  49. */
  50. goog.vec.Vec2.createFloat32FromArray = function(vec) {
  51. var newVec = goog.vec.Vec2.createFloat32();
  52. goog.vec.Vec2.setFromArray(newVec, vec);
  53. return newVec;
  54. };
  55. /**
  56. * Creates a new 2 element Float32 vector initialized with the supplied values.
  57. *
  58. * @param {number} vec0 The value for element at index 0.
  59. * @param {number} vec1 The value for element at index 1.
  60. * @return {!goog.vec.Vec2.Float32} The new vector.
  61. */
  62. goog.vec.Vec2.createFloat32FromValues = function(vec0, vec1) {
  63. var a = goog.vec.Vec2.createFloat32();
  64. goog.vec.Vec2.setFromValues(a, vec0, vec1);
  65. return a;
  66. };
  67. /**
  68. * Creates a clone of the given 2 element Float32 vector.
  69. *
  70. * @param {goog.vec.Vec2.Float32} vec The source 2 element vector.
  71. * @return {!goog.vec.Vec2.Float32} The new cloned vector.
  72. */
  73. goog.vec.Vec2.cloneFloat32 = goog.vec.Vec2.createFloat32FromArray;
  74. /**
  75. * Creates a new 2 element Float64 vector initialized with the value from the
  76. * given array.
  77. *
  78. * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
  79. * @return {!goog.vec.Vec2.Float64} The new 2 element array.
  80. */
  81. goog.vec.Vec2.createFloat64FromArray = function(vec) {
  82. var newVec = goog.vec.Vec2.createFloat64();
  83. goog.vec.Vec2.setFromArray(newVec, vec);
  84. return newVec;
  85. };
  86. /**
  87. * Creates a new 2 element Float64 vector initialized with the supplied values.
  88. *
  89. * @param {number} vec0 The value for element at index 0.
  90. * @param {number} vec1 The value for element at index 1.
  91. * @return {!goog.vec.Vec2.Float64} The new vector.
  92. */
  93. goog.vec.Vec2.createFloat64FromValues = function(vec0, vec1) {
  94. var vec = goog.vec.Vec2.createFloat64();
  95. goog.vec.Vec2.setFromValues(vec, vec0, vec1);
  96. return vec;
  97. };
  98. /**
  99. * Creates a clone of the given 2 element vector.
  100. *
  101. * @param {goog.vec.Vec2.Float64} vec The source 2 element vector.
  102. * @return {!goog.vec.Vec2.Float64} The new cloned vector.
  103. */
  104. goog.vec.Vec2.cloneFloat64 = goog.vec.Vec2.createFloat64FromArray;
  105. /**
  106. * Initializes the vector with the given values.
  107. *
  108. * @param {goog.vec.Vec2.AnyType} vec The vector to receive the values.
  109. * @param {number} vec0 The value for element at index 0.
  110. * @param {number} vec1 The value for element at index 1.
  111. * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
  112. * chained together.
  113. */
  114. goog.vec.Vec2.setFromValues = function(vec, vec0, vec1) {
  115. vec[0] = vec0;
  116. vec[1] = vec1;
  117. return vec;
  118. };
  119. /**
  120. * Initializes the vector with the given array of values.
  121. *
  122. * @param {goog.vec.Vec2.AnyType} vec The vector to receive the
  123. * values.
  124. * @param {goog.vec.Vec2.AnyType} values The array of values.
  125. * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
  126. * chained together.
  127. */
  128. goog.vec.Vec2.setFromArray = function(vec, values) {
  129. vec[0] = values[0];
  130. vec[1] = values[1];
  131. return vec;
  132. };
  133. /**
  134. * Performs a component-wise addition of vec0 and vec1 together storing the
  135. * result into resultVec.
  136. *
  137. * @param {goog.vec.Vec2.AnyType} vec0 The first addend.
  138. * @param {goog.vec.Vec2.AnyType} vec1 The second addend.
  139. * @param {goog.vec.Vec2.AnyType} resultVec The vector to
  140. * receive the result. May be vec0 or vec1.
  141. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  142. * chained together.
  143. */
  144. goog.vec.Vec2.add = function(vec0, vec1, resultVec) {
  145. resultVec[0] = vec0[0] + vec1[0];
  146. resultVec[1] = vec0[1] + vec1[1];
  147. return resultVec;
  148. };
  149. /**
  150. * Performs a component-wise subtraction of vec1 from vec0 storing the
  151. * result into resultVec.
  152. *
  153. * @param {goog.vec.Vec2.AnyType} vec0 The minuend.
  154. * @param {goog.vec.Vec2.AnyType} vec1 The subtrahend.
  155. * @param {goog.vec.Vec2.AnyType} resultVec The vector to
  156. * receive the result. May be vec0 or vec1.
  157. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  158. * chained together.
  159. */
  160. goog.vec.Vec2.subtract = function(vec0, vec1, resultVec) {
  161. resultVec[0] = vec0[0] - vec1[0];
  162. resultVec[1] = vec0[1] - vec1[1];
  163. return resultVec;
  164. };
  165. /**
  166. * Negates vec0, storing the result into resultVec.
  167. *
  168. * @param {goog.vec.Vec2.AnyType} vec0 The vector to negate.
  169. * @param {goog.vec.Vec2.AnyType} resultVec The vector to
  170. * receive the result. May be vec0.
  171. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  172. * chained together.
  173. */
  174. goog.vec.Vec2.negate = function(vec0, resultVec) {
  175. resultVec[0] = -vec0[0];
  176. resultVec[1] = -vec0[1];
  177. return resultVec;
  178. };
  179. /**
  180. * Takes the absolute value of each component of vec0 storing the result in
  181. * resultVec.
  182. *
  183. * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
  184. * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the result.
  185. * May be vec0.
  186. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  187. * chained together.
  188. */
  189. goog.vec.Vec2.abs = function(vec0, resultVec) {
  190. resultVec[0] = Math.abs(vec0[0]);
  191. resultVec[1] = Math.abs(vec0[1]);
  192. return resultVec;
  193. };
  194. /**
  195. * Multiplies each component of vec0 with scalar storing the product into
  196. * resultVec.
  197. *
  198. * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
  199. * @param {number} scalar The value to multiply with each component of vec0.
  200. * @param {goog.vec.Vec2.AnyType} resultVec The vector to
  201. * receive the result. May be vec0.
  202. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  203. * chained together.
  204. */
  205. goog.vec.Vec2.scale = function(vec0, scalar, resultVec) {
  206. resultVec[0] = vec0[0] * scalar;
  207. resultVec[1] = vec0[1] * scalar;
  208. return resultVec;
  209. };
  210. /**
  211. * Returns the magnitudeSquared of the given vector.
  212. *
  213. * @param {goog.vec.Vec2.AnyType} vec0 The vector.
  214. * @return {number} The magnitude of the vector.
  215. */
  216. goog.vec.Vec2.magnitudeSquared = function(vec0) {
  217. var x = vec0[0], y = vec0[1];
  218. return x * x + y * y;
  219. };
  220. /**
  221. * Returns the magnitude of the given vector.
  222. *
  223. * @param {goog.vec.Vec2.AnyType} vec0 The vector.
  224. * @return {number} The magnitude of the vector.
  225. */
  226. goog.vec.Vec2.magnitude = function(vec0) {
  227. var x = vec0[0], y = vec0[1];
  228. return Math.sqrt(x * x + y * y);
  229. };
  230. /**
  231. * Normalizes the given vector storing the result into resultVec.
  232. *
  233. * @param {goog.vec.Vec2.AnyType} vec0 The vector to normalize.
  234. * @param {goog.vec.Vec2.AnyType} resultVec The vector to
  235. * receive the result. May be vec0.
  236. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  237. * chained together.
  238. */
  239. goog.vec.Vec2.normalize = function(vec0, resultVec) {
  240. var ilen = 1 / goog.vec.Vec2.magnitude(vec0);
  241. resultVec[0] = vec0[0] * ilen;
  242. resultVec[1] = vec0[1] * ilen;
  243. return resultVec;
  244. };
  245. /**
  246. * Returns the scalar product of vectors vec0 and vec1.
  247. *
  248. * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
  249. * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
  250. * @return {number} The scalar product.
  251. */
  252. goog.vec.Vec2.dot = function(vec0, vec1) {
  253. return vec0[0] * vec1[0] + vec0[1] * vec1[1];
  254. };
  255. /**
  256. * Returns the squared distance between two points.
  257. *
  258. * @param {goog.vec.Vec2.AnyType} vec0 First point.
  259. * @param {goog.vec.Vec2.AnyType} vec1 Second point.
  260. * @return {number} The squared distance between the points.
  261. */
  262. goog.vec.Vec2.distanceSquared = function(vec0, vec1) {
  263. var x = vec0[0] - vec1[0];
  264. var y = vec0[1] - vec1[1];
  265. return x * x + y * y;
  266. };
  267. /**
  268. * Returns the distance between two points.
  269. *
  270. * @param {goog.vec.Vec2.AnyType} vec0 First point.
  271. * @param {goog.vec.Vec2.AnyType} vec1 Second point.
  272. * @return {number} The distance between the points.
  273. */
  274. goog.vec.Vec2.distance = function(vec0, vec1) {
  275. return Math.sqrt(goog.vec.Vec2.distanceSquared(vec0, vec1));
  276. };
  277. /**
  278. * Returns a unit vector pointing from one point to another.
  279. * If the input points are equal then the result will be all zeros.
  280. *
  281. * @param {goog.vec.Vec2.AnyType} vec0 Origin point.
  282. * @param {goog.vec.Vec2.AnyType} vec1 Target point.
  283. * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
  284. * results (may be vec0 or vec1).
  285. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  286. * chained together.
  287. */
  288. goog.vec.Vec2.direction = function(vec0, vec1, resultVec) {
  289. var x = vec1[0] - vec0[0];
  290. var y = vec1[1] - vec0[1];
  291. var d = Math.sqrt(x * x + y * y);
  292. if (d) {
  293. d = 1 / d;
  294. resultVec[0] = x * d;
  295. resultVec[1] = y * d;
  296. } else {
  297. resultVec[0] = resultVec[1] = 0;
  298. }
  299. return resultVec;
  300. };
  301. /**
  302. * Linearly interpolate from vec0 to vec1 according to f. The value of f should
  303. * be in the range [0..1] otherwise the results are undefined.
  304. *
  305. * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
  306. * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
  307. * @param {number} f The interpolation factor.
  308. * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
  309. * results (may be vec0 or vec1).
  310. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  311. * chained together.
  312. */
  313. goog.vec.Vec2.lerp = function(vec0, vec1, f, resultVec) {
  314. var x = vec0[0], y = vec0[1];
  315. resultVec[0] = (vec1[0] - x) * f + x;
  316. resultVec[1] = (vec1[1] - y) * f + y;
  317. return resultVec;
  318. };
  319. /**
  320. * Compares the components of vec0 with the components of another vector or
  321. * scalar, storing the larger values in resultVec.
  322. *
  323. * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
  324. * @param {goog.vec.Vec2.AnyType|number} limit The limit vector or scalar.
  325. * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
  326. * results (may be vec0 or limit).
  327. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  328. * chained together.
  329. */
  330. goog.vec.Vec2.max = function(vec0, limit, resultVec) {
  331. if (typeof limit === 'number') {
  332. resultVec[0] = Math.max(vec0[0], limit);
  333. resultVec[1] = Math.max(vec0[1], limit);
  334. } else {
  335. resultVec[0] = Math.max(vec0[0], limit[0]);
  336. resultVec[1] = Math.max(vec0[1], limit[1]);
  337. }
  338. return resultVec;
  339. };
  340. /**
  341. * Compares the components of vec0 with the components of another vector or
  342. * scalar, storing the smaller values in resultVec.
  343. *
  344. * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
  345. * @param {goog.vec.Vec2.AnyType|number} limit The limit vector or scalar.
  346. * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
  347. * results (may be vec0 or limit).
  348. * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
  349. * chained together.
  350. */
  351. goog.vec.Vec2.min = function(vec0, limit, resultVec) {
  352. if (typeof limit === 'number') {
  353. resultVec[0] = Math.min(vec0[0], limit);
  354. resultVec[1] = Math.min(vec0[1], limit);
  355. } else {
  356. resultVec[0] = Math.min(vec0[0], limit[0]);
  357. resultVec[1] = Math.min(vec0[1], limit[1]);
  358. }
  359. return resultVec;
  360. };
  361. /**
  362. * Returns true if the components of vec0 are equal to the components of vec1.
  363. *
  364. * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
  365. * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
  366. * @return {boolean} True if the vectors are equal, false otherwise.
  367. */
  368. goog.vec.Vec2.equals = function(vec0, vec1) {
  369. return vec0.length == vec1.length && vec0[0] == vec1[0] && vec0[1] == vec1[1];
  370. };