Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
/**
* Get an arc (circle segment) between two points.
* @param x0 The x-coordinate of the arc's starting point.
* @param y0 The y-coordinate of the arc's starting point.
* @param a0 The angle in radians of the arc's starting point.
* @param x1 The x-coordinate of the arc's ending point.
* @param y1 The y-coordinate of the arc's ending point.
* @param a1 The angle in radians of the arc's ending point.
* @param r The radius of the circle.
* @param flip Whether to flip the segment.
@steveruizok
steveruizok / mapValues.ts
Created October 11, 2020 21:05
Map values.
export function remap<P, T>(
obj: { [key: string]: T },
fn: (value: T, index: number) => P
): { [key: string]: P } {
return Object.fromEntries(
Object.entries(obj).map(([id, value], index) => [id, fn(value, index)])
)
}
@steveruizok
steveruizok / castRay.tsx
Created October 4, 2020 19:41
A minimal ray-casting function.
export type RayOptions = {
max?: number
min?: number
start?: number
}
/**
* Casts a ray in a given direction and check if it hits something.
* @param from - [x, y]
@steveruizok
steveruizok / castRay.tsx
Created October 4, 2020 13:21
Cast a ray.
export function castRay<T = any>(
from: number[],
direction: number[],
hitTest: (info: {
point: number[]
distance: number
position: number[]
positions: number[][]
}) => T,
@steveruizok
steveruizok / getScreenFrame.tsx
Created August 27, 2020 09:19
Get the screen frame of an element.
function getScreenFrame(el: HTMLElement) {
var rect = el.getBoundingClientRect(),
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop
const x = rect.left + scrollLeft,
y = rect.top + scrollTop,
w = rect.width,
h = rect.height
@steveruizok
steveruizok / bugfix.md
Created August 21, 2020 08:28
SwiftUI TextField bug, where first character comes after the second.

Bug Fix

Hey all, just getting started with Swift and running into this issue where, on Textfields with bindings, the first character is always placed ahead of the cursor.

The fix was to upgrade MacOS from 10.15.5 to 10.15.6.

@steveruizok
steveruizok / getSegmentCubicBezierIntersections.ts
Last active May 13, 2022 06:32
Get the point(s) at which a line segment intersects a cubic bezier curve.
// Adapted from https://github.com/w8r/bezier-intersect
/**
* Get the point(s) at which a line segment intersects a cubic bezier curve.
* @param p0x The x-coordinate of the curve's starting point.
* @param p0y The y-coordinate of the curve's starting point.
* @param p1x The x-coordinate of the curve's first control point.
* @param p1y The y-coordinate of the curve's first control point.
* @param p2x The x-coordinate of the curve's second control point.
* @param p2y The y-coordinate of the curve's second control point.
@steveruizok
steveruizok / getSegmentCircleIntersections.ts
Created July 24, 2020 19:10
Get the point(s) at which a line segment intersects a circle.
function getSegmentCircleIntersections(
cx: number,
cy: number,
r: number,
x0: number,
y0: number,
x1: number,
y1: number
) {
var b: number,
@steveruizok
steveruizok / getSegmentSegmentIntersection.ts
Created July 24, 2020 18:48
Find the point at which one line segment intersects another.
function getSegmentSegmentIntersection(
x0: number,
y0: number,
x1: number,
y1: number,
x2: number,
y2: number,
x3: number,
y3: number
) {
@steveruizok
steveruizok / getSegmentRoundedRectangleIntersection.ts
Created July 24, 2020 12:43
Get the points where a line segment intersects a rectangle with rounded corners.
/**
* Get the intersection points between a line segment and a rectangle with rounded corners.
* @param x0 The x-axis coordinate of the segment's starting point.
* @param y0 The y-axis coordinate of ththe segment's ending point.
* @param x1 The delta-x of the ray.
* @param y1 The delta-y of the ray.
* @param x The x-axis coordinate of the rectangle.
* @param y The y-axis coordinate of the rectangle.
* @param w The width of the rectangle.