2021-02-15 21:40:47 +00:00
|
|
|
import { Point } from "../vendor/bezier-js";
|
|
|
|
|
2021-03-26 22:07:01 +00:00
|
|
|
/** Return the "reverse" of the given point/vector, i.e. scale it by -1. */
|
|
|
|
export function reversePoint(p: Point): Point {
|
|
|
|
return { x: -p.x, y: -p.y };
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:52:52 +00:00
|
|
|
export function scalePointXY(p: Point, xScale: number, yScale: number): Point {
|
|
|
|
return {
|
|
|
|
x: p.x * xScale,
|
|
|
|
y: p.y * yScale,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:40:47 +00:00
|
|
|
export function subtractPoints(p1: Point, p2: Point): Point {
|
|
|
|
return {
|
|
|
|
x: p1.x - p2.x,
|
|
|
|
y: p1.y - p2.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function normalizePoint(p: Point): Point {
|
|
|
|
const len = Math.sqrt(Math.pow(p.x, 2) + Math.pow(p.y, 2));
|
2021-02-16 11:49:45 +00:00
|
|
|
if (len === 0) {
|
|
|
|
throw new Error(`Unable to normalize point with length 0`);
|
|
|
|
}
|
2021-02-15 21:40:47 +00:00
|
|
|
return {
|
|
|
|
x: p.x / len,
|
|
|
|
y: p.y / len,
|
|
|
|
};
|
|
|
|
}
|
2021-02-16 16:52:52 +00:00
|
|
|
|
|
|
|
export function normalizedPoint2rad(p: Point): number {
|
|
|
|
let result = Math.acos(p.x);
|
|
|
|
if (p.y < 0) {
|
|
|
|
result += (Math.PI - result) * 2;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|