まゆたまガジェット開発逆引き辞典

電子工作やプログラミングのHowtoを逆引き形式で掲載しています。作りたいモノを決めて学んでいくスタイル。プログラマではないので、コードの汚さはお許しを。参照していないものに関しては、コピペ改変まったく問いません

Processingでランダムな個数、かつ特定の色の中からランダムな色で描く

図形をランダムな個数生成して、色もランダムにしたい場合です。
色は特定の色からランダムに選ばれるので、多少の重複はあります。

まずはこんな感じで1つのパーツにつき1色の場合。
f:id:prince9:20170818004753p:plain

//最大個数分、または色のパターンの数配列を用意する
 int[] col1 = new int[36];//小さい三角パーツ
//小さい三角の位置
 int triPx;
 //小さい三角形の数
 int triPcount1;
//基本の三角形の大きさ
int mosaicSacle;

//色の種類ぶんだけ配列をつくる
 color[] colarray = 
{ 
#ed621c,#e81c44,#abcf2d,#5fb734,#874195,#01449d,#45bcca,#f6c620,#898989,#ffffff
};

void setup() {
  size(600,600);
 noLoop();

 //基本の三角形の大きさ
   mosaicSacle = 20;
}

void draw() {
  background(255);
  
  //色の種類ぶんランダムな数値をつくる
 for(int c = 0; c < col1.length; c++){
  //色の乱数生成、小さい三角。ランダムの数値は色の種類の数
  col1[c] = int(random(10));
}

//三角パーツ
triMini();

}

void mousePressed() {
      redraw();// ボタンが押されたときだけ実行
}

void triMini() {

  //三角形の数
    triPcount1 = int(random(1,6));
    //ランダムに算出された数ぶんだけ三角を描写
  for (int i = 0; i < triPcount1; i++) {
        //これで1色ずつ色を変える(重複あり)
    for (int pt1col = 0; pt1col < triPcount1; pt1col++) {
fill(colarray[col1[pt1col]]);
//三角形の位置をランダムに
   triPx = int(random(1,30))*20;
   //三角形描写
  triP1();
}
}
}


void triP1() {
  pushMatrix();
  //ランダムに移動させる
  translate(triPx,100);
  noStroke();

    
    //三角形
triangle(0, 0,mosaicSacle/2, mosaicSacle/2, mosaicSacle, 0);

        popMatrix();
}


次に、下記のように1つの図形が複数のパーツで構成されている場合。
f:id:prince9:20170818013954p:plain

//最大個数分、または色のパターンの数配列を用意する
  int[] col2 = new int[25];//大さい三角パーツ
  
 //大きい三角の位置
  int triPx2;

 //大きい三角形の数
 int triPcount2;
 
 //色のパターン
 int pt2col;

  //基本の三角形の大きさ
int mosaicSacle;


//色の種類ぶんだけ配列をつくる
 color[] colarray = 
{ 
#ed621c,#e81c44,#abcf2d,#5fb734,#874195,#01449d,#45bcca,#f6c620,#898989,#ffffff
};

void setup() {
  size(600,600);
 noLoop();

 //基本の三角形の大きさ
   mosaicSacle = 20;
}

void draw() {
  background(255);
  
   for(int c2 = 0; c2 < col2.length; c2++){
  //色の乱数生成、大きい三角。ランダムの数値は色の種類の数
  col2[c2] = int(random(10));
}


//三角パーツ大
triBig();
}

void mousePressed() {
      redraw();// ボタンが押されたときだけ再描画
}

//図形の数や位置を設定
void triBig() {
 //三角形の数
    triPcount2 = int(random(1,6));
     //ランダムに算出された数ぶんだけ三角を描写
  for (int i2 = 0; i2 < triPcount2; i2++) {
     //これで1色ずつ色を変える(重複あり)
    for (pt2col = 0; pt2col < triPcount2; pt2col++) {
    //三角形の位置をランダムに
   triPx2 = int(random(1,15))*40;
   //三角形描写
 triBigpt();
}
}
}

//実際の図形の設定
void triBigpt() {
  pushMatrix();
  translate(triPx2,300);
  noStroke();
  
 
  fill(colarray[col2[pt2col]]);
  triangle(mosaicSacle/2, mosaicSacle/2,  0, mosaicSacle, mosaicSacle, mosaicSacle);
  fill(colarray[col2[pt2col+1]]);
  triangle(mosaicSacle, 0,  mosaicSacle/2,mosaicSacle/2 , mosaicSacle, mosaicSacle);
  fill(colarray[col2[pt2col+2]]);
  triangle(mosaicSacle, 0,  mosaicSacle+10, mosaicSacle/2, mosaicSacle, mosaicSacle);
   fill(colarray[col2[pt2col+3]]);
  triangle(mosaicSacle+10,mosaicSacle/2,mosaicSacle,mosaicSacle,mosaicSacle*2,mosaicSacle);

  popMatrix();
}


上記のものとこちらの「ランダムに図形を描く」
prince9.hatenablog.com
を組み合わせると、こんな感じのものもできます。コードは複雑すぎるのでまたの機会に。
f:id:prince9:20170818023306p:plain