| 5 | import {PatternFactoryRegistry} from './pattern_factory_registry.js'; |
| 6 | |
| 7 | export class CableLayout { |
| 8 | cables: number; |
| 9 | innerCables: boolean; |
| 10 | cableWidth: number = 2; // Stitches. |
| 11 | rowsBetweenCables: number; |
| 12 | cableEdgeRows: number; |
| 13 | smoothingRows: number; |
| 14 | rowsCount: number; |
| 15 | rowsWidth: number; |
| 16 | positions: number[][]; // positions[row][cable] |
| 17 | positionsInnerCables: number[][]; |
| 18 | marginType: 'None' | 'ICord' | 'Knit'; |
| 19 | marginDistance: number; |
| 20 | |
| 21 | constructor( |
| 22 | cables: number, rowsBetweenCables: number, cableEdgeRows: number, |
| 23 | smoothingRows: number, innerCables: boolean, |
| 24 | marginType: 'None' | 'ICord' | 'Knit', marginDistance: number) { |
| 25 | this.cables = cables; |
| 26 | this.innerCables = innerCables; |
| 27 | this.rowsBetweenCables = rowsBetweenCables; |
| 28 | this.cableEdgeRows = cableEdgeRows; |
| 29 | this.smoothingRows = smoothingRows; |
| 30 | this.rowsCount = cables * (this.cableEdgeRows + this.rowsBetweenCables); |
| 31 | this.rowsWidth = this.#rowsWidth(); |
| 32 | this.positions = this.#cablePositions(); |
| 33 | this.positionsInnerCables = this.#innerCablePositions(); |
| 34 | this.marginType = marginType; |
| 35 | this.marginDistance = marginDistance; |
| 36 | } |
| 37 | |
| 38 | #rowsWidth(): number { |
| 39 | const moveRows = 1 + this.rowsCount / 2 - this.cableEdgeRows; |
| 40 | const singleMoveRows = Math.min(moveRows, 2 * this.smoothingRows); |
| 41 | const doubleMoveRows = moveRows - singleMoveRows; |
| 42 | return this.cableWidth + singleMoveRows + 2 * doubleMoveRows; |
| 43 | } |
| 44 | |
| 45 | #cablePositions(): number[][] { |
| 46 | const output: number[][] = |
| 47 | Array.from({length: this.rowsCount}, () => Array(this.cables).fill(-1)); |
| 48 | for (let cable = 0; cable < this.cables; cable++) { |
| 49 | const cableStartRow = |
| 50 | cable * (this.cableEdgeRows + this.rowsBetweenCables); |
| 51 | let position = 0; |
| 52 | for (let row = 0; row < this.rowsCount / 2; row++) { |
| 53 | const currentRowIndex1 = (cableStartRow + row) % this.rowsCount; |
| 54 | const targetRow1 = output[currentRowIndex1]; |
| 55 | if (targetRow1 !== undefined) { |
| 56 | targetRow1[cable] = position; |
| 57 | } |
| 58 | const currentRowIndex2 = (cableStartRow + this.rowsCount / 2 + row) % this.rowsCount; |
| 59 | const targetRow2 = output[currentRowIndex2]; |
| 60 | if (targetRow2 !== undefined) { |
| 61 | targetRow2[cable] = |
| 62 | this.rowsWidth - position - this.cableWidth; |
| 63 | } |
| 64 | if (row < this.cableEdgeRows - 1) |
nothing calls this directly
no outgoing calls
no test coverage detected