wmc/skycraft/linalg.ts

40 lines
820 B
TypeScript

export type vec3 = [number, number, number];
export function cross(a: vec3, b: vec3) : vec3 {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
];
}
export function diff(a: vec3, b: vec3) : vec3 {
return [
a[0] - b[0],
a[1] - b[1],
a[2] - b[2],
];
}
export function add(a: vec3, b: vec3) : vec3 {
return [
a[0] + b[0],
a[1] + b[1],
a[2] + b[2],
];
}
export function norm(a: vec3) : number {
return Math.sqrt(a[0] ** 2 + a[1] ** 2 + a[2] ** 2);
}
export function dot(a: vec3, b: vec3) : number {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
export function scale(a: vec3, s: number) : vec3 {
return [
a[0] * s,
a[1] * s,
a[2] * s,
];
}