-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintarsia.js
executable file
·245 lines (223 loc) · 7.59 KB
/
intarsia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env node
//intarsia on a sheet, driven by a pattern giving the carrier name per stitch:
const Carriers = ["2", "6", "7"];
/*
const Pattern = [
".......XXX.............",
"......XXXX.............",
"......XXXX.............",
".............XXX.......",
"...............XX......",
"................XX.....",
"................XX.....",
"................XX.....",
"...............XX......",
".............XXX.......",
"......XXXX.............",
"......XXXX.............",
".......XXX.............",
];
*/
const Pattern = [
"..........XXXXXXXXXXXXXXXXXXXX..................",
"...........XXXXXXXXXXXXXXXXXXXX.................",
"............XXXXXXXXXXXXXXXXXXXX................",
".............XXXXXXXXXXXXXXXXXXXX...............",
"..............XXXXXXXXXXXXXXXXXXXX..............",
"...............XXXXXXXXXXXXXXXXXXXX.............",
"................XXXXXXXXXXXXXXXXXXXX............",
".................XXXXXXXXXXXXXXXXXXXX...........",
"..................XXXXXXXXXXXXXXXXXXXX..........",
".................XXXXXXXXXXXXXXXXXXXX...........",
"................XXXXXXXXXXXXXXXXXXXX............",
"...............XXXXXXXXXXXXXXXXXXXX.............",
"..............XXXXXXXXXXXXXXXXXXXX..............",
".............XXXXXXXXXXXXXXXXXXXX...............",
"............XXXXXXXXXXXXXXXXXXXX................",
"...........XXXXXXXXXXXXXXXXXXXX.................",
"..........XXXXXXXXXXXXXXXXXXXX..................",
".........XXXXXXXXXXXXXXXXXXXX...................",
"..........XXXXXXXXXXXXXXXXXXXX..................",
"...........XXXXXXXXXXXXXXXXXXXX.................",
"............XXXXXXXXXXXXXXXXXXXX................",
".............XXXXXXXXXXXXXXXXXXXX...............",
"..............XXXXXXXXXXXXXXXXXXXX..............",
"...............XXXXXXXXXXXXXXXXXXXX.............",
"................XXXXXXXXXXXXXXXXXXXX............",
".................XXXXXXXXXXXXXXXXXXXX...........",
"..................XXXXXXXXXXXXXXXXXXXX..........",
".................XXXXXXXXXXXXXXXXXXXX...........",
"................XXXXXXXXXXXXXXXXXXXX............",
"...............XXXXXXXXXXXXXXXXXXXX.............",
"..............XXXXXXXXXXXXXXXXXXXX..............",
".............XXXXXXXXXXXXXXXXXXXX...............",
"............XXXXXXXXXXXXXXXXXXXX................",
"...........XXXXXXXXXXXXXXXXXXXX.................",
"..........XXXXXXXXXXXXXXXXXXXX.................."
];
console.log(";!knitout-2");
console.log(";;Carriers: 1 2 3 4 5 6 7 8 9 10");
console.log('x-stitch-number 70');
const Width = Pattern[0].length;
let carrierAt = [];
for (let i = 0; i < Carriers.length; ++i) {
carrierAt.push(NaN);
}
//used to drop the tuck-pattern after carrier brought in:
let toDrop = [];
function bringCarrier(index, n) {
if (!(carrierAt[index] === carrierAt[index])) {
//if carrierAt is NaN, carrier needs to be brought in:
console.log('inhook ' + Carriers[index]);
console.log('tuck - f' + (Width+6) + ' ' + Carriers[index]);
console.log('tuck - f' + (Width+4) + ' ' + Carriers[index]);
console.log('tuck - f' + (Width+2) + ' ' + Carriers[index]);
console.log('tuck + f' + (Width+1) + ' ' + Carriers[index]);
console.log('tuck + f' + (Width+3) + ' ' + Carriers[index]);
console.log('tuck + f' + (Width+5) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+6) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+5) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+4) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+3) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+2) + ' ' + Carriers[index]);
console.log('knit - f' + (Width+1) + ' ' + Carriers[index]);
toDrop.push('f' + (Width+1));
toDrop.push('f' + (Width+2));
toDrop.push('f' + (Width+3));
toDrop.push('f' + (Width+4));
toDrop.push('f' + (Width+5));
toDrop.push('f' + (Width+6));
console.log('releasehook ' + Carriers[index]);
carrierAt[index] = Width+1;
}
const MaxDis = 4; //maximum distance between tucks that we'll allow
const TuckAlignment = index; //use different tuck offset among skipped needles
if (carrierAt[index] > n + MaxDis) {
let minTuck = n + 1;
while ((minTuck - TuckAlignment) % MaxDis != 0) ++minTuck;
let maxTuck = carrierAt[index] - 1;
while ((maxTuck - TuckAlignment) % MaxDis != 0) --maxTuck;
for (let i = maxTuck; i >= minTuck; i -= MaxDis) {
carrierAt[index] = i;
console.log('tuck - f' + carrierAt[index] + ' ' + Carriers[index]);
}
}
if (carrierAt[index] < n - MaxDis) {
let minTuck = carrierAt[index] + 1;
while ((minTuck - TuckAlignment) % MaxDis != 0) ++minTuck;
let maxTuck = n - 1;
while ((maxTuck - TuckAlignment) % MaxDis != 0) --maxTuck;
for (let i = minTuck; i <= maxTuck; i += MaxDis) {
carrierAt[index] = i;
console.log('tuck + f' + carrierAt[index] + ' ' + Carriers[index]);
}
}
}
function doDrops() {
toDrop.forEach(function(bn){
console.log('drop ' + bn);
});
toDrop = [];
}
//Basic alternating tucks cast-on:
bringCarrier(0,Width-1);
for (let n = Width-1; n >= 0; --n) {
if ((Width-1 - n) % 2 === 0) {
console.log('knit - f' + n + ' ' + Carriers[0]);
}
}
for (let n = 0; n <= Width-1; ++n) {
if ((Width-1 - n) % 2 !== 0) {
console.log('knit + f' + n + ' ' + Carriers[0]);
}
}
doDrops();
//'dir' will track the direction of the next pass:
let dir = '-';
//a few starting rows with Carriers[0]:
for (let r = 0; r < 8; ++r) {
if (dir === '+') {
for (let n = 0; n < Width; ++n) {
console.log('knit + ' + 'f' + n + ' ' + Carriers[0]);
}
carrierAt[0] = Width - 1;
dir = '-';
} else { //dir === '-'
for (let n = Width-1; n >= 0; --n) {
console.log('knit - ' + 'f' + n + ' ' + Carriers[0]);
}
carrierAt[0] = 0;
dir = '+';
}
}
//Actual intarsia pattern:
for (let rowIndex = Pattern.length - 1; rowIndex >= 0; --rowIndex) {
const row = Pattern[rowIndex];
const segments = [];
for (let begin = 0; begin < row.length; /* later */) {
let end = begin;
while (end < row.length && row[end] === row[begin]) ++end;
segments.push([begin, end]);
begin = end;
}
console.assert(segments.length <= Carriers.length);
if (dir === '+') {
for (let index = segments.length - 1; index >= 0; --index) {
const [begin, end] = segments[index];
bringCarrier(index, begin);
for (let n = begin; n < end; ++n) {
console.log('knit + ' + 'f' + n + ' ' + Carriers[index]);
}
carrierAt[index] = end - 1;
if (end < Width) {
console.log('tuck - ' + 'f' + end + ' ' + Carriers[index]);
carrierAt[index] = end;
}
doDrops();
}
dir = '-';
} else { //dir === '-'
for (let index = 0; index < segments.length; ++index) {
const [begin, end] = segments[index];
bringCarrier(index, end - 1);
for (let n = end - 1; n >= begin; --n) {
console.log('knit - ' + 'f' + n + ' ' + Carriers[index]);
}
carrierAt[index] = begin;
if (begin > 0) {
console.log('tuck + ' + 'f' + (begin-1) + ' ' + Carriers[index]);
carrierAt[index] = begin-1;
}
doDrops();
}
dir = '+';
}
}
//finish up: (assumes that Carriers[0] is in)
for (let index = 1; index < Carriers.length; ++index) {
if (carrierAt[index] === carrierAt[index]) {
console.log('outhook ' + Carriers[index]);
carrierAt[index] = NaN;
}
}
//a few final rows with Carriers[0]:
if (dir === '+') bringCarrier(0,0);
if (dir === '-') bringCarrier(0,Width-1);
for (let r = 0; r < 8; ++r) {
if (dir === '+') {
for (let n = 0; n < Width; ++n) {
console.log('knit + ' + 'f' + n + ' ' + Carriers[0]);
}
dir = '-';
} else { //dir === '-'
for (let n = Width-1; n >= 0; --n) {
console.log('knit - ' + 'f' + n + ' ' + Carriers[0]);
}
dir = '+';
}
}
console.log('outhook ' + Carriers[0]);
//drop:
for (let n = 0; n < Width; ++n) {
console.log('drop ' + 'f' + n);
}