|
| 1 | +import type { V2 } from '../types'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Returns the euclidian distance between two points |
| 5 | + */ |
| 6 | +const distance = (a: V2, b: V2) => { |
| 7 | + return Math.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2); |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Given a sorted array arr and a value, returns the index i of arr |
| 12 | + * such that arr[i] < value && arr[i + 1] > value using binary search |
| 13 | + */ |
| 14 | +const getSortedIndex = (arr: number[], v: number) => { |
| 15 | + let low = 0, |
| 16 | + high = arr.length; |
| 17 | + while (low < high) { |
| 18 | + let mid = (low + high) >>> 1; // === Math.abs((low + high) * .5) but faster |
| 19 | + if (arr[mid] < v) low = mid + 1; |
| 20 | + else high = mid; |
| 21 | + } |
| 22 | + return low; |
| 23 | +}; |
| 24 | + |
1 | 25 | /** |
2 | | -Given a quadratic bezier defined by points a, b and c, |
3 | | -returns a series of n points on the curve |
| 26 | + * Given a polyline, returns n evenly-spaced points on the polyline |
| 27 | + */ |
| 28 | +const interpolatePolyline = (points: V2[], n: number) => { |
| 29 | + let res: V2[] = [], |
| 30 | + totalLength: number = 0, |
| 31 | + segments: number[] = [0]; |
| 32 | + |
| 33 | + for (let i = 0; i < points.length - 2; i++) { |
| 34 | + totalLength += distance(points[i], points[i + 1]); |
| 35 | + segments.push(totalLength); |
| 36 | + } |
| 37 | + |
| 38 | + for (let i = 0; i < n; i++) { |
| 39 | + const d: number = totalLength * (i / n); |
| 40 | + const si: number = getSortedIndex(segments, d); |
| 41 | + const p0 = points[si]; |
| 42 | + const p1 = points[si + 1]; |
| 43 | + |
| 44 | + const sn = [p1[0] - p0[0], p1[1] - p0[1]]; |
| 45 | + const segmentFraction = (d - segments[si]) / distance(p0, p1); |
| 46 | + |
| 47 | + res.push([p1[0] + sn[0] * segmentFraction, p1[1] + sn[1] * segmentFraction]); |
| 48 | + } |
4 | 49 |
|
5 | | -The quadratic bezier is: B(t) = (1-t)[(1-t)P0 + tP1] + t[(1-t)P1+tP2] |
6 | | -Source: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves |
| 50 | + return res; |
| 51 | +}; |
7 | 52 |
|
8 | | -Below is a naive implementation but good enough for now. For a better approach see: |
9 | | -https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves |
| 53 | +/** |
| 54 | + * Given a quadratic Bezier defined by points a, b and c |
| 55 | + * returns a series of evenly-spaced points on the curve |
| 56 | + * |
| 57 | + * Sources: |
| 58 | + * - https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves |
| 59 | + * - https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves |
10 | 60 | */ |
11 | | -const quadraticToPoints = ( |
12 | | - a: [number, number], |
13 | | - b: [number, number], |
14 | | - c: [number, number], |
15 | | - n = 10 |
16 | | -) => { |
17 | | - let points = []; |
| 61 | + |
| 62 | +const quadraticToPoints = (a: V2, b: V2, c: V2, n = 25) => { |
| 63 | + let points: V2[] = []; |
| 64 | + |
| 65 | + // 1. Interpolate the quadratic bezier function to obtain an uneven polyline |
18 | 66 | for (let i = 0; i < n; i++) { |
19 | 67 | const t = i / n; |
20 | | - const x = (1 - t) * ((1 - t) * a[0] + t * c[0]) + t * ((1 - t) * c[0] + t * b[0]); |
21 | | - const y = (1 - t) * ((1 - t) * a[1] + t * c[1]) + t * ((1 - t) * c[1] + t * b[1]); |
22 | | - points.push([x, y]); |
| 68 | + points.push([ |
| 69 | + (1 - t) * ((1 - t) * a[0] + t * c[0]) + t * ((1 - t) * c[0] + t * b[0]), |
| 70 | + (1 - t) * ((1 - t) * a[1] + t * c[1]) + t * ((1 - t) * c[1] + t * b[1]) |
| 71 | + ]); |
23 | 72 | } |
24 | | - return [...points, b] as [number, number][]; |
| 73 | + |
| 74 | + // 2. Interpolate the polyline from (1) to get evenly-spaced points |
| 75 | + points = interpolatePolyline([...points, b], n); |
| 76 | + |
| 77 | + return points; |
25 | 78 | }; |
26 | 79 |
|
27 | 80 | export default quadraticToPoints; |
0 commit comments