0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

トリボナッチ・パターン

Posted at

image.png

トリボナッチ・パターン

トリボナッチ数列に基づき、非周期パターンを生成。

フィボナッチ数列

F_0 = 1
F_n = F_{n-1} + F_{n-2}
0 1 2 3 4 5 6 7 8 9 10 11
1 1 2 3 5 8 13 21 34 55 89 144

トリボナッチ数列

T_0 = 1
T_n = T_{n-1} + T_{n-2} + T_{n-3}
0 1 2 3 4 5 6 7 8 9 10 11
0 0 1 1 2 4 7 13 24 44 81 149
1 1 1 3 5 9 17 31 57 105 193 355

0開始と、1開始がある。

プログラム

String[] T = new String[20];
String patternX, patternY;
int cellSize = 8;  // 細かくする

color colA = color(230, 100, 100);  // a = 赤系
color colB = color(100, 230, 100);  // b = 緑系
color colC = color(100, 100, 230);  // c = 青系

void setup() {
  size(800, 800);
  generateTribonacci(14);    // n = 13 まで含むように生成
  patternX = T[13];           // 横糸(長さ:約2300以上)
  patternY = T[12];           // 縦糸

  int cols = width / cellSize;
  int rows = height / cellSize;

  println("織物サイズ: " + cols + "列 × " + rows + "行");

  noStroke();
  for (int y = 0; y < rows; y++) {
    char chY = patternY.charAt(y % patternY.length());
    color cy = getColorFromChar(chY);
    for (int x = 0; x < cols; x++) {
      char chX = patternX.charAt(x % patternX.length());
      color cx = getColorFromChar(chX);

      // 色の混合(交差点)
      color mix = lerpColor(cx, cy, 0.5);
      fill(mix);
      rect(x * cellSize, y * cellSize, cellSize, cellSize);
    }
  }
}

color getColorFromChar(char ch) {
  if (ch == 'a') return colA;
  if (ch == 'b') return colB;
  return colC;
}

void generateTribonacci(int n) {
  T[0] = "a";
  T[1] = "b";
  T[2] = "c";
  for (int i = 3; i <= n; i++) {
    T[i] = T[i - 1] + T[i - 2] + T[i - 3];
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?