vec2f.js 13 KB

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