|
| 1 | +import {Coord, Handle, Point, Shape} from "./types"; |
| 2 | +import {distance, splitLine} from "../util"; |
| 3 | + |
| 4 | +export const copyPoint = (p: Point): Point => ({ |
| 5 | + x: p.x, |
| 6 | + y: p.y, |
| 7 | + handleIn: {...p.handleIn}, |
| 8 | + handleOut: {...p.handleOut}, |
| 9 | +}); |
| 10 | + |
| 11 | +export const expandHandle = (point: Coord, handle: Handle): Coord => { |
| 12 | + return { |
| 13 | + x: point.x + handle.length * Math.cos(handle.angle), |
| 14 | + y: point.y + handle.length * Math.sin(handle.angle), |
| 15 | + }; |
| 16 | +}; |
| 17 | + |
| 18 | +const collapseHandle = (point: Coord, handle: Coord): Handle => { |
| 19 | + const dx = handle.x - point.x; |
| 20 | + const dy = -handle.y + point.y; |
| 21 | + let angle = Math.atan2(dy, dx); |
| 22 | + return { |
| 23 | + angle: angle < 0 ? Math.abs(angle) : 2 * Math.PI - angle, |
| 24 | + length: Math.sqrt(dx ** 2 + dy ** 2), |
| 25 | + }; |
| 26 | +}; |
| 27 | + |
| 28 | +export const length = (a: Point, b: Point): number => { |
| 29 | + const aHandle = expandHandle(a, a.handleOut); |
| 30 | + const bHandle = expandHandle(b, b.handleIn); |
| 31 | + const ab = distance(a, b); |
| 32 | + const abHandle = distance(aHandle, bHandle); |
| 33 | + return (ab + abHandle + a.handleOut.length + b.handleIn.length) / 2; |
| 34 | +}; |
| 35 | + |
| 36 | +export const reverse = (shape: Shape): Shape => { |
| 37 | + const inverted: Shape = []; |
| 38 | + for (let i = 0; i < shape.length; i++) { |
| 39 | + const j = shape.length - i - 1; |
| 40 | + const p = copyPoint(shape[j]); |
| 41 | + p.handleIn.angle += Math.PI; |
| 42 | + p.handleOut.angle += Math.PI; |
| 43 | + inverted.push(p); |
| 44 | + } |
| 45 | + return inverted; |
| 46 | +}; |
| 47 | + |
| 48 | +export const shift = (offset: number, shape: Shape): Shape => { |
| 49 | + if (offset === 0) return shape; |
| 50 | + const out: Shape = []; |
| 51 | + for (let i = 0; i < shape.length; i++) { |
| 52 | + out.push(shape[(i + offset) % shape.length]); |
| 53 | + } |
| 54 | + return out; |
| 55 | +}; |
| 56 | + |
| 57 | +// Add a control point to the curve between a and b. |
| 58 | +// Percentage [0, 1] from a to b. |
| 59 | +// a: original first point. |
| 60 | +// b: original last point. |
| 61 | +// c: new first point. |
| 62 | +// d: new added point. |
| 63 | +// e: new last point. |
| 64 | +// f: split point between a and b's handles. |
| 65 | +// g: split point between c's handle and f. |
| 66 | +// h: split point between e's handle and f. |
| 67 | +export const split = (percentage: number, a: Point, b: Point): [Point, Point, Point] => { |
| 68 | + const c = copyPoint(a); |
| 69 | + c.handleOut.length *= percentage; |
| 70 | + |
| 71 | + const e = copyPoint(b); |
| 72 | + e.handleIn.length *= 1 - percentage; |
| 73 | + |
| 74 | + const aHandle = expandHandle(a, a.handleOut); |
| 75 | + const bHandle = expandHandle(b, b.handleIn); |
| 76 | + const cHandle = expandHandle(c, c.handleOut); |
| 77 | + const eHandle = expandHandle(e, e.handleIn); |
| 78 | + const f = splitLine(percentage, aHandle, bHandle); |
| 79 | + const g = splitLine(percentage, cHandle, f); |
| 80 | + const h = splitLine(1 - percentage, eHandle, f); |
| 81 | + const dCoord = splitLine(percentage, g, h); |
| 82 | + |
| 83 | + const d: Point = { |
| 84 | + x: dCoord.x, |
| 85 | + y: dCoord.y, |
| 86 | + handleIn: collapseHandle(dCoord, g), |
| 87 | + handleOut: collapseHandle(dCoord, h), |
| 88 | + }; |
| 89 | + return [c, d, e]; |
| 90 | +}; |
0 commit comments