Successfully reported this slideshow.
Your SlideShare is downloading. ×

Processingによるプログラミング入門 第6回

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 37 Ad

More Related Content

Slideshows for you (20)

Advertisement

Similar to Processingによるプログラミング入門 第6回 (20)

Advertisement

Recently uploaded (20)

Processingによるプログラミング入門 第6回

  1. 1. 早稲田大学 基幹理工学部 表現工学科 長研究室 B4 鈴木 遼 31 July 2013
  2. 2. 今日やること  Processing とプログラミングの基礎  今日は 12 項目
  3. 3. 1. 関数とは 入力 機械 出力
  4. 4. 1. 関数とは 引数 関数 戻り値
  5. 5. 1. 関数とは 引数 関数 戻り値 abs()-3.5 3.5 sqrt()16.0 4.0
  6. 6. 2. 関数の作り方 戻り値の型 名前(引数の型 引数名) { 何らかの処理 return 戻り値 ; }
  7. 7. 2. 関数の作り方  与えられた数を二乗する Square() 関数を作る float Square(float x) { return x*x; } 戻り値の型 名前(引数の型 引数名) { 何らかの処理 return 戻り値 ; }
  8. 8. 2. 関数の作り方 float Square(float x) { return x*x; } void setup() { float s = Square(12); println(s); } void draw() { }
  9. 9. 2. 関数の作り方  絶対値を返す Abs() 関数を作る float Abs(float x) { if(x<0) { return –x; } else { return x; } }
  10. 10. 2. 関数の作り方 float Abs(float x) { if(x<0) { return –x; } else { return x; } } void setup() { println(Abs(-3.5)); } void draw() { }
  11. 11. 2. 関数の作り方  与えられた 2 つの値のうち大きい方の値 を返す Max() 関数 float Max(float x, float y) { if(x>y) { return x; } else { return y; } }
  12. 12. 2. 関数の作り方 float Max(float x, float y) { if(x>y) { return x; } else { return y; } } void setup() { println(Max(6.6, 3.5)); println(Max(-3.3, 5.5)); } void draw() { }
  13. 13. 2. 関数の作り方  与えられた 2 つの値のうち大きい方の値 を返す Min() 関数 float Min(float x, float y) { if(x>y) { return y; } else { return x; } }
  14. 14. 2. 関数の作り方 float Min (float x, float y) { if(x>y) { return y; } else { return x; } } void setup() { println(Min(6.6, 3.5)); println(Min(-3.3, 5.5)); } void draw() { }
  15. 15. 2. 関数の作り方  与えられた 3 つの値のうち最大の値を返 す Max3() 関数 float Max3(float x, float y, float z) { return Max(x, Max(y, z)); }
  16. 16. 2. 関数の作り方  与えられた中心位置と半径の円を描く Circle() 関数  戻り値が必要ない場合、戻り値の型は void(ヴォイド)で、return しない void Circle(float x, float y, float r) { ellipse(x,y,r*2,r*2); }
  17. 17. 2. 関数の作り方 void Circle(float x, float y, float r) { ellipse(x,y,r*2,r*2); } void setup() { size(600,400); } void draw() { Circle(300,200,200); }
  18. 18. 3. max(), min() 関数  Processing で最初から使える関数  大きい方の値、小さい方の値を返す max(a,b) min(a,b);
  19. 19. 4. dist() 関数  2 点間 (x1,y1) – (x2,y2) の距離を返す  dist(0,0,1,1) = 1.41421… dist(x1,y1,x2,y2)
  20. 20. 4. dist() 関数 void setup() { size(600,400); textSize(30); } void draw() { background(0,0,0); ellipse(300,200,10,10); float d = dist(300,200,mouseX,mouseY); text(d,50,50); }
  21. 21. 5. 時刻関数  現在の時、分、秒を int 型の値で返す hour() minute(); second();
  22. 22. 6. クラスとは いくつかの変数 1 つの型 int y int x int w int h Rectangle 型
  23. 23. 7. クラスの作り方 class クラス名 { いくつかのメンバ変数 }
  24. 24. 7. クラスの作り方 class Rectangle { float x, y, w, h; }
  25. 25. 8. クラスを使う  new を使って作成 Rectangle r = new Rectangle(); class Rectangle { float x, y, w, h; } void setup() { size(600,400); } void draw() { }
  26. 26. 8. クラスを使う Rectangle r = new Rectangle(); class Rectangle { float x, y, w, h; } void setup() { size(600,400); r.x = 200; r.y = 100; r.w = 300; r.h = 200; } void draw() { rect(r.x, r.y, r.w, r.h); } . を使ってメンバにアクセス. を使ってメンバにアクセス
  27. 27. 9. コンストラクタ class クラス名 { メンバ変数 クラス名( 引数 ) { 何らかの処理 } }
  28. 28. 9. コンストラクタ  new のときの引数と、初期化の方法を決 めることができる class Rectangle { float x, y, w, h; Rectangle(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } }
  29. 29. 9. コンストラクタ Rectangle r = new Rectangle(200,100,300,200); class Rectangle { float x, y, w, h; Rectangle(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } } void setup() { size(600,400); } void draw() { rect(r.x, r.y, r.w, r.h); }
  30. 30. 10. メンバ関数  メンバ関数は、メンバ変数を扱う関数 class クラス名 { メンバ変数 メンバ関数 }
  31. 31. 10. メンバ関数 class Rectangle { float x, y, w, h; Rectangle(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } float area() { return w * h; } void draw() { rect(x,y,w,h); } }
  32. 32. 10. メンバ関数 Rectangle r = new Rectangle(200,100,300,200); class Rectangle { //////////// //////////// } void setup() { size(600,400); println(r.area()); } void draw() { r.draw(); }
  33. 33. 11. クラスの配列 Rectangle[] rects = new Rectangle[6]; class Rectangle { //////////// //////////// } void setup() { size(600,400); for(int i=0; i<rects.length; ++i) { rects[i] = new Rectangle(i*100,100,50,50); } } void draw() { for(int i=0; i<rects.length; ++i) { rects[i].draw(); } }
  34. 34. 12. そのほかの便利な機能  String は文字列を代入できる型 String message = “Hello”; println(message);
  35. 35. 12. そのほかの便利な機能  color は色を代入できる型であり、色を作 成する関数でもある color c = color(255,0,0); background(c);
  36. 36. 12. そのほかの便利な機能 class Rectangle { ////// void draw(color c) { fill(c); rect(x,y,w,h); } } void draw() { r.draw( color(255,0,0) ); }
  37. 37.  簡単アプリ開発にチャレンジ http://p.tl/ishH

×