Before jumping into the methods, it's highly recommened that you read through theVectorArgRest documentation, as it's used by all methods in the Vector class
Instance methods
Methods called on an instance WILL overwrite the instance
import { Vector } from'volts';consta=newVector(1,2,3);constb=a.add(1); // calls .add(1) on 'a', and assigns 'a' to 'const b'a.values; // [2,3,4]b.values; // [2,3,4]b.add(1); // also calls .add(1) on 'a'
The "correct" way to prevent the case above, is to copy the vector, then do the operation
import { Vector } from'volts';consta=newVector(1,2,3);constb=a.copy().add(1); // creates a new Vector and adds 1 to ita.values; // [1,2,3] - not modifiedb.values; // [2,3,4] - modifiedb.add(1); // only modifies the 'b' Vecto
import { Vector } from'volts';consta=newVector(2,2,2);constb=newVector(1,2,3);a.mul(b); // multiples 'a' and 'b' together, assigns the result to 'a'a.values; // [2,4,6]newVector(2); // Vector<3> [2,2,2]newVector(2).mul(1) // Vector<3> [2,2,2]newVector(2).mul(1,2,3); // Vector<3> [2,4,6]newVector(2).mul([1,2,3]); // Vector<3> [2,4,6]
Vector.random2D
This static method returns a random unit-length 2D Vector
import { Vector } from'volts';// Create with a predefined magnitudeconstrand2d=Vector.random2D(0.25);// Or a default of 1.0constrandDefault=Vector.random2D();rand3d.mag(); // 0.25rand3d.dimension; // 2randDefault.mag() // 1
This static method returns a random unit-length 3D Vector
import { Vector } from'volts';// Create with a predefined magnitudeconstrand3d=Vector.random3D(0.25);// Or a default of 1.0constrandDefault=Vector.random3D();rand3d.mag(); // 0.25rand3d.dimension; // 3randDefault.mag() // 1