Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249export type Pt = [number, number];export type Pt3 = [number, number, number];
export interface Rect { x0: number; y0: number; x1: number; y1: number;}
export interface Box extends Rect { z0: number; z1: number;}
export type Face = "top" | "front" | "side";
export interface Prism { fill: string; top: string; band: string; side: string;}
type Place = (u: number, v: number, d: number) => string;
const COS = Math.cos(Math.PI / 6);
const LIFT: Record<Face, (u: number, v: number, d: number) => Pt3> = { top: (u, v, d) => [u, v, d], front: (u, v, d) => [u, d, v], side: (u, v, d) => [d, u, v]};
const AXES: Record<Face, [Pt3, Pt3]> = { top: [ [1, 0, 0], [0, 1, 0] ], front: [ [1, 0, 0], [0, 0, -1] ], side: [ [0, -1, 0], [0, 0, -1] ]};
export const project = (x: number, y: number, z: number): Pt => [(x - y) * COS, (x + y) / 2 - z];
export const span = (x0: number, y0: number, x1: number, y1: number): Rect => ({ x0, y0, x1, y1 });
export const cuboid = ([x0, y0, z0]: Pt3, [x1, y1, z1]: Pt3): Box => ({ x0, y0, z0, x1, y1, z1 });
export const inset = <R extends Rect>(r: R, by: number): R => ({ ...r, x0: r.x0 + by, y0: r.y0 + by, x1: r.x1 - by, y1: r.y1 - by});
export const centre = (r: Rect): Pt => [(r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2];
export const near = (box: Box, face: Face) => face === "top" ? box.z1 : face === "front" ? box.y1 : box.x1;
const arc = (cu: number, cv: number, r: number, from: number, sweep: number, steps: number) => Array.from({ length: steps + 1 }, (_, k): Pt => { const a = from + (k / steps) * sweep; return [cu + Math.cos(a) * r, cv + Math.sin(a) * r]; });
export const rect = ({ x0, y0, x1, y1 }: Rect): Pt[] => [ [x0, y0], [x1, y0], [x1, y1], [x0, y1]];
export const rounded = ({ x0, y0, x1, y1 }: Rect, r: number, steps = 4): Pt[] => [ ...arc(x1 - r, y1 - r, r, 0, Math.PI / 2, steps), ...arc(x0 + r, y1 - r, r, Math.PI / 2, Math.PI / 2, steps), ...arc(x0 + r, y0 + r, r, Math.PI, Math.PI / 2, steps), ...arc(x1 - r, y0 + r, r, (3 * Math.PI) / 2, Math.PI / 2, steps)];
export const arched = ({ x0, y0, x1, y1 }: Rect, r: number): Pt[] => [ [x0, y0], [x1, y0], ...arc(x1 - r, y1 - r, r, 0, Math.PI / 2, 6), ...arc(x0 + r, y1 - r, r, Math.PI / 2, Math.PI / 2, 6)];
export const stadium = ({ x0, y0, x1, y1 }: Rect): Pt[] => { const r = (y1 - y0) / 2; return [ ...arc(x1 - r, y0 + r, r, -Math.PI / 2, Math.PI, 8), ...arc(x0 + r, y0 + r, r, Math.PI / 2, Math.PI, 8) ];};
export const circle = ([cx, cy]: Pt, r: number, n = 20): Pt[] => arc(cx, cy, r, 0, 2 * Math.PI, n).slice(0, n);
export const subdivide = (outline: Pt[], step: number) => outline.flatMap(([u, v], i): Pt[] => { const [nu, nv] = outline[(i + 1) % outline.length]!; const n = Math.max(1, Math.ceil(Math.hypot(nu - u, nv - v) / step)); return Array.from({ length: n }, (_, k): Pt => [ u + ((nu - u) * k) / n, v + ((nv - v) * k) / n ]); });
export const facingChain = (outline: Pt[]) => { const n = outline.length; const facing = outline.map((p, i) => { const next = outline[(i + 1) % n]!; return next[1] - p[1] - (next[0] - p[0]) > 0; }); const start = facing.findIndex((f, i) => f && !facing[(i - 1 + n) % n]); const chain: Pt[] = []; for (let k = 0; k <= n; k++) { const i = (start + k) % n; chain.push(outline[i]!); if (!facing[i]) break; } return chain;};
export const prism = (outline: Pt[], d0: number, d1: number, place: Place): Prism => { const chain = facingChain(outline); const face = outline.map(([u, v]) => place(u, v, d1)); const near = chain.map(([u, v]) => place(u, v, d1)); const far = chain.map(([u, v]) => place(u, v, d0)); const top = `M${face.join(" L")} Z`; const band = `M${near.join(" L")} L${far.toReversed().join(" L")} Z`; return { top, band, fill: `${top} ${band}`, side: `M${near[0]} L${far.join(" L")} L${near.at(-1)}` };};
const corners = (b: Box) => [b.x0, b.x1].flatMap((x) => [b.y0, b.y1].flatMap((y) => [b.z0, b.z1].map((z) => project(x, y, z))) );
const overlaps = (a: Pt[], b: Pt[]) => { const range = (pts: Pt[], k: 0 | 1) => [ Math.min(...pts.map((p) => p[k])), Math.max(...pts.map((p) => p[k])) ]; return ([0, 1] as const).every((k) => { const [a0, a1] = range(a, k); const [b0, b1] = range(b, k); return a0! < b1! && b0! < a1!; });};
export const behind = (a: Box, b: Box) => { if (a.x1 <= b.x0) return true; if (b.x1 <= a.x0) return false; if (a.y1 <= b.y0) return true; if (b.y1 <= a.y0) return false; return a.z1 <= b.z0;};
export const paintOrder = <T extends Box>(boxes: T[]): T[] => { const shadows = boxes.map(corners); const under = boxes.map(() => [] as number[]); for (let i = 0; i < boxes.length; i++) { for (let j = i + 1; j < boxes.length; j++) { if (!overlaps(shadows[i]!, shadows[j]!)) continue; if (behind(boxes[i]!, boxes[j]!)) under[j]!.push(i); else if (behind(boxes[j]!, boxes[i]!)) under[i]!.push(j); } } const out: T[] = []; const visited = boxes.map(() => false); const visit = (i: number) => { if (visited[i]) return; visited[i] = true; for (const j of under[i]!) visit(j); out.push(boxes[i]!); }; boxes.forEach((_, i) => visit(i)); return out;};
export interface Sketch { at(x: number, y: number, z: number): Pt; coord(x: number, y: number, z: number): string; polyline(points: Pt3[]): string; polygon(points: Pt3[]): string; line(a: Pt3, b: Pt3): string; seam(outline: Pt[], z: number): string; face(face: Face, outline: Pt[], depth: number): string; extrude(face: Face, outline: Pt[], d0: number, d1: number): Prism; block(box: Box, r: number, steps?: number): Prism; mark(face: Face, centre: Pt3, size: number): string;}
export const sketch = (cx: number, cy: number, scale: number): Sketch => { const at = (x: number, y: number, z: number): Pt => { const [u, v] = project(x, y, z); return [cx + u * scale, cy + v * scale]; }; const coord = (x: number, y: number, z: number) => at(x, y, z) .map((n) => n.toFixed(1)) .join(" "); const polyline = (points: Pt3[]) => `M${points.map((p) => coord(...p)).join(" L")}`; const polygon = (points: Pt3[]) => `${polyline(points)} Z`; const placeOn = (face: Face): Place => (u, v, d) => coord(...LIFT[face](u, v, d)); const extrude = (face: Face, outline: Pt[], d0: number, d1: number) => prism(outline, d0, d1, placeOn(face));
return { at, coord, polyline, polygon, line: (a, b) => polyline([a, b]), seam: (outline, z) => polyline(facingChain(outline).map(([x, y]) => [x, y, z])), face: (face, outline, depth) => polygon(outline.map(([u, v]) => LIFT[face](u, v, depth))), extrude, block: (box, r, steps) => extrude("top", rounded(box, r, steps), box.z0, box.z1), mark: (face, [x, y, z], size) => { const [u, v] = AXES[face]; const [ux, uy] = project(...u).map((n) => n * scale); const [vx, vy] = project(...v).map((n) => n * scale); const [ex, ey] = at( x - ((u[0] + v[0]) * size) / 2, y - ((u[1] + v[1]) * size) / 2, z - ((u[2] + v[2]) * size) / 2 ); return `matrix(${ux} ${uy} ${vx} ${vy} ${ex} ${ey}) scale(${size / 32})`; } };};