Doodle3D-Slicer/src/sliceActions/helpers/VectorUtils.js

35 lines
846 B
JavaScript
Raw Normal View History

2017-08-24 10:55:36 +02:00
export const subtract = (a, b) => ({
x: a.x - b.x,
y: a.y - b.y
});
export const add = (a, b) => ({
x: a.x + b.x,
y: a.y + b.y
});
export const scale = (a, factor) => ({
x: a.x * factor,
y: a.y * factor
});
2018-02-01 16:59:35 +01:00
export const divide = (a, factor) => ({
x: a.x / factor,
y: a.y / factor
});
2017-08-24 10:55:36 +02:00
export const normal = (a) => ({
x: -a.y,
y: a.x
});
2018-02-01 16:13:04 +01:00
export const equals = (a, b) => a.x === b.x && a.y === b.y;
export const almostEquals = (a, b) => Math.abs(a.x - b.x) < 0.001 && Math.abs(a.y - b.y) < 0.001;
2017-08-24 10:55:36 +02:00
export const dot = (a, b) => a.x * b.x + a.y * b.y;
export const length = (v) => Math.sqrt(v.x * v.x + v.y * v.y);
2017-08-24 10:55:36 +02:00
export const distanceTo = (a, b) => length(subtract(a, b));
export const normalize = (v) => {
const l = length(v);
2017-08-24 10:55:36 +02:00
return {
x: v.x / l,
y: v.y / l
2017-08-24 10:55:36 +02:00
};
}
export const clone = (v) => ({ x: v.x, y: v.y });