vec2f.js 14 KB

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