hitomezashi stitch patterns

with alternating diagonal hatching

Log in to post a comment.

const SIZE = 15; // min=10, max=200, step=1
const draw_stitches = 0; // min=0 max=1 step=1 (No, Yes)
const draw_hatches_1 = 1; // min=0 max=1 step=1 (No, Yes)
const draw_hatches_2 = 1; // min=0 max=1 step=1 (No, Yes)
const n_hatches = 5; // min=1, max=15, step=2

cell = 200 / SIZE

Canvas.setpenopacity(-1);

const t = new Turtle();

const hor = []
const vert = []
for (let i = 0; i <= SIZE; i++) {
    hor[i] = []
    vert[i] = []
    
    hor[i][0] = (Math.random() < 0.5)
    vert[i][0] = (Math.random() < 0.5)
    
    for (let j = 1; j <= SIZE; j++) {
        hor[i][j] = !(hor[i][j - 1])
        vert[i][j] = !(vert[i][j - 1]) 
    }
}

if (draw_stitches == 1) {
    for (let y = 0; y <= SIZE; y++) {
        for (let x = 0; x <= SIZE; x++) {
            if (hor[y][x]) {
                t.jump(-100 + x * cell, -100 + y * cell)
                t.goto(-100 + (x + 1) * cell, -100 + y * cell)
            }
        }
    }

    for (let x = 0; x <= SIZE; x++) {
        for (let y = 0; y <= SIZE; y++) {
            if (vert[x][y]) {
                t.jump(-100 + x * cell, -100 + y * cell)
                t.goto(-100 + x * cell, -100 + (y + 1) * cell)
            }
        }
    }
}

if (draw_hatches_1 + draw_hatches_2 > 0) {
    let mark0 = true
    
    for (let y = 0; y < SIZE; y++) {
        if (hor[y][0]) {
            mark0 = !mark0
        }
        
        mark = mark0
        
        for (let x = 0; x < SIZE; x++) {
            if (mark) {
                if (draw_hatches_1 == 1) {
                    for (let i = 1; i <= n_hatches; i++) {
                        let d =  i / (n_hatches + 1) * cell
                        let cx = -100 + x * cell + d
                        let cy = -100 + y * cell + d
                        
                        t.jump(cx, cy)
                        
                        if (i <= n_hatches / 2) {
                            t.goto(cx - d, cy + d)
                            t.goto(cx + d, cy - d)
                        }
                        else {
                             t.goto(cx - (cell - d), cy + (cell -d))
                             t.goto(cx + (cell - d), cy - (cell -d))
                        }
                    }
                }
            }
            else {
                if (draw_hatches_2 == 1) {
                    for (let i = 1; i <= n_hatches; i++) {
                        let d =  i / (n_hatches + 1) * cell
                        let cx = -100 + x * cell + d
                        let cy = -100 + (y + 1) * cell - d
                        
                        t.jump(cx, cy)
                        
                        if (i <= n_hatches / 2) {
                            t.goto(cx + d, cy + d)
                            t.goto(cx - d, cy - d)
                        }
                        else {
                             t.goto(cx + (cell - d), cy + (cell -d))
                             t.goto(cx - (cell - d), cy - (cell -d))
                        }
                    }
                }
            }
            
            if (vert[x + 1][y]) {
                mark = !mark
            }
        }
    }
}