vec2.js 13 KB

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