Vector
Basics for simulating physical laws.
cm.vec([x[, y]])
Constructs a vector with the specified x and y component. If either x or y are not specified, each defaults to 0. The returned vector has the following properties:
- x - x component of the vector
- y - y component of the vector
cm.vec(); // { x: 0, y: 0 }
cm.vec(1); // { x: 1, y: 0 }
cm.vec(2, 3); // { x: 2, y: 3 }
cm.vecFromAngle(angle)
Constructs a vector from the specified angle in radians.
cm.vecFromAngle(Math.PI / 4); // { x: 1, y: 1 }
cm.vecAdd(a, b)
Adds the specified vectors and returns a new vector.
const a = cm.vec(1, 2);
const b = cm.vec(2, 3);
const c = cm.vecAdd(a, b);
a; // { x: 1, y: 2 }
b; // { x: 2, y: 3 }
c; // { x: 3, y: 5 }
cm.vecAngle(a)
Computes the angle of the specified vector.
const a = cm.vec(1, 1);
cm.vecAngle(a); // Math.PI / 4
cm.vecClamp(a, min[, max])
Constrains the magnitude of the specified vector within the specified range [min, max], and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecClamp(a, 10, 15);
a; // { x: 3, y: 4 }
b; // { x: 6, y: 8 }
If two arguments are specified, the second one is interpreted as the maximum magnitude, with the minium magnitude defaults to 0.
const a = cm.vec(6, 8);
cm.vecClamp(a, 5); // { a: 3, b: 4 }
cm.vecClampX(a, min[, max])
Constrains the x component of the specified vector within the specified range [min, max], and returns a new vector.
const a = cm.vec(6, 8);
const b = cm.vecClampX(a, 10, 15);
a; // { x: 6, y: 8 }
b; // { x: 10, y: 8 }
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(6, 8);
const b = cm.vecClampX(a, 5);
a; // { x: 6, y: 8 }
b; // { x: 5, y: 8 }
cm.vecClampY(a, min[, max])
Constrains the y component of the specified vector within the specified range [min, max], and returns a new vector.
const a = cm.vec(6, 8);
const b = cm.vecClampY(a, 10, 15);
a; // { x: 6, y: 8 }
b; // { x: 6, y: 10 }
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(6, 8);
const b = cm.vecClampY(a, 5);
a; // { x: 6, y: 8 }
b; // { x: 6, y: 5 }
cm.vecCross(a, b)
Computes the cross product of the specified vectors.
const a = cm.vec(3, 4);
const b = cm.vec(1, 2);
cm.vecCross(a, b); // 2
cm.vecDist(a, b)
Computes the distance of the specified vectors.
const a = cm.vec(4, 6);
const b = cm.vec(1, 2);
cm.vecDist(a, b); // 5
cm.vecDist2(a, b)
Computes the square distance of the specified vectors.
const a = cm.vec(4, 6);
const b = cm.vec(1, 2);
cm.vecDist2(a, b); // 25
cm.vecDiv(a, value)
Divides the specified vector’s x and y component by the specified value, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecDiv(a, 0.5);
a; // { x: 3, y: 4 }
b; // { x: 6, y: 8 }
cm.vecDot(a, b)
Computes the dot product of the specified vectors.
const a = cm.vec(3, 4);
const b = cm.vec(1, 2);
cm.vecDot(a, b); // 11
cm.vecInX(a, min[, max])
Returns true if the specified vector’s x component is within the specified range [min, max].
const a = cm.vec(3, 4);
cm.vecInX(a, 1, 2); // false
cm.vecInX(a, 1, 3); // true
cm.vecInX(a, 1, 4); // true
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(3, 4);
cm.vecInX(a, 2); // false
cm.vecInX(a, 3); // true
cm.vecInX(a, 4); // true
cm.vecInY(a, x[, x1])
Returns true if the specified vector’s y component is within the specified range [min, max].
const a = cm.vec(3, 4);
cm.vecInY(a, 1, 3); // false
cm.vecInY(a, 1, 4); // true
cm.vecInY(a, 1, 5); // true
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(3, 4);
cm.vecInY(a, 3); // false
cm.vecInY(a, 4); // true
cm.vecInY(a, 5); // true
cm.vecMag(a[, value])
If only one argument is specified, computes the magnitude of the specified vector.
const a = cm.vec(3, 4);
cm.vecMag(a); // 5
If two arguments are specified, sets the magnitude of the specified vector to the specified value, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecMag(a, 10);
a; // { x: 3, y: 4 }
b; // { x: 6, y: 8 }
cm.vecMult(a, value)
Multiplies the specified vector’s x and y component by the specified value, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecMult(a, 2);
a; // { x: 3, y: 4 }
b; // { x: 6, y: 8 }
cm.vecNeg(a)
Negates the specified vector’s x and y component, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecNeg(a);
a; // { x: 3, y: 4 }
b; // { x: -3, y: -4 }
cm.vecNegX(a)
Negates the specified vector’s x component, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecNegX(a);
a; // { x: 3, y: 4 }
b; // { x: -3, y: 4 }
cm.vecNegY(a)
Negates the specified vector’s y component, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecNegY(a);
a; // { x: 3, y: 4 }
b; // { x: 3, y: -4 }
cm.vecNorm(a)
Normalizes the specified vector, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecNorm(a);
a; // { x: 3, y: 4 }
b; // { x: 0.6, y: 0.8 }
cm.vecRandom()
Returns a unit vector with a random heading, following a uniform distribution.
cm.vecRandom(); // { x: 0.9239434883837478, y: 0.688605153583981 }
cm.vecSub(a, b)
Subtracts the specified vectors and returns a new vector.
const a = cm.vec(1, 2);
const b = cm.vec(2, 4);
const c = cm.vecSub(a, b);
a; // { x: 1, y: 2 }
b; // { x: 2, y: 4 }
c; // { x: -1, y: -2 }
vec.clone()
Clones the vector and returns a new vector.
const a = cm.vec(1, 2);
const b = a.clone();
a === b; // false
b; // { x: 1, y: 2 }
vec.add(a)
Adds the specified vector to the target vector, and returns the target vector.
const a = cm.vec(1, 2);
const b = cm.vec(3, 4);
a.add(b); // a
a; // { x: 4, y: 6 }
vec.angle()
Computes the angle of the target vector.
const a = cm.vec(1, 1);
a.angle(); // Math.PI / 4
vec.clamp(min[, max])
Constrains the magnitude of the target vector within the specified range [min, max], and returns it.
const a = cm.vec(3, 4);
a.clamp(10, 15); // a
a; // { x: 6, y: 8 }
If two arguments are specified, the second one is interpreted as the maximum magnitude, with the minium magnitude defaults to 0.
const a = cm.vec(6, 8);
a.clamp(5); // a
a; // { a: 3, b: 4 }
vec.clampX(min[, max])
Constrains the x component of the target vector within the specified range [min, max], and returns it.
const a = cm.vec(6, 8);
a.clampX(10, 15); // a
a; // { x: 10, y: 8 }
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(6, 8);
a.clampX(5); // a
a; // { x: 5, y: 8 }
vec.clampY(min[, max])
Constrains the y component of the target vector within the specified range [min, max], and returns it.
const a = cm.vec(6, 8);
a.clampY(10, 15); // a
a; // { x: 6, y: 10 }
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(6, 8);
a.clampY(5);
a; // { x: 6, y: 5 }
vec.cross(a)
Computes the cross product of the specified vector and the target vector.
const a = cm.vec(3, 4);
const b = cm.vec(1, 2);
a.cross(b); // 2
vec.dist(a)
Computes the distance of the specified vector and the target vector.
const a = cm.vec(4, 6);
const b = cm.vec(1, 2);
a.dist(b); // 5
vec.dist2(a)
Computes the square distance of the specified vector and the target vector.
const a = cm.vec(4, 6);
const b = cm.vec(1, 2);
a.dist(b); // 25
vec.div(value)
Divides the target vector’ x and y component by the specified value, and returns it.
const a = cm.vec(3, 4);
a.div(0.5); // a
a; // { x: 6, y: 8 }
vec.dot(a)
Computes the dot product of the specified vector and the target vector.
const a = cm.vec(3, 4);
const b = cm.vec(1, 2);
a.dot(b); // 11
vec.inX(min[, max])
Returns true if the target vector’s x component is within the specified range [min, max].
const a = cm.vec(3, 4);
a.inX(1, 2); // false
a.inX(1, 3); // true
a.inX(1, 4); // true
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(3, 4);
a.inX(2); // false
a.inX(3); // true
a.inX(4); // true
vec.inY(min[, max])
Returns true if the target vector’s y component is within the specified range [min, max].
const a = cm.vec(3, 4);
a.inY(1, 3); // false
a.inY(1, 4); // true
a.inY(1, 5); // true
If two arguments are specified, the second one is interpreted as the maximum value, with the minium value defaults to 0.
const a = cm.vec(3, 4);
a.inY(3); // false
a.inY(4); // true
a.inY(5); // true
vec.mag([value])
If no argument is specified, computes the magnitude of the target vector.
const a = cm.vec(3, 4);
cm.mag(); // 5
If one argument is specified, sets the magnitude of the target to the specified value, and returns it.
const a = cm.vec(3, 4);
a.mag(10); // a
a; // { x: 6, y: 8 }
vec.mult(m)
Multiplies the specified component’s x and y by the specified value, and returns a new vector.
const a = cm.vec(3, 4);
const b = cm.vecMult(a, 2);
a; // { x: 3, y: 4 }
b; // { x: 6, y: 8 }
vec.neg()
Negates the target vector’s x and y component, and returns it.
const a = cm.vec(3, 4);
a.neg(); // a
a; // { x: -3, y: -4 }
vec.negX()
Negates the target vector’s x component, and returns it.
const a = cm.vec(3, 4);
a.negX(); // a
a; // { x: -3, y: 4 }
vec.negY()
Negates the target vector’s y component, and returns it.
const a = cm.vec(3, 4);
a.negY(); // a
a; // { x: 3, y: -4 }
vec.norm()
Normalizes the target vector, and returns it.
const a = cm.vec(3, 4);
a.norm(); // a
a; // { x: 0.6, y: 0.8 }
vec.set(x[, y])
If only one argument is specified and it is a vector instance, sets the target vector’s x and y component with the source vector’s x and y component, and returns the target vector.
const a = cm.vec(1, 2);
const b = cm.vec(3, 4);
a.set(b); // a
a; // { x: 3, y: 4 }
If two arguments are specified, sets the target vector’s x and y component with the specified x and y, and returns it.
const a = cm.vec(1, 2);
a.set(3, 4); // a
a; // { x: 3, y: 4 }
vec.setX(x)
Sets the target vector’s x component with the specified x, and returns it.
const a = cm.vec(1, 2);
a.setX(3); // a
a; // { x: 3, y: 2 }
vec.setY(y)
Sets the target vector’s y component with the specified y, and returns it.
const a = cm.vec(1, 2);
a.setY(3); // a
a; // { x: 1, y: 3 }
vec.sub(a)
Subtracts the target vector with the specified vector and returns the target vector.
const a = cm.vec(1, 2);
const b = cm.vec(2, 4);
a.sub(b); // a
a; // { x: -1, y: -2 }
b; // { x: 2, y: 4 }