Vec
Constructors
Constructs a vector from two numbers.
Vec(3, 5)
// Result: Vec(3, 5)
xnumberThe x component
ynumberThe y component
Constructs a unit-length vector from an angle.
anglenumberAn angle specified in degrees
Constructs a unit-length vector from an angle.
anglenumberAn angle specified in radians
Properties
The x component
The y component
Methods
Returns a copy of this vector.
Transforms this vector by the affine matrix affineMatrix.
Use this when transforming a Vec that represents a point or position.
Transforms this vector by the affine matrix affineMatrix, ignoring
translation.
Use this instead of affineTransform() when transforming a Vec that
represents a normal or a tangent.
Transforms this vector.
vec.transform({
position: Vec(1, 2),
rotation: 45,
scale: Vec(1, 0.5), // Scale can also be a number
skew: 10,
origin: Vec(0, 0), // The center of rotation and scale
})
Multiplies both components of this vector by -1. This is functionally the
same as .mulScalar(-1).
Tests if this vector exactly equals the vector v.
vReturns true if the vectors are exactly equal, false otherwise.
Rounds the components of this vector to the next-lowest integer.
let v = Vec(3.141, -1.618);
v.floor();
// Result: Vec(3, -2)
Rounds the components of this vector to the next-highest integer.
let v = Vec(3.141, -1.618);
v.ceil();
// Result: Vec(4, -1)
Rounds the components of this vector to the closest integer.
let v = Vec(3.141, 3.618);
v.round();
// Result: Vec(3, 4)
Compares the components of this vector and v and sets this vector's to
the lesser of the two.
let a = Vec(1, 4);
let b = Vec(2, 3);
a.min(b);
// Result: Vec(1, 3)
Compares this vector's components to x and sets them to the lesser of the two.
let v = Vec(1, 2);
v.minScalar(3);
// Result: Vec(1, 2)
xnumberThe value to compare against
Compares the components of this vector and v and sets this vector's to
the greater of the two.
let a = Vec(1, 4);
let b = Vec(2, 3);
a.max(b);
// Result: Vec(2, 4)
Compares this vector's components to x and sets them to the greater of the two.
let v = Vec(1, 2);
v.maxScalar(3);
// Result: Vec(3, 3)
xnumberThe value to compare against
Linearly interpolates this vector to the vector v by the mixing factor
t.
Scales this vector so that its length is equal to 1.
Note the this vector must already have a non-zero length.
Rotates this vector clockwise by an angle specified in degrees.
anglenumberAn angle in degrees
Rotates this vector clockwise by an angle specified in radians.
angleRadiansnumberAn angle in radians
Rotates this vector clockwise by exactly 90°.
This is functionally the same as writing vec.rotate(90) but is
computationally simpler.
Rotates this vector counter-clockwise by exactly 90°.
This is functionally the same as writing vec.rotate(-90) but is
computationally simpler.
The vector projection of this vector onto a non-zero vector v, (also
known as the vector component or vector resolution of a in the direction of
b), is the orthogonal projection of this onto a straight line parallel to
v.
A trivial closest point implementation to maintain a uniform interface with Geometry.
vReturns a ClosestPointResult with itself as the position.
Returns the angle of this vector in degrees.
Returns the angle of this vector in radians.
Sets this vector to a unit-length vector at angle.
anglenumberAn angle specified in degrees
Sets this vector to a unit-length vector at angle.
anglenumberAn angle specified in radians
Returns true if this vector lies in the 180° region clockwise from v.
Returns the length of this vector. The length of a vector is also sometimes referred to as its "magnitude".
Returns the squared length of this vector.
This variation avoids the sqrt() used in length() and can be
computationally simpler when comparing the length of two vectors.
vReturns the squared distance from this vector to v.
This variation avoids the sqrt() used in distance() and can be
computationally simpler when comparing distances.
Returns true if both components of this vector are 0, false otherwise.
Returns true if both components of this vector are finite real numbers,
meaning not NaN or Infinity.
A two-dimensional vector.
Vectors can be used to represent positions, normals, tangents, offsets and translations.