Home > other >  Hitimezashi Stich patterns with Processing programming
Hitimezashi Stich patterns with Processing programming

Time:12-25

I watched this great video https://www.youtube.com/watch?v=JbfhzlMk2eY&ab_channel=Numberphile from numberphile and wanted to recreate it using Processing. The concept is this: Creating random patterns with 2 arrays of binary numbers, first set could be the vowels and consonants in a sentence next one could be even and uneven numbers in a long number like phi.

So my question is this, how do i iterate through my array and then draw the lines with or without an initial offset based on the outcome of the number, first vertical then horizontal or the otherway. I got some of it down, but why does my code not draw all the vertical lines? anyone has a take on this fun little problem?

fill(0);

int y = 0;
int x = 0;


size(500, 500);
background(102);
noStroke();

int name[] = {1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,1,0};

int pi[]   = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,3,2,3};
IntList  oddEven;
oddEven = new IntList();


for (int i = 0; i < pi.length; i  ) {
     if (pi[i] % 2 == 0) {
         oddEven.append(0); 
         }
         else {
            oddEven.append(1);
         }
          
}


for(int g = 0; g < 17; g  ) {

  for(int l = 0; l < 17; l  ) {
        
        rect(x, y, 25, 1); 
        x =50;

  }
  if (oddEven.get(g) % 2 == 0)
  {
   x=25;
   y =25;
  }
  else{
    x= 0;
    y =25;
  }
}  
  





for(int xxx = 0; xxx < 14; xxx  ) {

  for(int l = 0; l < 3; l  ) {
          
        rect(x, y, 1, 25); 
        y =50;

  }
  if (oddEven.get(xxx) % 2 == 0)
  {
   x=25;
   y =25;
  }
  else{
    y= 0;
    x =25;
  }
}  


  [1]: https://www.youtube.com/watch?v=JbfhzlMk2eY&ab_channel=Numberphile

CodePudding user response:

I added setup() and draw() and revised the second loop which draws the vertical lines.

int y = 0;
int x = 0;
IntList  oddEven;
int name[] = {1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0};
int pi[]   = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 3, 2, 3};

void setup() {
  size(850, 425);
  background(102);
  //noStroke();
  fill(0);
  oddEven = new IntList();

  for (int i = 0; i < pi.length; i  ) {
    if (pi[i] % 2 == 0) {
      oddEven.append(0);
    } else {
      oddEven.append(1);
    }
  }
}

void draw() {
  // Horizontal lines
  for (int m = 0; m < name.length; m  ) {
    for (int n = 0; n < name.length; n  ) {
      rect(x, y, 25, 1); 
      x =50;
    }
    if (oddEven.get(m) % 2 == 0) {
      x=25;
      y =25;
    } else {
      x= 0;
      y =25;
    }
  }  

  // Vertical lines
  for (int a = 0; a < 34; a  ) {
    for (int b = 0; b < 9; b  ) {
      rect(x, y, 1, 25); 
      y =50;
    }
    x =25;
    y= 0;
  }
}

Revised version which does away with the IntList and uses two arrays of ones and zeroes instead. Also an odd/even check of the second array is used for the vertical lines. The demo was modified to use lines instead of rectangles for the stitches; therefore, two line grids were used, one for the horizontal stitches and one for the vertical stitches. XOffset is the distance that the first horizontal stitch in each row is offset and yOffset is the distance that the first vertical stitch in each column is offset; both are set based on respective array values. Note that the vertical stitch array (cols) is twice the size of the horizontal stitch array (rows) since there are two ends to each horizontal stitch. Conversely, the number of vertical stitches in each column is one half the number of rows since each vertical stitch connects two horizontal stitches and there is a skip between each two rows connected.

final int _horzRows = 17;
final int _horzCols = 17;
final int _vertRows = 8;
final int _vertCols = 34;

int rows[] = {1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0};
int cols[] = {1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0};

int x = 0;
int y = 0;
int xOffset = 0;
int yOffset = 0;

void horzGrid(int l, int t, int stitchLength) {
  for (int j = 0; j < _horzCols; j  ) {
    for (int k = 0; k < _horzRows; k  ) {
      x = l   j*(stitchLength*2); // stitch   skip
      y = t   k*(stitchLength);
      if (rows[k] == 1) {
        xOffset = stitchLength;
      } else {
        xOffset = 0;
      }
      line(x xOffset, y, x xOffset   stitchLength, y);
    }
  }
}

void vertGrid(int l, int t, int stitchLength) {
  for (int m = 0; m < _vertCols; m  ) {
    for (int n = 0; n < _vertRows; n  ) {
      x = l   m*(stitchLength);
      y = t   n*(stitchLength*2); // stitch   skip
      if (cols[m] == 1) {
        yOffset = stitchLength;
      } else {
        yOffset = 0;
      }
      line(x, y yOffset, x, y yOffset   stitchLength);
    }
  }
}

void setup() { 
  size(920, 480);
  background(255);
  strokeWeight(2);
  horzGrid(30, 40, 25);
  vertGrid(30, 40, 25);
}

void draw() {
}

  • Related