mirror of
https://github.com/PolarisSS13/Polaris.git
synced 2025-12-24 17:11:40 +00:00
TGUI v3.0
This ports TGUI, and makes the old nano crew monitor and the disposal bins use it as first examples.
This commit is contained in:
48
tgui/packages/common/vector.js
Normal file
48
tgui/packages/common/vector.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import { map, reduce, zipWith } from './collections';
|
||||
|
||||
/**
|
||||
* Creates a vector, with as many dimensions are there are arguments.
|
||||
*/
|
||||
export const vecCreate = (...components) => {
|
||||
if (Array.isArray(components[0])) {
|
||||
return [...components[0]];
|
||||
}
|
||||
return components;
|
||||
};
|
||||
|
||||
const ADD = (a, b) => a + b;
|
||||
const SUB = (a, b) => a - b;
|
||||
const MUL = (a, b) => a * b;
|
||||
const DIV = (a, b) => a / b;
|
||||
|
||||
export const vecAdd = (...vecs) => {
|
||||
return reduce((a, b) => zipWith(ADD)(a, b))(vecs);
|
||||
};
|
||||
|
||||
export const vecSubtract = (...vecs) => {
|
||||
return reduce((a, b) => zipWith(SUB)(a, b))(vecs);
|
||||
};
|
||||
|
||||
export const vecMultiply = (...vecs) => {
|
||||
return reduce((a, b) => zipWith(MUL)(a, b))(vecs);
|
||||
};
|
||||
|
||||
export const vecDivide = (...vecs) => {
|
||||
return reduce((a, b) => zipWith(DIV)(a, b))(vecs);
|
||||
};
|
||||
|
||||
export const vecScale = (vec, n) => {
|
||||
return map(x => x * n)(vec);
|
||||
};
|
||||
|
||||
export const vecInverse = vec => {
|
||||
return map(x => -x)(vec);
|
||||
};
|
||||
|
||||
export const vecLength = vec => {
|
||||
return Math.sqrt(reduce(ADD)(zipWith(MUL)(vec, vec)));
|
||||
};
|
||||
|
||||
export const vecNormalize = vec => {
|
||||
return vecDivide(vec, vecLength(vec));
|
||||
};
|
||||
Reference in New Issue
Block a user