-
Notifications
You must be signed in to change notification settings - Fork 5
/
mosaic.js
executable file
·138 lines (121 loc) · 3.63 KB
/
mosaic.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
#!/usr/bin/env node
//"garter stitch mosaic", in the style of:
// https://blog.weareknitters.com/knitting-stitches/how-to-knit-color-mosaics-in-garter-stitch/
//example mosaic from:
//https://chi2019.acm.org/2019/04/04/chi-2019-cowl-knitting-pattern/
const mosaic = [
".#..#..#####.#..#.",
".......#...#......",
".#..#..#.#.#.#..#.",
".......#..........",
".#.###########..#.",
"...#...#.....#....",
".#.#.#.#.#.#.#.###",
"...#...#.....#...#",
".#.###.#.#.#.#.#.#",
".......#.....#...#",
".#..#..###########",
".............#....",
".#..#..#.#.#.#..#.",
".........#...#....",
".#..#..#.#####..#.",
];
//check mosaic:
mosaic.forEach(function(line){
console.assert(line.length === mosaic[0].length, "mosaic should have lines of the same length");
console.assert(/^[.#]*$/.test(line), "mosaic lines should have only '.' or '#'");
});
let min = 1;
let max = min + mosaic[0].length - 1;
let CarrierA = 3;
let CarrierB = 6;
console.log(";!knitout-2")
console.log(";;Carriers: 1 2 3 4 5 6 7 8 9 10")
console.log("inhook " + CarrierA);
//cast on (alternating tucks):
console.log("x-stitch-number " + 68); //looser cast-on
for (let n = max; n >= min; --n) {
if ((max-n) % 2 == 0) {
console.log("tuck - f" + n + " " + CarrierA);
}
}
for (let n = min; n <= max; ++n) {
if ((max-n) % 2 == 1) {
console.log("tuck + f" + n + " " + CarrierA);
}
}
console.log("x-stitch-number " + 63); //standard stitch
//some plain knitting to start:
for (let n = max; n >= min; --n) {
console.log("knit - f" + n + " " + CarrierA);
}
for (let n = min; n <= max; ++n) {
console.log("knit + f" + n + " " + CarrierA);
}
console.log("releasehook " + CarrierA);
for (let n = max; n >= min; --n) {
console.log("knit - f" + n + " " + CarrierA);
}
//bring in carrier B and do some plain knitting as well:
console.log("inhook " + CarrierB);
for (let n = max; n >= min; --n) {
console.log("knit - f" + n + " " + CarrierB);
}
for (let n = min; n <= max; ++n) {
console.log("knit + f" + n + " " + CarrierB);
}
console.log("releasehook " + CarrierB);
for (let n = max; n >= min; --n) {
console.log("knit - f" + n + " " + CarrierB);
}
//now knit the mosaic:
for (let m = mosaic.length-1; m >= 0; --m) {
let row = mosaic[m];
console.assert(max - min + 1 === row.length, "Mosaic row should be long enough.");
const Carrier = ((m % 2) == 0 ? CarrierA : CarrierB);
const Symbol = ((m % 2) == 0 ? '#' : '.');
//front-bed knit everything marked with the current symbol:
for (let n = min; n <= max; ++n) {
if (row[n-min] === Symbol) {
console.log("knit + f" + n + " " + Carrier);
} else if (n === max) {
//make sure carrier is out of the way:
console.log("miss + f" + n + " " + Carrier);
}
}
//now back-bed knit everything marked with the current symbol:
//(a) xfer to back:
for (let n = min; n <= max; ++n) {
if (row[n-min] === Symbol) {
console.log("xfer f" + n + " " + "b" + n + " ;xfer '" + Symbol + "'");
}
}
//(b) knit:
for (let n = max; n >= min; --n) {
if (row[n-min] === Symbol) {
console.log("knit - b" + n + " " + Carrier);
} else if (n === min) {
//make sure carrier is out of the way:
console.log("miss - b" + n + " " + Carrier);
}
}
//(c) xfer to front:
for (let n = max; n >= min; --n) {
if (row[n-min] === Symbol) {
console.log("xfer b" + n + " " + "f" + n + " ;return '" + Symbol + "'");
}
}
}
//a few plain rows, get carriers out:
for (let n = min; n <= max; ++n) {
console.log("knit + f" + n + " " + CarrierA);
}
console.log("outhook " + CarrierA);
for (let n = min; n <= max; ++n) {
console.log("knit + f" + n + " " + CarrierB);
}
console.log("outhook " + CarrierB);
//drop:
for (let n = min; n <= max; ++n) {
console.log("drop f" + n);
}