Instantly share code, notes, and snippets.

@lealbaugh lealbaugh/glotch.js
Created Feb 2, 2019

Embed
What would you like to do?
var knitoutWriter = require('../knitout-frontend-js/knitoutWriter');
k = new knitoutWriter();
k.addHeader('Machine','SWGXYZ');
k.addHeader('Gauge','15');
k.addHeader('Presser','On');
k.addHeader('X-Presser','On');
k.stitchNumber(85);
var yarn = 3;
var gauge = "half";
glove = {leftedge: 0, rightedge: 0};
var yarnIsIn = false;
fingerQuantityMinimum = 5;
fingerQuantityMaximum = 10;
fingerWidthMinimum = 6;
fingerWidthMaximum = 8;
fingerHeightMinimum = 20;
fingerHeightMaximum = 100;
thumbChance = 1/4;
var numFingers = randomBetween(fingerQuantityMinimum, fingerQuantityMaximum);
for (var f = 0; f<numFingers; f++) {
var fingerWidth = randomBetween(fingerWidthMinimum, fingerWidthMaximum);
var fingerHeight = randomBetween(fingerHeightMinimum, fingerHeightMaximum);
if (f>0 && Math.random() < thumbChance) { // can't add a thumb to a null palm
glove = addThumb(glove, fingerWidth, fingerHeight, yarn);
}
else {
glove = addFinger(glove, fingerWidth, fingerHeight, yarn);
}
}
glove = addCuff(glove, yarn);
k.write('glotch.k');
// ============================================================================
// ============ High-level ====================================================
// ============================================================================
function addFinger (confines, width, height, carrier) {
// finger adds a finger to a right side of the glove
var leftedge = (confines.rightedge - confines.leftedge > 0) ? confines.rightedge + 1 : confines.rightedge;
var finger = {leftedge: leftedge, rightedge: leftedge+width};
console.log("adding finger: ", finger);
// breaks yarn if necessary
if (yarnIsIn) {
k.outhook(yarn);
yarnIsIn = false;
}
// casts on
caston("allneedle", finger, carrier);
// shortrows for roundedness
fingertip(finger, carrier);
// knits the height of the tube
knitRows(finger, height, carrier);
// transfers by one stitch onto the finger next to it
if (confines.rightedge - confines.leftedge > 0) {
transferChunk(finger, -1);
finger.rightedge = finger.rightedge - 1;
}
confines.rightedge = finger.rightedge;
// (if there is one -- that is, if incoming confines is not zero width)
var palmHeight = randomBetween(1,2);
knitRows(confines, palmHeight, carrier);
// and does a course or two of palm pickup
return confines;
}
function addThumb (confines, width, height, carrier) {
// a thumb rotates the confines for a handful of courses, then knits a new finger, joins, and narrows back the width of the thumb
console.log("thumb!");
confines = rotate(confines, 3, carrier);
var originalRightedge = confines.rightedge;
confines = addFinger(confines, width, height, carrier);
for (var i = 0; i<width; i++) {
knitRows(confines, 1, carrier);
transferChunk({leftedge: originalRightedge + 1, rightedge: confines.rightedge}, -1);
confines.rightedge = confines.rightedge - 1;
}
knitRows(confines, 1, carrier);
return confines;
}
function addCuff (confines, carrier) {
// a cuff adds courses of 2x2 ribbing, then binds off
// height of courses is between 1/3 and 1/2 the width of the confines
var width = confines.rightedge - confines.leftedge;
var height = randomBetween(width/2, width);
knitRows(confines, height, carrier);
knitRibRows(confines, height, carrier);
bindoff("open", confines, carrier);
return {leftedge:0,rightedge:0};
}
function fingertip (confines, carrier) {
function shortRows (bed) {
var setback = 1;
// shortrow a nice round fingertip
for (var s = confines.rightedge; s>confines.leftedge+((((confines.rightedge - confines.leftedge)/(2*setback))-1)*setback); s--) {
knit("-", bed, s, carrier);
}
for (var i = Math.round((confines.rightedge - confines.leftedge)/(2*setback)); i>0; i--) {
for (var s = Math.round(confines.rightedge - (i*setback) - 1); s>confines.leftedge+((i-1)*setback); s--) {
knit("-", bed, s, carrier);
}
tuck("+", bed, Math.round(confines.leftedge+((i-1)*setback)), carrier);
for (var s = Math.round(confines.leftedge+((i-1)*setback) + 1); s <= confines.rightedge - ((i)*setback); s++) {
knit("+", bed, s, carrier);
}
if (i>1) tuck("-", bed, Math.round(confines.rightedge - ((i - 1)*setback)), carrier);
else {
for (var s = Math.round(confines.rightedge - ((i - 1)*setback)); s<=confines.rightedge; s++) {
knit("+", bed, s, carrier);
}
}
}
}
shortRows("f");
shortRows("b");
}
// ============================================================================
// ============ Mid-level =====================================================
// ============================================================================
function knitRows (confines, height, carrier) {
for (var h=0; h<height; h++) {
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
// console.log(h, s);
knit("-", "f", s, carrier);
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
// console.log(h, s);
knit("+", "b", s, carrier);
}
}
}
function knitRibRows (confines, height, carrier) {
function ribGen (face, stitch) {
if (face == "f"){
return (s%4 <= 1) ? "k" : "p";
}
else {
return (s%4 <= 1) ? "p" : "k";
}
}
for (var h=0; h<height; h++) {
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
if (ribGen("f", s) != "k") xfer("f", s, "bs", s);
}
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
var bed = (ribGen("f", s) == "k") ? "f" : "bs";
knit("-", bed, s, carrier);
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
if (ribGen("f", s) != "k") xfer("bs", s, "f", s);
if (ribGen("b", s) != "k") xfer("b", s, "fs", s);
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
var bed = (ribGen("b", s) == "k") ? "b" : "fs";
knit("+", bed, s, carrier);
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
if (ribGen("b", s) != "k") xfer("fs", s, "b", s);
}
}
}
function rotate (confines, height, carrier) {
for (var h=0; h<height; h++) {
// knit to one before the edge
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
// console.log(h, s);
knit("-", "f", s, carrier);
}
for (var s=confines.leftedge; s<confines.rightedge; s++) {
// console.log(h, s);
knit("+", "b", s, carrier);
}
// xfer
xfer("f", confines.leftedge, "b", confines.leftedge - 1);
xfer("b", confines.rightedge, "f", confines.rightedge + 1);
for (var s = confines.leftedge + 1; s<=confines.rightedge + 1; s++) {
xfer("f", s, "bs", s-1);
}
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("bs", s, "f", s);
}
for (var s = confines.leftedge - 1; s<=confines.rightedge - 1; s++) {
xfer("b", s, "fs", s+1);
}
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("fs", s, "b", s);
}
align("centered");
}
knitRows(confines, 1, carrier);
return confines;
}
function transferChunk (confines, offset) {
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("f", s, "bs", s);
}
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("bs", s, "f", s+offset);
}
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("b", s, "fs", s+offset);
}
for (var s = confines.leftedge; s<=confines.rightedge; s++) {
xfer("fs", s+offset, "b", s+offset);
}
}
function pickup (confines, yarn) {
k.inhook(yarn);
yarnIsIn = true;
knitRows(confines, 1, yarn);
k.releasehook(yarn);
}
function caston (style, confines, carrier) {
k.inhook(yarn);
yarnIsIn = true;
if (style == "tube") {
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
if (s%2 == confines.rightedge%2) {
knit("-", "f", s, carrier);
}
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
if (s%2 != confines.rightedge%2) {
knit("+", "b", s, carrier);
}
}
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
if (s%2 != confines.rightedge%2) {
knit("-", "f", s, carrier);
}
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
if (s%2 == confines.rightedge%2) {
knit("+", "b", s, carrier);
}
}
}
else if (style == "allneedle") {
align(-1);
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
knit("-", "f", s, carrier);
knit("-", "b", s, carrier);
}
align("centered");
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
knit("+", "f", s, carrier);
knit("+", "b", s, carrier);
}
}
k.releasehook(yarn);
}
function bindoff (style, confines, carrier) {
if (style == "closed") {
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
knit("-", "b", s, carrier);
xfer("b", s, "f", s);
knit("-", "f", s, carrier);
if (s != confines.leftedge) xfer("f", s, "b", s - 1, "centered");
}
var tag = makeTag(confines.leftedge, carrier);
k.outhook(yarn);
yarnIsIn = false;
dropAll(tag);
}
else if (style == "open") {
for (var s=confines.rightedge; s>confines.leftedge; s--) {
knit("-", "f", s, carrier);
xfer("f", s, "bs", s);
xfer("bs", s, "f", s-1);
}
knit("-", "f", confines.leftedge, carrier);
xfer("f", confines.leftedge, "b", confines.leftedge);
for (var s=confines.leftedge; s<confines.rightedge; s++) {
knit("+", "b", s, carrier);
xfer("b", s, "fs", s);
xfer("fs", s, "b", s+1);
}
knit("+", "b", confines.rightedge, carrier);
xfer("b", confines.rightedge, "f", confines.rightedge);
align("centered");
var tag = makeTag(confines.rightedge, carrier);
k.outhook(yarn);
yarnIsIn = false;
dropAll(tag);
}
else {
k.outhook(carrier);
yarnIsIn = false;
dropAll(confines);
}
}
function makeTag (s, carrier) {
knit("+", "f", s, carrier);
knit("-", "f", s, carrier);
knit("+", "f", s, carrier);
knit("-", "f", s+1, carrier);
knit("-", "f", s, carrier);
knit("+", "f", s, carrier);
knit("+", "f", s+1, carrier);
for (var i=0; i<3; i++) {
knit("-", "f", s+2, carrier);
knit("-", "f", s+1, carrier);
knit("-", "f", s, carrier);
knit("+", "f", s, carrier);
knit("+", "f", s+1, carrier);
knit("+", "f", s+2, carrier);
}
return {leftedge: s, rightedge: s+2};
}
function dropAll (confines) {
for (var s=confines.rightedge; s>=confines.leftedge; s--) {
drop("f", s);
}
for (var s=confines.leftedge; s<=confines.rightedge; s++) {
drop("b", s);
}
}
// ============================================================================
// ============ Low-level =====================================================
// ============================================================================
function randomBetween (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function mapNeedle (bed, needle) {
var mappedBed, mappedNeedle;
if (gauge == "full") {
mappedBed = bed;
mappedNeedle = needle;
}
else if (gauge == "half") {
mappedBed = (bed == "f" || bed == "fs") ? "f" : "b";
mappedNeedle = (bed == "f" || bed == "bs") ? (2*needle) - 1 : 2*needle;
}
// console.log({bed: mappedBed, needle: mappedNeedle});
return {bed: mappedBed, needle: mappedNeedle};
}
function knit (direction, bed, needle, carrier) {
var mappedNeedle = mapNeedle(bed, needle);
k.knit(direction, mappedNeedle.bed + mappedNeedle.needle, carrier);
}
function miss (direction, bed, needle, carrier) {
var mappedNeedle = mapNeedle(bed, needle);
k.miss(direction, mappedNeedle.bed + mappedNeedle.needle, carrier);
}
function tuck (direction, bed, needle, carrier) {
var mappedNeedle = mapNeedle(bed, needle);
k.tuck(direction, mappedNeedle.bed + mappedNeedle.needle, carrier);
}
function drop (bed, needle) {
var mappedNeedle = mapNeedle(bed, needle);
k.drop(mappedNeedle.bed + mappedNeedle.needle);
}
function align (alignment) {
if (alignment == "centered") {
if (gauge == "full") {
k.rack(-0.75);
}
else {
k.rack(0);
}
}
else if (typeof alignment == "number") {
if (gauge == "full") {
k.rack(alignment);
}
else {
k.rack(2*alignment);
}
}
else {
console.log("unrecognized alignment style");
}
}
function xfer (fromBed, fromNeedle, toBed, toNeedle, returnAlignment) {
if (fromBed == toBed || (fromBed == "f" && toBed == "fs")|| (fromBed == "fs" && toBed == "f")|| (fromBed == "b" && toBed == "bs")|| (fromBed == "bs" && toBed == "b")) {
console.log("cannot xfer to same bed! attempted: ", fromBed, fromNeedle, toBed, toNeedle);
}
else {
mappedFrom = mapNeedle(fromBed, fromNeedle);
mappedTo = mapNeedle(toBed, toNeedle);
offset = mappedTo.needle - mappedFrom.needle;
if (fromBed == "f" || fromBed == "fs") offset = 0 - offset;
k.rack(offset);
k.xfer(mappedFrom.bed+mappedFrom.needle, mappedTo.bed+mappedTo.needle);
if (typeof returnAlignment !== 'undefined') align(returnAlignment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment