Home > 1a > | ノート > 1a-05 繰り返しとランダム

1a-05 繰り返しとランダム

  • Posted by: masa
  • 2009年5月18日 14:40
  • 1a | ノート
  • 処理を繰り返す
  • 繰り返しを繰り返す
  • ランダムに決める

for 処理を繰り返す

05-3

(この条件) を満たす間は繰り返し {この処理の実行する}

size(400,200);

int x=100;
int y=100;
int d=60;

for(int i=1; i<=3; i++){
  ellipse(x+100*i,y,d,d);
}

※前回の授業では以下のように書いていた。 その1

size(400,200);

int x=100;
int y=100;
int d=60;

ellipse(x,y,d,d);

x+=100;
ellipse(x,y,d,d);

x+=100;
ellipse(x,y,d,d);

※前回の授業では以下のように書いていた。 その2

size(400,200);

ellipse(100,100,60,60);
ellipse(200,100,60,60);
ellipse(300,100,60,60);

練習

  • 円を5つ並べてみる
  • 一定の大きさで小さくなる円を3つ描く

繰り返しを繰り返す

for1-1

size(200,200);
colorMode(HSB,100);
background(99);
noStroke();

for(int x=0; x<10; x++){
  fill(x*10,10,99);
  rect(x*20,0,10,10);
}

for文の中にfor文を書くことができる

for1-2

size(200,200);
colorMode(HSB,100);
background(99);
noStroke();

for(int y=0; y<10; y++){
  for(int x=0; x<10; x++){
    fill(10*x,10+y*10,99);
    rect(x*20,y*20,10,10);
  }
}

random ランダムに決める

random1-1 random1-2 random1-3

size(200,200);
colorMode(RGB,100);
background(99);

for(int i=0; i<100; i++){
  stroke(0);
  line(random(width),random(height),random(width),random(height));
}

※プログラムが実行されるたびに、出力結果が変わる。

色もランダムにしてみる

random2-1 random2-2 random2-3

size(200,200);
colorMode(RGB,100);
background(99);

for(int i=0; i<100; i++){
  stroke(random(100),random(100),random(100));
  line(random(width),random(height),random(width),random(height));
}

ランダムな値(乱数)を変数に入れて利用する

random3-1

size(200,200);
colorMode(HSB,100);
background(99);

for(int x=0; x<width; x++){
  float color1 = random(100); // 色を設定する乱数の値は0から100未満とする
  stroke(color1,60,99);
  line(x,0,x,height);
}

※randomはfloat型で変数を設定する(int型にするとエラーになる)。

Home > 1a > | ノート > 1a-05 繰り返しとランダム

Search
Feeds
Textbook
  • Built with Processing [改訂版]
Recommends
  • Processing – A Programming Handbook for Visual Designers and Artists
  • Learning Processing: A Beginner's Guide to Programming Images, Animation, and Interaction (Morgan Kaufmann Series in Computer Graphics)
  • Arduinoをはじめよう
  • ビジュアライジング・データ ―Processingによる情報視覚化手法
  • +GAINER―PHYSICAL COMPUTING WITH GAINER

Return to page top