/vector2/add
stdAdds two vectors.
Type signature
([x1, y1]: [number, number], [x2, y2]: [number, number]) => [number, number]
Examples
add([3, 5], [-1, 8]);
// ⇒ [2, 13]
Try in REPL
Questions
- How to add two vectors?
([x1, y1]: [number, number], [x2, y2]: [number, number]) => [number, number]
add([3, 5], [-1, 8]);
// ⇒ [2, 13]
Try in REPL
(space: {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}) => ([x, y]: [number, number]) => number[]
convertSpace(rotate(Math.PI))([
2, 3,
]);
// ⇒ [-2, -3]
Try in REPL
([a, b]: [number, number], [c, d]: [number, number]) => number
([a, b]: [number, number], [c, d]: [number, number]) => number
([x, y]: [number, number]) => number
length([3, 5]);
// ⇒ Math.sqrt(3 * 3 + 5 * 5)
Try in REPL
({ a, b, c, d, e, f }: {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}, [x, y]: [number, number]) => number[]
mul(scale(4, 5), [2, 3]);
// ⇒ [8, 15]
Try in REPL
(m1: {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}, m2: {
a: number;
b: number;
c: number;
d: number;
e: number;
f: number;
}) => {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}
multiply(
{
a: 1,
c: 2,
e: 3,
b: 4,
d: 5,
f: 6,
},
{
a: 7,
c: 8,
e: 9,
b: 10,
d: 11,
f: 12,
},
);
// ⇒ { a: 27, b: 78, c: 30, d: 87, e: 36, f: 102 }
Try in REPL
(vector: [number, number]) => [number, number]
normalize([2, 3]);
// ⇒ [2 / length([2, 3]), 3 / length([2, 3])]
Try in REPL
(a: [number, number], v: [number, number]) => [number, number]
reflect([1, -2], [1, 0]);
// ⇒ [0.6, 0.8]
Try in REPL
(angle?: number, cx?: number, cy?: number) => {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}
const angle = Math.PI;
const sine = Math.sin(angle);
const cosine = Math.cos(angle);
rotate(angle); // { a: cosine, b: sine, c: -sine, d: cosine, e: 0, f: 0 }
Try in REPL
(sx?: number, sy?: number) => {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}
scale(2, 3);
// ⇒ { a: 2, b: 0, c: 0, d: 3, e: 0, f: 0 }
Try in REPL
([x1, y1]: [number, number], [x2, y2]: [number, number]) => [number, number]
sub([3, 5], [-1, 8]);
// ⇒ [4, -3]
Try in REPL
(...matrices: {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}[]) => {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}
const originX = 5;
const originY = 6;
const angle = Math.PI;
transform(
translate(originX, originY),
rotate(angle),
translate(-originX, -originY),
);
// ⇒ rotate(Math.PI, originX, originY)
Try in REPL
(tx?: number, ty?: number) => {
a: number;
c: number;
e: number;
b: number;
d: number;
f: number;
}
translate(2, 3);
// ⇒ { a: 1, b: 0, c: 0, d: 1, e: 2, f: 3 }
Try in REPL