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

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

Processingで紙ふぶきを描く

よくイラストで飛んでる三角形です。
色数多すぎたかも・・・完全に形までランダムだといびつになったりするので、欲しい三角形の形を拡大縮小と回転させたほうがよさげです。

f:id:prince9:20170824041659p:plain

int tx,ty;
//三角形の数
  int triangleCount = 20;
  
//最大個数分、または色のパターンの数配列を用意する
 int[] colt = new int[triangleCount];
 
 color[] colarray = 
{ 
#263716,#f16343,#9774c7,#7b6c30,#f32cd5,#e4dfb2,#17cfc8,#8bac33,#315d21,#eee9e6,#e8cb37,#f16bb4,#92cedc,#2a45e7,#720e29,#f20943,#ffffff
};

void setup() {
  size(600,600);
   background(255);
   noLoop();
    smooth(); 
}

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



  
  //0から600の間で、triangleCountの個数分ランダムに描く。実際はtranslateでランダムに移動させている
 for (int n = 0; n < triangleCount; n++) {
   noStroke();
fill(colarray[colt[n]]);
   tx = int(random(600));
  ty = int(random(600));
  sankakuR();
}

 
 
}

void sankakuR() {
  pushMatrix();
  translate(tx,ty);
  //拡大縮小
  scale(random(0.5,3.0));
  //回転
  rotate(radians(random(359)));
  
  triangle(29,36,11,62,43,56);
   popMatrix();
  
  
 }

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