Skip to content

Commit

Permalink
Vector.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakuna committed Jun 17, 2021
1 parent 5449bd1 commit a6fa4bf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
70 changes: 70 additions & 0 deletions docs/src/math/Vector.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Vector
======

**Extends:** Array_

A value with direction and magnitude.

magnitude
---------

**Type:** number

The magnitude (length; size) of this vector.

set()
-----

**Parameters:**

- number[] **...data**

**Returns:** Vector - Itself.

Sets the data in the vector.

cross()
-------

**Parameters:**

- Vector **vector**

**Returns:** Vector - Itself.

Calculates the cross product of two vectors.

operate()
---------

**Parameters:**

- Vector **vector**
- function(number **a**, number **b**) **operation**

**Returns:** Vector - Itself.

Performs an operation between two vectors.

.. code-block:: javascript
// Addition
console.log([...new Vector(1, 2, 3).operate(new Vector(1, 2, 3), (a, b) => a + b)]); // [2, 4, 6]
// Subtraction
console.log([...new Vector(1, 2, 3).operate(new Vector(1, 2, 3), (a, b) => a - b)]); // [0, 0, 0]
// Multiplication
console.log([...new Vector(1, 2, 3).operate(new Vector(1, 2, 3), (a, b) => a * b)]); // [1, 4, 9]
// Division
console.log([...new Vector(1, 2, 3).operate(new Vector(1, 2, 3), (a, b) => a / b)]); // [1, 1, 1]
normalize()
-----------

**Returns:** Vector - Itself.

Normalizes the vector so that it has a magnitude of 1 while maintaining its direction.

.. _Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
8 changes: 7 additions & 1 deletion docs/src/math/sigma.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ sigma()

**Returns:** number

Calculates the sum of expression(n) for range (min - max).
Calculates the sum of expression(n) for range (min - max).

.. code-block:: javascript
console.log(sigma(1, 4, (n) => n)); // 1 + 2 + 3 + 4 = 10
console.log(sigma(3, 6, (n) => n * 2)); // 6 + 8 + 10 + 12 = 36
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lakuna/umbra.js",
"version": "0.4.0",
"version": "0.5.0",
"description": "Lightweight JavaScript game framework.",
"keywords": [
"front-end",
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { arrayFromRule } from "./math/arrayFromRule.js";
export { sigma } from "./math/sigma.js";
export { degreesToRadians } from "./math/degreesToRadians.js";
export { Vector } from "./math/Vector.js";

// WebGL
// export * from "./webgl/constants.js";
26 changes: 26 additions & 0 deletions src/math/Vector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { arrayFromRule } from "./arrayFromRule.js";
import { sigma } from "./sigma.js";

export class Vector extends Array {
set(...data) {
while (this.length > 0) { this.pop(); }
for (const value of data) { this.push(value); }

return this;
}

cross = (vector) => this.set(...arrayFromRule(this.length, (i) => {
const loop = (i) => i < this.length ? i : i - this.length;
i = loop(i + 1);
let j = loop(i + 1);
return this[i] * vector[j] - this[j] * vector[i];
}));

operate = (vector, operation) => this.set(...arrayFromRule(this.length, (i) => operation(this[i], vector[i])));

normalize = () => this.set(...arrayFromRule(this.length, (i) => this[i] / this.magnitude));

get magnitude() {
return Math.sqrt(sigma(0, this.length - 1, (n) => this[n] ** 2));
}
};

0 comments on commit a6fa4bf

Please sign in to comment.