This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Clamp a value into a range. | |
* @param n | |
* @param min | |
*/ | |
export function clamp(n: number, min: number): number | |
export function clamp(n: number, min: number, max: number): number | |
export function clamp(n: number, min: number, max?: number): number { | |
return Math.max(min, typeof max !== "undefined" ? Math.min(n, max) : n) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getRotatedEllipseBounds( | |
x: number, | |
y: number, | |
rx: number, | |
ry: number, | |
rotation: number | |
) { | |
const c = Math.cos(rotation) | |
const s = Math.sin(rotation) | |
const w = Math.hypot(rx * c, ry * s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getRelativeTransformedBoundingBox( | |
bounds: Bounds, | |
initialBounds: Bounds, | |
initialShapeBounds: Bounds, | |
isFlippedX: boolean, | |
isFlippedY: boolean | |
) { | |
const minX = | |
bounds.minX + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pointOnBezierCurve( | |
start: number[], | |
cp1: number[], | |
cp2: number[], | |
end: number[], | |
t: number | |
) { | |
let [ax, ay] = start | |
let [bx, by] = cp1 | |
let [cx, cy] = cp2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ## getStreamlinedPoints | |
* @description Streamline a point set, removing irregularities. | |
* @param points An array of points (as `[x, y]`). | |
* @returns number[][] | |
*/ | |
export function getStreamlinedPoints(points: number[][], streamline = 0.5) { | |
return points.reduce<number[][]>((acc, cur, i, arr) => { | |
if (i === 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pts = [ | |
[300, 100], | |
[500, 200], | |
[700, 250], | |
[800, 300], | |
[900, 500], | |
[800, 600], | |
[650, 650], | |
[500, 600], | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the intersection of two rays, with origin points p0 and p1, and direction vectors n0 and n1. | |
* @param p0 The origin point of the first ray | |
* @param n0 The direction vector of the first ray | |
* @param p1 The origin point of the second ray | |
* @param n1 The direction vector of the second ray | |
* @returns | |
*/ | |
export function getRayRayIntesection( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the nearest point on a line with a known unit vector that passes through point A | |
* @param A Any point on the line | |
* @param u The unit vector for the line. | |
* @param P A point not on the line to test. | |
* @returns | |
*/ | |
export function nearestPointOnLineThroughPoint( | |
A: number[], | |
u: number[], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find the closest point on a path to an off-path point. | |
* @param pathNode | |
* @param point | |
* @returns | |
*/ | |
export function getClosestPointOnPath( | |
pathNode: SVGPathElement, | |
point: number[] | |
) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the bounding box of a cubic bezier curve. | |
// Adapted from https://stackoverflow.com/a/24814530/7437688 | |
// Evaluate a point along a 1d bezier curve. | |
export function bez1d(a: number, b: number, c: number, d: number, t: number) { | |
return ( | |
a * (1 - t) * (1 - t) * (1 - t) + | |
3 * b * t * (1 - t) * (1 - t) + | |
3 * c * t * t * (1 - t) + | |
d * t * t * t |