| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 | <!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8">    <title>JSDoc: Source: vec2.js</title>    <script src="scripts/prettify/prettify.js"> </script>    <script src="scripts/prettify/lang-css.js"> </script>    <!--[if lt IE 9]>      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>    <![endif]-->    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"></head><body><div id="main">    <h1 class="page-title">Source: vec2.js</h1>            <section>        <article>            <pre class="prettyprint source linenums"><code>import * as glMatrix from "./common.js";/** * 2 Dimensional Vector * @module vec2 *//** * Creates a new, empty vec2 * * @returns {vec2} a new 2D vector */export function create() {  let out = new glMatrix.ARRAY_TYPE(2);  if(glMatrix.ARRAY_TYPE != Float32Array) {    out[0] = 0;    out[1] = 0;  }  return out;}/** * Creates a new vec2 initialized with values from an existing vector * * @param {vec2} a vector to clone * @returns {vec2} a new 2D vector */export function clone(a) {  let out = new glMatrix.ARRAY_TYPE(2);  out[0] = a[0];  out[1] = a[1];  return out;}/** * Creates a new vec2 initialized with the given values * * @param {Number} x X component * @param {Number} y Y component * @returns {vec2} a new 2D vector */export function fromValues(x, y) {  let out = new glMatrix.ARRAY_TYPE(2);  out[0] = x;  out[1] = y;  return out;}/** * Copy the values from one vec2 to another * * @param {vec2} out the receiving vector * @param {vec2} a the source vector * @returns {vec2} out */export function copy(out, a) {  out[0] = a[0];  out[1] = a[1];  return out;}/** * Set the components of a vec2 to the given values * * @param {vec2} out the receiving vector * @param {Number} x X component * @param {Number} y Y component * @returns {vec2} out */export function set(out, x, y) {  out[0] = x;  out[1] = y;  return out;}/** * Adds two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function add(out, a, b) {  out[0] = a[0] + b[0];  out[1] = a[1] + b[1];  return out;}/** * Subtracts vector b from vector a * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function subtract(out, a, b) {  out[0] = a[0] - b[0];  out[1] = a[1] - b[1];  return out;}/** * Multiplies two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function multiply(out, a, b) {  out[0] = a[0] * b[0];  out[1] = a[1] * b[1];  return out;}/** * Divides two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function divide(out, a, b) {  out[0] = a[0] / b[0];  out[1] = a[1] / b[1];  return out;}/** * Math.ceil the components of a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to ceil * @returns {vec2} out */export function ceil(out, a) {  out[0] = Math.ceil(a[0]);  out[1] = Math.ceil(a[1]);  return out;}/** * Math.floor the components of a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to floor * @returns {vec2} out */export function floor(out, a) {  out[0] = Math.floor(a[0]);  out[1] = Math.floor(a[1]);  return out;}/** * Returns the minimum of two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function min(out, a, b) {  out[0] = Math.min(a[0], b[0]);  out[1] = Math.min(a[1], b[1]);  return out;}/** * Returns the maximum of two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec2} out */export function max(out, a, b) {  out[0] = Math.max(a[0], b[0]);  out[1] = Math.max(a[1], b[1]);  return out;}/** * Math.round the components of a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to round * @returns {vec2} out */export function round (out, a) {  out[0] = Math.round(a[0]);  out[1] = Math.round(a[1]);  return out;}/** * Scales a vec2 by a scalar number * * @param {vec2} out the receiving vector * @param {vec2} a the vector to scale * @param {Number} b amount to scale the vector by * @returns {vec2} out */export function scale(out, a, b) {  out[0] = a[0] * b;  out[1] = a[1] * b;  return out;}/** * Adds two vec2's after scaling the second operand by a scalar value * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @param {Number} scale the amount to scale b by before adding * @returns {vec2} out */export function scaleAndAdd(out, a, b, scale) {  out[0] = a[0] + (b[0] * scale);  out[1] = a[1] + (b[1] * scale);  return out;}/** * Calculates the euclidian distance between two vec2's * * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {Number} distance between a and b */export function distance(a, b) {  var x = b[0] - a[0],    y = b[1] - a[1];  return Math.sqrt(x*x + y*y);}/** * Calculates the squared euclidian distance between two vec2's * * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {Number} squared distance between a and b */export function squaredDistance(a, b) {  var x = b[0] - a[0],    y = b[1] - a[1];  return x*x + y*y;}/** * Calculates the length of a vec2 * * @param {vec2} a vector to calculate length of * @returns {Number} length of a */export function length(a) {  var x = a[0],    y = a[1];  return Math.sqrt(x*x + y*y);}/** * Calculates the squared length of a vec2 * * @param {vec2} a vector to calculate squared length of * @returns {Number} squared length of a */export function squaredLength (a) {  var x = a[0],    y = a[1];  return x*x + y*y;}/** * Negates the components of a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to negate * @returns {vec2} out */export function negate(out, a) {  out[0] = -a[0];  out[1] = -a[1];  return out;}/** * Returns the inverse of the components of a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to invert * @returns {vec2} out */export function inverse(out, a) {  out[0] = 1.0 / a[0];  out[1] = 1.0 / a[1];  return out;}/** * Normalize a vec2 * * @param {vec2} out the receiving vector * @param {vec2} a vector to normalize * @returns {vec2} out */export function normalize(out, a) {  var x = a[0],    y = a[1];  var len = x*x + y*y;  if (len > 0) {    //TODO: evaluate use of glm_invsqrt here?    len = 1 / Math.sqrt(len);    out[0] = a[0] * len;    out[1] = a[1] * len;  }  return out;}/** * Calculates the dot product of two vec2's * * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {Number} dot product of a and b */export function dot(a, b) {  return a[0] * b[0] + a[1] * b[1];}/** * Computes the cross product of two vec2's * Note that the cross product must by definition produce a 3D vector * * @param {vec3} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @returns {vec3} out */export function cross(out, a, b) {  var z = a[0] * b[1] - a[1] * b[0];  out[0] = out[1] = 0;  out[2] = z;  return out;}/** * Performs a linear interpolation between two vec2's * * @param {vec2} out the receiving vector * @param {vec2} a the first operand * @param {vec2} b the second operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @returns {vec2} out */export function lerp(out, a, b, t) {  var ax = a[0],    ay = a[1];  out[0] = ax + t * (b[0] - ax);  out[1] = ay + t * (b[1] - ay);  return out;}/** * Generates a random vector with the given scale * * @param {vec2} out the receiving vector * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned * @returns {vec2} out */export function random(out, scale) {  scale = scale || 1.0;  var r = glMatrix.RANDOM() * 2.0 * Math.PI;  out[0] = Math.cos(r) * scale;  out[1] = Math.sin(r) * scale;  return out;}/** * Transforms the vec2 with a mat2 * * @param {vec2} out the receiving vector * @param {vec2} a the vector to transform * @param {mat2} m matrix to transform with * @returns {vec2} out */export function transformMat2(out, a, m) {  var x = a[0],    y = a[1];  out[0] = m[0] * x + m[2] * y;  out[1] = m[1] * x + m[3] * y;  return out;}/** * Transforms the vec2 with a mat2d * * @param {vec2} out the receiving vector * @param {vec2} a the vector to transform * @param {mat2d} m matrix to transform with * @returns {vec2} out */export function transformMat2d(out, a, m) {  var x = a[0],    y = a[1];  out[0] = m[0] * x + m[2] * y + m[4];  out[1] = m[1] * x + m[3] * y + m[5];  return out;}/** * Transforms the vec2 with a mat3 * 3rd vector component is implicitly '1' * * @param {vec2} out the receiving vector * @param {vec2} a the vector to transform * @param {mat3} m matrix to transform with * @returns {vec2} out */export function transformMat3(out, a, m) {  var x = a[0],    y = a[1];  out[0] = m[0] * x + m[3] * y + m[6];  out[1] = m[1] * x + m[4] * y + m[7];  return out;}/** * Transforms the vec2 with a mat4 * 3rd vector component is implicitly '0' * 4th vector component is implicitly '1' * * @param {vec2} out the receiving vector * @param {vec2} a the vector to transform * @param {mat4} m matrix to transform with * @returns {vec2} out */export function transformMat4(out, a, m) {  let x = a[0];  let y = a[1];  out[0] = m[0] * x + m[4] * y + m[12];  out[1] = m[1] * x + m[5] * y + m[13];  return out;}/** * Rotate a 2D vector * @param {vec2} out The receiving vec2 * @param {vec2} a The vec2 point to rotate * @param {vec2} b The origin of the rotation * @param {Number} c The angle of rotation * @returns {vec2} out */export function rotate(out, a, b, c) {  //Translate point to the origin  let p0 = a[0] - b[0],  p1 = a[1] - b[1],  sinC = Math.sin(c),  cosC = Math.cos(c);    //perform rotation and translate to correct position  out[0] = p0*cosC - p1*sinC + b[0];  out[1] = p0*sinC + p1*cosC + b[1];  return out;}/** * Get the angle between two 2D vectors * @param {vec2} a The first operand * @param {vec2} b The second operand * @returns {Number} The angle in radians */export function angle(a, b) {  let x1 = a[0],    y1 = a[1],    x2 = b[0],    y2 = b[1];    let len1 = x1*x1 + y1*y1;  if (len1 > 0) {    //TODO: evaluate use of glm_invsqrt here?    len1 = 1 / Math.sqrt(len1);  }    let len2 = x2*x2 + y2*y2;  if (len2 > 0) {    //TODO: evaluate use of glm_invsqrt here?    len2 = 1 / Math.sqrt(len2);  }    let cosine = (x1 * x2 + y1 * y2) * len1 * len2;      if(cosine > 1.0) {    return 0;  }  else if(cosine < -1.0) {    return Math.PI;  } else {    return Math.acos(cosine);  }}/** * Returns a string representation of a vector * * @param {vec2} a vector to represent as a string * @returns {String} string representation of the vector */export function str(a) {  return 'vec2(' + a[0] + ', ' + a[1] + ')';}/** * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) * * @param {vec2} a The first vector. * @param {vec2} b The second vector. * @returns {Boolean} True if the vectors are equal, false otherwise. */export function exactEquals(a, b) {  return a[0] === b[0] && a[1] === b[1];}/** * Returns whether or not the vectors have approximately the same elements in the same position. * * @param {vec2} a The first vector. * @param {vec2} b The second vector. * @returns {Boolean} True if the vectors are equal, false otherwise. */export function equals(a, b) {  let a0 = a[0], a1 = a[1];  let b0 = b[0], b1 = b[1];  return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&          Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)));}/** * Alias for {@link vec2.length} * @function */export const len = length;/** * Alias for {@link vec2.subtract} * @function */export const sub = subtract;/** * Alias for {@link vec2.multiply} * @function */export const mul = multiply;/** * Alias for {@link vec2.divide} * @function */export const div = divide;/** * Alias for {@link vec2.distance} * @function */export const dist = distance;/** * Alias for {@link vec2.squaredDistance} * @function */export const sqrDist = squaredDistance;/** * Alias for {@link vec2.squaredLength} * @function */export const sqrLen = squaredLength;/** * Perform some operation over an array of vec2s. * * @param {Array} a the array of vectors to iterate over * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed * @param {Number} offset Number of elements to skip at the beginning of the array * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array * @param {Function} fn Function to call for each vector in the array * @param {Object} [arg] additional argument to pass to fn * @returns {Array} a * @function */export const forEach = (function() {  let vec = create();  return function(a, stride, offset, count, fn, arg) {    let i, l;    if(!stride) {      stride = 2;    }    if(!offset) {      offset = 0;    }    if(count) {      l = Math.min((count * stride) + offset, a.length);    } else {      l = a.length;    }    for(i = offset; i < l; i += stride) {      vec[0] = a[i]; vec[1] = a[i+1];      fn(vec, vec, arg);      a[i] = vec[0]; a[i+1] = vec[1];    }    return a;  };})();</code></pre>        </article>    </section></div><nav>    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-glMatrix.html">glMatrix</a></li><li><a href="module-mat2.html">mat2</a></li><li><a href="module-mat2d.html">mat2d</a></li><li><a href="module-mat3.html">mat3</a></li><li><a href="module-mat4.html">mat4</a></li><li><a href="module-quat.html">quat</a></li><li><a href="module-quat2.html">quat2</a></li><li><a href="module-vec2.html">vec2</a></li><li><a href="module-vec3.html">vec3</a></li><li><a href="module-vec4.html">vec4</a></li></ul></nav><br class="clear"><footer>    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jul 13 2018 11:51:33 GMT+0200 (W. Europe Daylight Time)</footer><script> prettyPrint(); </script><script src="scripts/linenumber.js"> </script></body></html>
 |