Warning I won't be maintaining this library. I thought I'd write some tests and be done with it but in order to get the kinds of meaningful tests and generic-ness out of the library that I want, I'd be reinventing the wheel. There are already functional libraries that are much more stable/flexible/well-tested for the kinds of tasks that I'd want to do with
Vextreme
. If you're looking for an alternative, try ramdajs.
Type-safe functions for 3-dimensional vectors.
npm install vextreme
Use the types and functions in your source code
import { type Vec3, add } from 'vextreme';
const a: Vec3 = [0, 1, 2];
const b: Vec3 = [3, 4, 5];
const c = add(a, b);
- Vec2
- Vec3
- add
- angle
- createScale
- cross
- diff
- dot
- equal
- mag
- midpoint
- multiply
- negate
- normalize
- rotateLeft
- rotateRight
- toVec2
Represents a two-dimensional vector.
Represents a three-dimensional vector.
Component-wise addition of a
and b
.
add([0, 1, 2], [5, 4, 3]); // [5, 5, 5]
Returns Vec3
Calculates the angle in radians between a
and b
.
Note that the angle between any vector and the zero vector is NaN
because the zero vector does not have a magnitude.
angle([0, 1, 2], [0, 1, 2]); // 0
angle([1, 0, 0], [0, 1, 0]); // 1.57...
angle([0, 0, 0], [1, 0, 0]); // NaN
Returns number
Returns a function that takes v
and scales it by s
.
You can think of negation as a scaling of -1.
s
number
createScale(2)([0, 1, 2]); // [0, 2, 4]
const negate = createScale(-1);
negate([0, 1, 2]); // [0, -1, -2]
Calculates the cross product of a
and b
.
cross([0, 1, 2], [3, 4, 5]); // [-3, 6, -3]
cross([0, 0, 0], [0, 1, 2]); // [0, 0, 0]
cross([1, 0, 0], [0, 1, 0]); // [0, 0, 1]
Returns Vec3
Component-wise subtraction between a
and b
.
Note that diff(a, b)
!== diff(b, a)
.
diff([0, 1, 2], [0, 0, -1]); // [0, 1, 3]
diff([0, 0, -1], [0, 1, 2]); // [0, -1, -3]
diff([0, 1, 2], [0, 1, 2]); // [0, 0, 0]
Returns Vec3
Calculates the dot product of a
and b
.
dot([1, 0, 0], [0, 1, 0]); // 0
dot([0, 0, 0], [1, 0, 0]); // 0
dot([1, 0, 0], [2, 0, 0]); // 2
Returns number
Returns true if a
and b
are equal on a component-wise level.
In other words a[i] === b[i]
for i
in the range 0 <= i < a.length
.
const a: Vec3 = [0, 1, 2];
const b: Vec3 = [0, 1, 2];
a === b; // false
equal(a, b); // true
Returns boolean
Returns the magnitude of v
.
v
Vec3
mag(1, 0, 0); // 1
mag(0, 1, 2); // 2.23606797749979
Returns number
Calculates the midpoint as a Vec3
of the addition of a
and b
.
It may help to think of a
and b
as offsets from the origin.
First you move along a
then move along b
to get to the point p
.
If v
is a vector from the origin to p
, midpoint(a, b)
is scaling of v
by 1 / 2;
midpoint([1, 0, 0], [0, 1, 0]); // [0.5, 0.5, 0]
Returns Vec3
Component-wise multiplication of a
and b
.
You can think of multiply
as a scaling of a
by the vector b
where b[0] is a scaling in the x-dimension, b[1] in the y-dimension, and b[2] in the z-dimension.
If you only want to scale in one or two dimensions, you can use the identity for multiplication, a * 1 = a
.
multiply([0, 1, 2], [3, 4, 5]); // [0, 4, 10]
multiply([0, 1, 2], [1, 1, 5]); // [0, 1, 10]
multiply([0, 1, 2], [0, 0, 0]); // [0, 0, 0]
Returns Vec3
Negates each component of a Vec3
.
negate([0, 0, 0]); // [0, 0, 0]
negate([0, 1, 2]); // [0, -1, -2]
Normalizes v
.
v
Vec3
normalize([0, 1, 2]); // [0, 0.4472135954999579, 0.8944271909999159]
normalize([1, 0, 0]); // [1, 0, 0]
Returns Vec3
Rotates v
by shifting every component one position to the left.
Shifting the leftmost component moves it to the end.
v
Vec3
rotateLeft([0, 1, 2]); // [1, 2, 0]
Returns Vec3
Rotates v
by shifting every component one position to the right.
Shifting the rightmost component moves it to the start.
v
Vec3
rotateRight([0, 1, 2]); // [2, 0, 1]
Returns Vec3
Returns a Vec2
which is just v
without its z-component.
Useful for generating points in an SVG path or anytime you want to cast a Vec3
to a 2D space.
v
Vec3
toVec2([0, 1, 2]); // [0, 1]
Returns Vec2