saxi
Version:
Drive the AxiDraw pen plotter
56 lines • 1.17 kB
JavaScript
/**
* Square of length of the vector
*/
export function vlen2(a) {
return a.x * a.x + a.y * a.y;
}
/**
* Length of the vector
*/
export function vlen(a) {
return Math.sqrt(vlen2(a));
}
/**
* Vector operation a - b
*/
export function vsub(a, b) {
return { x: a.x - b.x, y: a.y - b.y };
}
/**
* Scalar vector multiplication s * a
*/
export function vmul(a, s) {
return { x: a.x * s, y: a.y * s };
}
/**
* Get a normalized vector - length 1
*/
export function vnorm(a) {
return vmul(a, 1 / vlen(a));
}
/**
* Vector operation a + b
*/
export function vadd(a, b) {
return { x: a.x + b.x, y: a.y + b.y };
}
/**
* Vector dot-product a . b
*/
export function vdot(a, b) {
return a.x * b.x + a.y * b.y;
}
/**
* Rotate vector "v" an angle "a" around a center point "c".
*/
export function vrot(v, c, a) {
if (a === 0)
return v;
const radians = (Math.PI / 180) * a;
const cos = Math.cos(radians);
const sin = Math.sin(radians);
const nx = cos * (v.x - c.x) - sin * (v.y - c.y) + c.x;
const ny = cos * (v.y - c.y) + sin * (v.x - c.x) + c.y;
return { x: nx, y: ny };
}
//# sourceMappingURL=vec.js.map