|
| 1 | +import PathHelper from '@markroland/path-helper' |
| 2 | + |
| 3 | +export function spiral(r1, r2, n, sides) { |
| 4 | + |
| 5 | + let path = []; |
| 6 | + |
| 7 | + const PathHelp = new PathHelper(); |
| 8 | + |
| 9 | + const stepSize = (2 * Math.PI * r1) / sides; // Approximate step size based on initial radius |
| 10 | + let theta = 0; |
| 11 | + let r = r1; |
| 12 | + |
| 13 | + while (r >= r2) { |
| 14 | + path.push([ |
| 15 | + r * Math.cos(theta), |
| 16 | + r * Math.sin(theta), |
| 17 | + ]); |
| 18 | + |
| 19 | + theta += stepSize / r; // Increment angle based on step size |
| 20 | + r = r1 + (theta / (2 * Math.PI * n)) * (r2 - r1); // Increment radius proportionally |
| 21 | + } |
| 22 | + |
| 23 | + // path = PathHelp.simplify(path, stepSize); |
| 24 | + |
| 25 | + // const theta1 = 0; |
| 26 | + // const theta2 = n * 2 * Math.PI; |
| 27 | + // const i_max = n * sides; |
| 28 | + // for (let i = 0; i <= i_max; i++) { |
| 29 | + // const r = r1 + (i/i_max) * (r2 - r1); |
| 30 | + // const theta = theta1 + (i/i_max) * (theta2 - theta1); |
| 31 | + |
| 32 | + // path.push([ |
| 33 | + // r * Math.cos(theta), |
| 34 | + // r * Math.sin(theta), |
| 35 | + // ]); |
| 36 | + // } |
| 37 | + |
| 38 | + // path = PathHelp.simplify(path, step_size); |
| 39 | + |
| 40 | + return path; |
| 41 | +} |
| 42 | + |
| 43 | +export function zigZag(path, offset) { |
| 44 | + |
| 45 | + const PathHelp = new PathHelper(); |
| 46 | + |
| 47 | + let innerPath = PathHelp.offsetPath(path, -offset); |
| 48 | + |
| 49 | + // Need this for closed shapes: |
| 50 | + // innerPath = PathHelp.shiftPath(innerPath, 2); |
| 51 | + // innerPath.push(innerPath[0]); |
| 52 | + // innerPath.splice(1, 1); |
| 53 | + |
| 54 | + let outerPath = PathHelp.offsetPath(path, offset); |
| 55 | + |
| 56 | + // Need this for closed shapes: |
| 57 | + // outerPath = PathHelp.shiftPath(outerPath, 2); |
| 58 | + // outerPath.push(outerPath[0]); |
| 59 | + // outerPath.splice(1, 1); |
| 60 | + |
| 61 | + // ZigZag |
| 62 | + let newPath = []; |
| 63 | + for (let i = 0; i < path.length; i++) { |
| 64 | + if (i % 2 === 0) { |
| 65 | + newPath.push(innerPath[i]); |
| 66 | + newPath.push(outerPath[i]); |
| 67 | + } else { |
| 68 | + newPath.push(outerPath[i]); |
| 69 | + newPath.push(innerPath[i]); |
| 70 | + } |
| 71 | + } |
| 72 | + path = newPath; |
| 73 | + |
| 74 | + return newPath; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * This may not do quite what I want it to do. |
| 79 | + */ |
| 80 | +export function arcBetweenPoints(x1, y1, x2, y2, step_size) { |
| 81 | + |
| 82 | + const PathHelp = new PathHelper(); |
| 83 | + |
| 84 | + let path = [[x1, y1]]; |
| 85 | + |
| 86 | + const r1 = Math.sqrt(x1 * x1 + y1 * y1); |
| 87 | + const theta1 = Math.atan2(y1, x1); |
| 88 | + |
| 89 | + const r2 = Math.sqrt(x2 * x2 + y2 * y2); |
| 90 | + const theta2 = Math.atan2(y2, x2); |
| 91 | + |
| 92 | + const i_max = 1000; |
| 93 | + for (let i = 0; i < i_max; i++) { |
| 94 | + |
| 95 | + const r = r1 + (i/i_max) * (r2 - r1); |
| 96 | + const theta = theta1 + (i/i_max) * (theta2 - theta1); |
| 97 | + |
| 98 | + path.push([ |
| 99 | + r * Math.cos(theta), |
| 100 | + r * Math.sin(theta), |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + path = PathHelp.simplify(path, step_size); |
| 105 | + |
| 106 | + return path; |
| 107 | +} |
0 commit comments