123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JSDoc: Source: math/Vector2.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: math/Vector2.js</h1>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>"use strict";
- /**
- * Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.
- *
- * @class
- * @param {number} x
- * @param {number} y
- */
- function Vector2(x, y)
- {
- this.x = x || 0;
- this.y = y || 0;
- }
- /**
- * Set vector x and y values.
- *
- * @param {number} x
- * @param {number} y
- */
- Vector2.prototype.set = function(x, y)
- {
- this.x = x;
- this.y = y;
- };
- /**
- * Set a scalar value into the x and y values.
- */
- Vector2.prototype.setScalar = function(scalar)
- {
- this.x = scalar;
- this.y = scalar;
- };
- /**
- * Create a clone of this vector object.
- */
- Vector2.prototype.clone = function()
- {
- return new Vector2(this.x, this.y);
- };
- /**
- * Copy the content of another vector into this one.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.copy = function(v)
- {
- this.x = v.x;
- this.y = v.y;
- };
- /**
- * Add the content of another vector to this one.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.add = function(v)
- {
- this.x += v.x;
- this.y += v.y;
- };
- /**
- * Add a scalar value to booth vector components.
- *
- * @param {number} s
- */
- Vector2.prototype.addScalar = function(s)
- {
- this.x += s;
- this.y += s;
- };
- /**
- * Add two vectors and store the result in this vector.
- *
- * @param {Vector2} a
- * @param {Vector2} b
- */
- Vector2.prototype.addVectors = function(a, b)
- {
- this.x = a.x + b.x;
- this.y = a.y + b.y;
- };
- /**
- * Scale a vector components and add the result to this vector.
- *
- * @param {Vector2} v
- * @param {number} s
- */
- Vector2.prototype.addScaledVector = function(v, s)
- {
- this.x += v.x * s;
- this.y += v.y * s;
- };
- /**
- * Subtract the content of another vector to this one.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.sub = function(v)
- {
- this.x -= v.x;
- this.y -= v.y;
- };
- /**
- * Subtract a scalar value to booth vector components.
- *
- * @param {number} s
- */
- Vector2.prototype.subScalar = function(s)
- {
- this.x -= s;
- this.y -= s;
- };
- /**
- * Subtract two vectors and store the result in this vector.
- *
- * @param {Vector2} a
- * @param {Vector2} b
- */
- Vector2.prototype.subVectors = function(a, b)
- {
- this.x = a.x - b.x;
- this.y = a.y - b.y;
- };
- /**
- * Multiply the content of another vector to this one.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.multiply = function(v)
- {
- this.x *= v.x;
- this.y *= v.y;
- };
- /**
- * Multiply a scalar value by booth vector components.
- *
- * @param {number} s
- */
- Vector2.prototype.multiplyScalar = function(scalar)
- {
- this.x *= scalar;
- this.y *= scalar;
- };
- /**
- * Divide the content of another vector from this one.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.divide = function(v)
- {
- this.x /= v.x;
- this.y /= v.y;
- };
- /**
- * Divide a scalar value by booth vector components.
- *
- * @param {number} s
- */
- Vector2.prototype.divideScalar = function(scalar)
- {
- return this.multiplyScalar(1 / scalar);
- };
- /**
- * Set the minimum of x and y coordinates between two vectors.
- *
- * X is set as the min between this vector and the other vector.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.min = function(v)
- {
- this.x = Math.min(this.x, v.x);
- this.y = Math.min(this.y, v.y);
- };
- /**
- * Set the maximum of x and y coordinates between two vectors.
- *
- * X is set as the max between this vector and the other vector.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.max = function(v)
- {
- this.x = Math.max(this.x, v.x);
- this.y = Math.max(this.y, v.y);
- };
- Vector2.prototype.clamp = function(min, max)
- {
- // assumes min < max, componentwise
- this.x = Math.max(min.x, Math.min(max.x, this.x));
- this.y = Math.max(min.y, Math.min(max.y, this.y));
- };
- Vector2.prototype.clampScalar = function(minVal, maxVal)
- {
- this.x = Math.max(minVal, Math.min(maxVal, this.x));
- this.y = Math.max(minVal, Math.min(maxVal, this.y));
- };
- Vector2.prototype.clampLength = function(min, max)
- {
- var length = this.length();
- return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
- };
- /**
- * Round the vector coordinates to integer by flooring to the smaller integer.
- */
- Vector2.prototype.floor = function()
- {
- this.x = Math.floor(this.x);
- this.y = Math.floor(this.y);
- };
- /**
- * Round the vector coordinates to integer by ceiling to the bigger integer.
- */
- Vector2.prototype.ceil = function()
- {
- this.x = Math.ceil(this.x);
- this.y = Math.ceil(this.y);
- };
- /**
- * Round the vector coordinates to their closest integer.
- */
- Vector2.prototype.round = function()
- {
- this.x = Math.round(this.x);
- this.y = Math.round(this.y);
- };
- /**
- * Negate the coordinates of this vector.
- */
- Vector2.prototype.negate = function()
- {
- this.x = -this.x;
- this.y = -this.y;
- return this;
- };
- /**
- * Dot multiplication between this vector and another vector.
- *
- * @param {Vector2} vector
- * @return {number} Result of the dot multiplication.
- */
- Vector2.prototype.dot = function(v)
- {
- return this.x * v.x + this.y * v.y;
- };
- /**
- * Cross multiplication between this vector and another vector.
- *
- * @param {Vector2} vector
- * @return {number} Result of the cross multiplication.
- */
- Vector2.prototype.cross = function(v)
- {
- return this.x * v.y - this.y * v.x;
- };
- /**
- * Squared length of the vector.
- *
- * Faster for comparions.
- */
- Vector2.prototype.lengthSq = function()
- {
- return this.x * this.x + this.y * this.y;
- };
- /**
- * Length of the vector.
- */
- Vector2.prototype.length = function()
- {
- return Math.sqrt(this.x * this.x + this.y * this.y);
- };
- /**
- * Manhattan length of the vector.
- */
- Vector2.prototype.manhattanLength = function()
- {
- return Math.abs(this.x) + Math.abs(this.y);
- };
- /**
- * Normalize the vector (make it length one).
- */
- Vector2.prototype.normalize = function()
- {
- return this.divideScalar(this.length() || 1);
- };
- /**
- * Computes the angle in radians with respect to the positive x-axis
- */
- Vector2.prototype.angle = function()
- {
- var angle = Math.atan2(this.y, this.x);
- if(angle < 0)
- {
- angle += 2 * Math.PI;
- }
-
- return angle;
- };
- /**
- * Distance between two vector positions.
- */
- Vector2.prototype.distanceTo = function(v)
- {
- return Math.sqrt(this.distanceToSquared(v));
- };
- /**
- * Distance between two vector positions squared.
- *
- * Faster for comparisons.
- */
- Vector2.prototype.distanceToSquared = function(v)
- {
- var dx = this.x - v.x;
- var dy = this.y - v.y;
- return dx * dx + dy * dy;
- };
- /**
- * Manhattan distance between two vector positions.
- */
- Vector2.prototype.manhattanDistanceTo = function(v)
- {
- return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
- };
- /**
- * Scale the vector to have a defined length value.
- */
- Vector2.prototype.setLength = function(length)
- {
- return this.normalize().multiplyScalar(length);
- };
- Vector2.prototype.lerp = function(v, alpha)
- {
- this.x += (v.x - this.x) * alpha;
- this.y += (v.y - this.y) * alpha;
- };
- Vector2.prototype.lerpVectors = function(v1, v2, alpha)
- {
- return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
- };
- /**
- * Check if two vectors are equal.
- *
- * @param {Vector2} v
- */
- Vector2.prototype.equals = function(v)
- {
- return ((v.x === this.x) && (v.y === this.y));
- };
- /**
- * Set vector value from array with a offset.
- *
- * @param {array} array
- * @param {number} [offset]
- */
- Vector2.prototype.fromArray = function(array, offset)
- {
- if(offset === undefined) offset = 0;
- this.x = array[offset];
- this.y = array[offset + 1];
- };
- /**
- * Convert this vector to an array.
- *
- * @param {array} array
- * @param {number} [offset]
- */
- Vector2.prototype.toArray = function(array, offset)
- {
- if(array === undefined) array = [];
- if(offset === undefined) offset = 0;
- array[offset] = this.x;
- array[offset + 1] = this.y;
- return array;
- };
- /**
- * Rotate the vector around a central point.
- *
- * @param {Vector2} center
- * @param {number} angle
- */
- Vector2.prototype.rotateAround = function(center, angle)
- {
- var c = Math.cos(angle);
- var s = Math.sin(angle);
- var x = this.x - center.x;
- var y = this.y - center.y;
- this.x = x * c - y * s + center.x;
- this.y = x * s + y * c + center.y;
- };
- export {Vector2};
- </code></pre>
- </article>
- </section>
- </div>
- <nav>
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul>
- </nav>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 13 2019 11:55:28 GMT+0100 (Western European Summer Time)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|