Hitomezashi 使用 Processing 编程缝合花样

Hitomezashi Stitch patterns with Processing programming

我看了这个很棒的视频 https://www.youtube.com/watch?v=JbfhzlMk2eY&ab_channel=Numberphile 来自 Numberphile,想使用 Processing 重新创建它。

概念是这样的:用 2 个二进制数组创建随机模式,第一个可以是句子中的元音和辅音,下一个可以是长数中的偶数和奇数,例如 phi。

如何遍历我的数组,然后根据数字的结果绘制带或不带初始偏移量的线,先垂直然后水平或以其他方式?

我记下了一些,但为什么我的代码没有画出所有的垂直线?有人能解决这个有趣的小问题吗?

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

我添加了 setup() 和 draw() 并修改了绘制垂直线的第二个循环。

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;
  }
}

修改后的版本取消了 IntList 并改为使用两个 1 和 0 数组。第二个数组的 odd/even 检查也用于垂直线。演示被修改为使用线而不是矩形来缝合;因此,使用了两个线网格,一个用于水平针迹,一个用于垂直针迹。 XOffset为每行第一个水平针迹偏移的距离,yOffset为每列第一个垂直针迹偏移的距离;两者都是根据各自的数组值设置的。请注意,垂直针迹阵列 (cols) 的大小是水平针迹阵列 (rows) 的两倍,因为每个水平针迹都有两端。反之,每列的垂直针迹数是行数的一半,因为每个垂直针迹连接两个水平针迹,并且每连接的两行之间有一个跳线。

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() {
}