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