Home > 1b > | ノート > 1b-03 関数を定義し利用する

1b-03 関数を定義し利用する

よく利用する処理は「関数」を利用することで、見渡しの良いプログラムにすることができる。

矩形を2つ描く

1b_03_1

void setup() {
  size(100, 100);
  colorMode(HSB, 100);
  background(0);
  noLoop(); // draw関数を繰り返さない
}

void draw() {
  // 左に赤の矩形
  fill(0, 99, 99);
  rect(20, 40, 20, 20);
  // 右にオレンジの矩形
  fill(10, 99, 99);
  rect(60, 40, 20, 20);
}

矩形を2つ描く(関数を使って)

void setup() {
  size(100, 100);
  colorMode(HSB, 100);
  background(0);
  noLoop();
}

void draw() {
  drawBox(20, 40, 0); // 左に赤の矩形
  drawBox(60, 40, 10); // 右にオレンジの矩形
}

void drawBox(float x, float y, int h) {
  fill(h, 99, 99);
  rect(x, y, 20, 20);
}

練習 - 上記(関数を使った方)のプログラムを改変してみる

  1. 黄色の矩形を追加し、矩形を3個にする。
  2. 矩形のサイズを、一辺15pxの正方形に変更する。
  3. 矩形を、直径が20pxの円に変更する。関数名も変更する。
  4. 幅400px高さ400pxのウィンドウに、任意のイラストを3つ表示する。
  5. 4で描いたイラストを動かしてみる。

Home > 1b > | ノート > 1b-03 関数を定義し利用する

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