vec2.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2007 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 Defines a 2-element vector class that can be used for
  16. * coordinate math, useful for animation systems and point manipulation.
  17. *
  18. * Vec2 objects inherit from goog.math.Coordinate and may be used wherever a
  19. * Coordinate is required. Where appropriate, Vec2 functions accept both Vec2
  20. * and Coordinate objects as input.
  21. *
  22. * @author brenneman@google.com (Shawn Brenneman)
  23. */
  24. goog.provide('goog.math.Vec2');
  25. goog.require('goog.math');
  26. goog.require('goog.math.Coordinate');
  27. /**
  28. * Class for a two-dimensional vector object and assorted functions useful for
  29. * manipulating points.
  30. *
  31. * @param {number} x The x coordinate for the vector.
  32. * @param {number} y The y coordinate for the vector.
  33. * @struct
  34. * @constructor
  35. * @extends {goog.math.Coordinate}
  36. */
  37. goog.math.Vec2 = function(x, y) {
  38. /**
  39. * X-value
  40. * @type {number}
  41. */
  42. this.x = x;
  43. /**
  44. * Y-value
  45. * @type {number}
  46. */
  47. this.y = y;
  48. };
  49. goog.inherits(goog.math.Vec2, goog.math.Coordinate);
  50. /**
  51. * @return {!goog.math.Vec2} A random unit-length vector.
  52. */
  53. goog.math.Vec2.randomUnit = function() {
  54. var angle = Math.random() * Math.PI * 2;
  55. return new goog.math.Vec2(Math.cos(angle), Math.sin(angle));
  56. };
  57. /**
  58. * @return {!goog.math.Vec2} A random vector inside the unit-disc.
  59. */
  60. goog.math.Vec2.random = function() {
  61. var mag = Math.sqrt(Math.random());
  62. var angle = Math.random() * Math.PI * 2;
  63. return new goog.math.Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
  64. };
  65. /**
  66. * Returns a new Vec2 object from a given coordinate.
  67. * @param {!goog.math.Coordinate} a The coordinate.
  68. * @return {!goog.math.Vec2} A new vector object.
  69. */
  70. goog.math.Vec2.fromCoordinate = function(a) {
  71. return new goog.math.Vec2(a.x, a.y);
  72. };
  73. /**
  74. * @return {!goog.math.Vec2} A new vector with the same coordinates as this one.
  75. * @override
  76. */
  77. goog.math.Vec2.prototype.clone = function() {
  78. return new goog.math.Vec2(this.x, this.y);
  79. };
  80. /**
  81. * Returns the magnitude of the vector measured from the origin.
  82. * @return {number} The length of the vector.
  83. */
  84. goog.math.Vec2.prototype.magnitude = function() {
  85. return Math.sqrt(this.x * this.x + this.y * this.y);
  86. };
  87. /**
  88. * Returns the squared magnitude of the vector measured from the origin.
  89. * NOTE(brenneman): Leaving out the square root is not a significant
  90. * optimization in JavaScript.
  91. * @return {number} The length of the vector, squared.
  92. */
  93. goog.math.Vec2.prototype.squaredMagnitude = function() {
  94. return this.x * this.x + this.y * this.y;
  95. };
  96. /**
  97. * @return {!goog.math.Vec2} This coordinate after scaling.
  98. * @override
  99. */
  100. goog.math.Vec2.prototype.scale =
  101. /** @type {function(number, number=):!goog.math.Vec2} */
  102. (goog.math.Coordinate.prototype.scale);
  103. /**
  104. * Reverses the sign of the vector. Equivalent to scaling the vector by -1.
  105. * @return {!goog.math.Vec2} The inverted vector.
  106. */
  107. goog.math.Vec2.prototype.invert = function() {
  108. this.x = -this.x;
  109. this.y = -this.y;
  110. return this;
  111. };
  112. /**
  113. * Normalizes the current vector to have a magnitude of 1.
  114. * @return {!goog.math.Vec2} The normalized vector.
  115. */
  116. goog.math.Vec2.prototype.normalize = function() {
  117. return this.scale(1 / this.magnitude());
  118. };
  119. /**
  120. * Adds another vector to this vector in-place.
  121. * @param {!goog.math.Coordinate} b The vector to add.
  122. * @return {!goog.math.Vec2} This vector with {@code b} added.
  123. */
  124. goog.math.Vec2.prototype.add = function(b) {
  125. this.x += b.x;
  126. this.y += b.y;
  127. return this;
  128. };
  129. /**
  130. * Subtracts another vector from this vector in-place.
  131. * @param {!goog.math.Coordinate} b The vector to subtract.
  132. * @return {!goog.math.Vec2} This vector with {@code b} subtracted.
  133. */
  134. goog.math.Vec2.prototype.subtract = function(b) {
  135. this.x -= b.x;
  136. this.y -= b.y;
  137. return this;
  138. };
  139. /**
  140. * Rotates this vector in-place by a given angle, specified in radians.
  141. * @param {number} angle The angle, in radians.
  142. * @return {!goog.math.Vec2} This vector rotated {@code angle} radians.
  143. */
  144. goog.math.Vec2.prototype.rotate = function(angle) {
  145. var cos = Math.cos(angle);
  146. var sin = Math.sin(angle);
  147. var newX = this.x * cos - this.y * sin;
  148. var newY = this.y * cos + this.x * sin;
  149. this.x = newX;
  150. this.y = newY;
  151. return this;
  152. };
  153. /**
  154. * Rotates a vector by a given angle, specified in radians, relative to a given
  155. * axis rotation point. The returned vector is a newly created instance - no
  156. * in-place changes are done.
  157. * @param {!goog.math.Vec2} v A vector.
  158. * @param {!goog.math.Vec2} axisPoint The rotation axis point.
  159. * @param {number} angle The angle, in radians.
  160. * @return {!goog.math.Vec2} The rotated vector in a newly created instance.
  161. */
  162. goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) {
  163. var res = v.clone();
  164. return res.subtract(axisPoint).rotate(angle).add(axisPoint);
  165. };
  166. /** @override */
  167. goog.math.Vec2.prototype.equals = function(b) {
  168. if (this == b) {
  169. return true;
  170. }
  171. return b instanceof goog.math.Vec2 && !!b && this.x == b.x && this.y == b.y;
  172. };
  173. /**
  174. * Returns the distance between two vectors.
  175. * @param {!goog.math.Coordinate} a The first vector.
  176. * @param {!goog.math.Coordinate} b The second vector.
  177. * @return {number} The distance.
  178. */
  179. goog.math.Vec2.distance = goog.math.Coordinate.distance;
  180. /**
  181. * Returns the squared distance between two vectors.
  182. * @param {!goog.math.Coordinate} a The first vector.
  183. * @param {!goog.math.Coordinate} b The second vector.
  184. * @return {number} The squared distance.
  185. */
  186. goog.math.Vec2.squaredDistance = goog.math.Coordinate.squaredDistance;
  187. /**
  188. * Compares vectors for equality.
  189. * @param {!goog.math.Coordinate} a The first vector.
  190. * @param {!goog.math.Coordinate} b The second vector.
  191. * @return {boolean} Whether the vectors have the same x and y coordinates.
  192. */
  193. goog.math.Vec2.equals = goog.math.Coordinate.equals;
  194. /**
  195. * Returns the sum of two vectors as a new Vec2.
  196. * @param {!goog.math.Coordinate} a The first vector.
  197. * @param {!goog.math.Coordinate} b The second vector.
  198. * @return {!goog.math.Vec2} The sum vector.
  199. */
  200. goog.math.Vec2.sum = function(a, b) {
  201. return new goog.math.Vec2(a.x + b.x, a.y + b.y);
  202. };
  203. /**
  204. * Returns the difference between two vectors as a new Vec2.
  205. * @param {!goog.math.Coordinate} a The first vector.
  206. * @param {!goog.math.Coordinate} b The second vector.
  207. * @return {!goog.math.Vec2} The difference vector.
  208. */
  209. goog.math.Vec2.difference = function(a, b) {
  210. return new goog.math.Vec2(a.x - b.x, a.y - b.y);
  211. };
  212. /**
  213. * Returns the dot-product of two vectors.
  214. * @param {!goog.math.Coordinate} a The first vector.
  215. * @param {!goog.math.Coordinate} b The second vector.
  216. * @return {number} The dot-product of the two vectors.
  217. */
  218. goog.math.Vec2.dot = function(a, b) {
  219. return a.x * b.x + a.y * b.y;
  220. };
  221. /**
  222. * Returns the determinant of two vectors.
  223. * @param {!goog.math.Vec2} a The first vector.
  224. * @param {!goog.math.Vec2} b The second vector.
  225. * @return {number} The determinant of the two vectors.
  226. */
  227. goog.math.Vec2.determinant = function(a, b) {
  228. return a.x * b.y - a.y * b.x;
  229. };
  230. /**
  231. * Returns a new Vec2 that is the linear interpolant between vectors a and b at
  232. * scale-value x.
  233. * @param {!goog.math.Coordinate} a Vector a.
  234. * @param {!goog.math.Coordinate} b Vector b.
  235. * @param {number} x The proportion between a and b.
  236. * @return {!goog.math.Vec2} The interpolated vector.
  237. */
  238. goog.math.Vec2.lerp = function(a, b, x) {
  239. return new goog.math.Vec2(
  240. goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x));
  241. };