2020-04-28 11:31:01 +10:00
|
|
|
import { toRadians, roundTo as roundToNumber } from "./shared";
|
2020-04-27 21:39:21 +10:00
|
|
|
|
2020-04-21 17:05:38 +10:00
|
|
|
export function lengthSquared(p) {
|
|
|
|
|
return p.x * p.x + p.y * p.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function length(p) {
|
|
|
|
|
return Math.sqrt(lengthSquared(p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalize(p) {
|
|
|
|
|
const l = length(p);
|
2020-04-27 21:39:21 +10:00
|
|
|
return divide(p, l);
|
2020-04-21 17:05:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dot(a, b) {
|
|
|
|
|
return a.x * b.x + a.y * b.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function subtract(a, b) {
|
2020-04-27 21:39:21 +10:00
|
|
|
if (typeof b === "number") {
|
|
|
|
|
return { x: a.x - b, y: a.y - b };
|
|
|
|
|
} else {
|
|
|
|
|
return { x: a.x - b.x, y: a.y - b.y };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function add(a, b) {
|
|
|
|
|
if (typeof b === "number") {
|
|
|
|
|
return { x: a.x + b, y: a.y + b };
|
|
|
|
|
} else {
|
|
|
|
|
return { x: a.x + b.x, y: a.y + b.y };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function multiply(a, b) {
|
|
|
|
|
if (typeof b === "number") {
|
|
|
|
|
return { x: a.x * b, y: a.y * b };
|
|
|
|
|
} else {
|
|
|
|
|
return { x: a.x * b.x, y: a.y * b.y };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function divide(a, b) {
|
|
|
|
|
if (typeof b === "number") {
|
|
|
|
|
return { x: a.x / b, y: a.y / b };
|
|
|
|
|
} else {
|
|
|
|
|
return { x: a.x / b.x, y: a.y / b.y };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function rotate(point, origin, angle) {
|
|
|
|
|
const cos = Math.cos(toRadians(angle));
|
|
|
|
|
const sin = Math.sin(toRadians(angle));
|
|
|
|
|
const dif = subtract(point, origin);
|
|
|
|
|
return {
|
|
|
|
|
x: origin.x + cos * dif.x - sin * dif.y,
|
|
|
|
|
y: origin.y + sin * dif.x + cos * dif.y,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function rotateDirection(direction, angle) {
|
|
|
|
|
return rotate(direction, { x: 0, y: 0 }, angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function min(a) {
|
|
|
|
|
return a.x < a.y ? a.x : a.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function max(a) {
|
|
|
|
|
return a.x > a.y ? a.x : a.y;
|
2020-04-21 17:05:38 +10:00
|
|
|
}
|
2020-04-28 11:31:01 +10:00
|
|
|
|
|
|
|
|
export function roundTo(p, to) {
|
|
|
|
|
return {
|
|
|
|
|
x: roundToNumber(p.x, to.x),
|
|
|
|
|
y: roundToNumber(p.y, to.y),
|
|
|
|
|
};
|
|
|
|
|
}
|