Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
@steveruizok
steveruizok / throttle.ts
Created March 27, 2021 15:24
A typed throttle for Typescript.
export function throttle<T extends (...args: any[]) => any>(
fn: T,
wait: number
) {
let inThrottle: boolean, lastFn: any, lastTime: number
return function(...a: Parameters<T>): ReturnType<T> {
const context = this,
args = arguments
if (!inThrottle) {
@steveruizok
steveruizok / point-to-line.ts
Last active March 23, 2021 06:08
Get the minimum distance between a point and a line. Get the nearest point on a line to a second point.
/**
* Get the minimum distance from a point P to a line with a segment AB.
* @param A The start of the line.
* @param B The end of the line.
* @param P A point.
* @returns
*/
export function distanceToLine(A: number[], B: number[], P: number[]) {
const delta = sub(B, A)
const angle = Math.atan2(delta[1], delta[0])
@steveruizok
steveruizok / 0-getBezierCurveSegments.ts
Last active May 6, 2021 20:26
Get cubic bezier curve segments that connect an array of points.
function getBezierCurveSegments(points: number[][], tension = 0.4) {
const len = points.length,
cpoints: number[][] = points.slice(0)
if (len < 2) {
throw Error('Curve must have at least two points.')
}
for (let i = 1; i < len - 1; i++) {
let pp = points[i - 1],
@steveruizok
steveruizok / interpolateCubicBezier.ts
Created March 7, 2021 14:35
Interpolate a point along a cubic bezier curve.
export function interpolateCubicBezier(
p0: { x: number; y: number },
c0: { x: number; y: number },
c1: { x: number; y: number },
p1: { x: number; y: number }
) {
// 0 <= t <= 1
return function interpolator(t: number) {
return [
pow(1 - t, 3) * p0.x +
@steveruizok
steveruizok / getOuterTangents.ts
Last active May 13, 2022 06:28
Get outer tangents of two circles.
function getOuterTangents(
x0: number,
y0: number,
r0: number,
x1: number,
y1: number,
r1: number
) {
const dx = x1 - x0,
dy = y1 - y0,
@steveruizok
steveruizok / spline.ts
Last active May 13, 2022 06:29
Get a Bezier curve to fit the provided points.
/*
Use this with canvas.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, px, py).
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo
Uses a Catmull-Rom spline (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline).
*/
/**
* Get a bezier curve data to for a spline that fits an array of points.
* @param points An array of points formatted as [x, y]
* @param k
@steveruizok
steveruizok / uniqueArrays.ts
Created March 1, 2021 13:08
Unique values in an array.
// Get unique values from an array of primitives
export function unique<T extends any>(arr: T[]) {
return Array.from(new Set(arr).values())
}
export function uniqueAdjacent<T extends any>(arr: T[]) {
return arr.filter((t, i) => i === 0 || !(t === arr[i - 1]))
}
@steveruizok
steveruizok / pointsAreClockwise.ts
Last active February 28, 2021 11:15
Get whether points A and B are clockwise, relative to a point C.
function pointsAreClockwise(A: number[], B: number[], C: number[]) {
return (B[0] - A[0]) * (C[1] - A[1]) - (C[0] - A[0]) * (B[1] - A[1]) > 0
}
@steveruizok
steveruizok / rdp-simplify.ts
Created February 28, 2021 11:10
Simplify a line (using Ramer-Douglas-Peucker algorithm).
/**
* Simplify a line (using Ramer-Douglas-Peucker algorithm).
* @param points An array of points as [x, y, ...][]
* @param tolerance The minimum line distance (also called epsilon).
* @returns Simplified array as [x, y, ...][]
*/
export function simplify(points: number[][], tolerance = 1) {
const len = points.length,
a = points[0],
b = points[len - 1],
@steveruizok
steveruizok / snippet.ts
Last active January 22, 2021 21:44
Dream API for psuedo-bundled code.
async function getConcatenatedCodeFromWorker(...args: any[]) {
return "..."
}
async function getRunnableBundle<
Files extends Record<string, string>,
Entry extends keyof Files
>({
files,
entry,