Vectors
Yay! You have vectors too in Chelsea.js
Vectors are used to represent points in 2D space. They are used to represent the position of objects in 2D space.
Vectors open a whole new frontier in the world of graphics.
Following is a list of all the functions you can use with vectors in Chelsea.js
Create Vector
createVector(x, y) is used to create a vector with components x and y in 2D space.
createVector(x,y);
var v = createVector(1,2); // v= { x : 1, y : 2}
Add Vectors
addVec(v1, v2) is used to add a vector to another vector.
addVec(v1, v2);
var v1 = createVector(1,2);
var v2 = createVector(3,4);
var v3 = addVec(v1, v2); // v3 = { x : 4, y : 6}
Subtract Vectors
subtractVec(v1, v2) is used to subtract a vector from another vector.
subtractVec(v1, v2);
var v1 = createVector(1,4);
var v2 = createVector(6,2);
var v3 = subtractVec(v1, v2); // v3 = { x : -5, y : 2}
Multiply Vector
multiplyVec(v, scalar) is used to multiply a vector to a scalar( magnitude ).
multiplyVec(v, scalar);
var v = createVector(1,2);
var s = 3;
var v2 = multiplyVec(v, s); // v2 = { x : 3, y : 6}
Divide Vector
divideVec(v, scalar) is used to divide a vector by a scalar( magnitude ).
divideVec(v, scalar);
var v = createVector(5,9);
var s = 3;
var v2 = divideVec(v, s); // v2 = {x: 1.6666666666666667, y: 3}
Get Distance between two vectors
distanceVec(v1, v2) is used to get the distance between heads of two vectors.
distanceVec(v1, v2);
var v1 = createVector(1,2);
var v2 = createVector(3,4);
var d = distanceVec(v1, v2); // d = 2.8284271247461903
Angle Between Vectors
angleBetween(v1, v2) is used to get the angle between two vectors.
angleBetween(v1, v2);
var v1 = createVector(1,2);
var v2 = createVector(3,4);
var a = angleBetween(v1, v2); // a = 0.17985349979247847
Rotate Vector
rotate(v, angle) is used to rotate a vector by an angle.
rotate(v, angle);
var v = createVector(1,2);
var v2 = rotate(v, PI/2); // v2 = { x : 2, y : -1}
Magnitude
magnitude(v) is used to get the magnitude of a vector.
The magnitude is the length of the vector.
magnitude(v);
var v = createVector(3,4);
var m = magnitude(v); // m = 5
Angle
angle(v) is used to get the angle( in radian ) of a vector with the positive x-axis.
angle(v);
var v = createVector(3,4);
var a = angle(v); // a = 0.9272952180016122
Normalized Vector
normalize(v) is used to get the normalized vector of a vector.
The normalized vector is a vector with the same direction as the original vector, but with a magnitude of 1.
normalize(v);
var v = createVector(3,4);
var n = normalize(v); // n = { x : 0.6, y : 0.8}
Dot Product
dotProduct(v1, v2) is used to get the dot product of two vectors.
The dot product is the sum of the products of the components of the vectors.
dotProduct(v1, v2);
var v1 = createVector(4,6);
var v2 = createVector(3,4);
var d = dotProduct(v1, v2); // d = 36
Cross Product
crossProduct(v1, v2) is used to get the cross product of two vectors.
The cross product in 2D space is the area of a parallelogram with the two vectors as sides
and a negative value of the cross product simply signifies that the vectors are in clockwise order.
crossProduct(v1, v2);
var v1 = createVector(4,6);
var v2 = createVector(3,4);
var c = crossProduct(v1, v2); // c = -2
Projection
projection(v1, v2) is used to get the projection of a vector(v1) onto another vector(v2).
The projection in simple words is the length of shadow that vector(v1) casts on vector(v2).
projection(v1, v2);
var v1 = createVector(4,6);
var v2 = createVector(3,4);
var p = projection(v1, v2); // p = 2
Reflection
reflect(v1, v2) is used to get the reflection of a vector(v1) in another vector(v2).
The reflection in simple words is the vector that is created by reflecting the vector(v1) in vector(v2).
reflect(v1, v2);
var v1 = createVector(1,6);
var v2 = createVector(2,4);
var r = reflect(v1, v2); // r = { x : 4.2, y : 4.4}
Random Vector
randomVector() generates a normalized random vector.
randomVector();
Random Vector with Components
randomVectorComp(min, max) generates a vector with random components between minimum and maximum value.
randomVectorComp(min, max);
Random Vector with Magnitude
randomVectorMagnitude(min, max) generates a random vector with the specified minimum and maximum values.
randomVectorMagnitude(min, max);
Random Vector with Angle
randomVectorAngle(angle, min, max) generates a random vector with magnitude between m1, m2 at a given angle.
magnitude constraints are optional.
randomVectorAngle(angle, min, max);