123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <!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>/**
- * 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 X value.
- * @param {number} y Y value.
- */
- function Vector2(x, y)
- {
- this.x = x || 0;
- this.y = y || 0;
- }
- /**
- * Set vector x and y values.
- *
- * @param {number} x X value.
- * @param {number} y Y value.
- */
- Vector2.prototype.set = function(x, y)
- {
- this.x = x;
- this.y = y;
- };
- /**
- * Set a scalar value into the x and y values.
- *
- * @param {number} scalar Scalar value.
- */
- Vector2.prototype.setScalar = function(scalar)
- {
- this.x = scalar;
- this.y = scalar;
- };
- /**
- * Create a clone of this vector object.
- *
- * @return {Vector2} A new vector with the same values as this one.
- */
- 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 The other vector.
- */
- Vector2.prototype.add = function(v)
- {
- this.x += v.x;
- this.y += v.y;
- };
- /**
- * Add a scalar value to booth vector components.
- *
- * @param {number} s Scalar value.
- */
- Vector2.prototype.addScalar = function(s)
- {
- this.x += s;
- this.y += s;
- };
- /**
- * Add two vectors and store the result in this vector.
- *
- * @param {Vector2} a The first vector.
- * @param {Vector2} b The second vector.
- */
- 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 The other vector.
- * @param {number} s Scalar value.
- */
- 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 The other vector.
- */
- Vector2.prototype.sub = function(v)
- {
- this.x -= v.x;
- this.y -= v.y;
- };
- /**
- * Subtract a scalar value to booth vector components.
- *
- * @param {number} s Scalar value.
- */
- Vector2.prototype.subScalar = function(s)
- {
- this.x -= s;
- this.y -= s;
- };
- /**
- * Subtract two vectors and store the result in this vector.
- *
- * @param {Vector2} a The first vector.
- * @param {Vector2} b The second vector.
- */
- 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 The other vector.
- */
- Vector2.prototype.multiply = function(v)
- {
- this.x *= v.x;
- this.y *= v.y;
- };
- /**
- * Multiply a scalar value by booth vector components.
- *
- * @param {number} scalar Scalar value.
- */
- 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);
- };
- /**
- * Clamp the vector coordinates to the range defined by two vectors.
- *
- * Applied to x and y independently.
- *
- * @param {Vector2} min Minimum value.
- * @param {Vector2} max Maximum value.
- */
- 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));
- };
- /**
- * Clamp the vector coordinates to the range defined by two scalars.
- *
- * @param {number} minVal Minimum value.
- * @param {number} maxVal Maximum value.
- */
- 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.
- *
- * @return {number} Squared length of the vector.
- */
- Vector2.prototype.lengthSq = function()
- {
- return this.x * this.x + this.y * this.y;
- };
- /**
- * Length of the vector.
- *
- * @return {number} Length of the vector.
- */
- Vector2.prototype.length = function()
- {
- return Math.sqrt(this.x * this.x + this.y * this.y);
- };
- /**
- * Manhattan length of the vector.
- *
- * @return {number} 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).
- *
- * @return {Vector2} This vector.
- */
- Vector2.prototype.normalize = function()
- {
- return this.divideScalar(this.length() || 1);
- };
- /**
- * Computes the angle in radians with respect to the positive x-axis.
- *
- * @param {boolean} forcePositive If true, the angle will be forced to be positive.
- * @return {number} Angle in radians.
- */
- Vector2.prototype.angle = function(forcePositive)
- {
- var angle = Math.atan2(this.y, this.x);
- if(forcePositive && angle < 0)
- {
- angle += 2 * Math.PI;
- }
-
- return angle;
- };
- /**
- * Distance between two vector positions.
- *
- * @param {Vector2} v Vector to compute the distance to.
- * @return {number} Distance between the two vectors.
- */
- Vector2.prototype.distanceTo = function(v)
- {
- return Math.sqrt(this.distanceToSquared(v));
- };
- /**
- * Distance between two vector positions squared.
- *
- * Faster for comparisons.
- *
- * @param {Vector2} v Vector to compute the distance to.
- * @return {number} Distance between the two vectors squared.
- */
- 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.
- *
- * @param {Vector2} v Vector to compute the distance to.
- * @return {number} Manhattan distance between the two vectors.
- */
- 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.
- *
- * @param {number} length Length to scale the vector to.
- * @return {Vector2} This vector.
- */
- Vector2.prototype.setLength = function(length)
- {
- return this.normalize().multiplyScalar(length);
- };
- /**
- * Lerp this vector to another vector.
- *
- * @param {Vector2} v Vector to lerp to.
- * @param {number} alpha Lerp factor.
- */
- Vector2.prototype.lerp = function(v, alpha)
- {
- this.x += (v.x - this.x) * alpha;
- this.y += (v.y - this.y) * alpha;
- };
- /**
- * Lerp between this vector and another vector.
- *
- * @param {Vector2} v1 Vector to lerp from.
- * @param {Vector2} v2 Vector to lerp to.
- * @param {number} alpha Lerp factor.
- * @return {Vector2} This vector.
- *
- 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 Vector to compare with.
- */
- Vector2.prototype.equals = function(v)
- {
- return ((v.x === this.x) && (v.y === this.y));
- };
- /**
- * Set vector value from array [x, y].
- *
- * The vector can be converted to array using the toArray() method.
- *
- * @param {number[]} array Array to set the vector value from.
- */
- Vector2.prototype.fromArray = function(array)
- {
- this.set(array[0], array[1]);
- };
- /**
- * Convert this vector to an array. Useful for serialization and storage.
- *
- * Values stored as [x, y].
- *
- * @return {number[]} Array containing the values of the vector.
- */
- Vector2.prototype.toArray = function()
- {
- return [this.x, this.y];
- };
- /**
- * Rotate the vector around a central point.
- *
- * @param {Vector2} center Point to rotate around.
- * @param {number} angle Angle in radians.
- */
- 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="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><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="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</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="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</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><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
- </nav>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|