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

35 lines
847 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
});
2018-02-12 11:13:50 +01:00
export const scale = (v, factor) => ({
x: v.x * factor,
y: v.y * factor
2017-08-24 10:55:36 +02:00
});
2018-02-12 11:13:50 +01:00
export const divide = (v, factor) => ({
x: v.x / factor,
y: v.y / factor
});
2018-02-12 11:13:50 +01:00
export const normal = (v) => ({
x: -v.y,
y: v.x
2017-08-24 10:55:36 +02:00
});
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));
2018-05-24 16:14:03 +02:00
export const angle = (v) => Math.atan2(v.y, v.x);
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
};
2018-02-11 23:50:57 +01:00
};