Top Banner
早稲田大学 基幹理工学部 表現工学科 長研究室 B4 鈴木 31 July 2013
37

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

May 27, 2015

Download

Technology

Ryo Suzuki
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Processingによるプログラミング入門 第6回

早稲田大学 基幹理工学部 表現工学科長研究室 B4 鈴木 遼

31 July 2013

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

今日やること

Processing とプログラミングの基礎

今日は 12 項目

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

1. 関数とは

入力 機械 出力

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

1. 関数とは

引数 関数 戻り値

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

1. 関数とは

引数 関数 戻り値

abs() -3.5 3.5

sqrt() 16.0 4.0

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

2. 関数の作り方

戻り値の型 名前(引数の型 引数名) { 何らかの処理 return 戻り値 ; }

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

2. 関数の作り方

与えられた数を二乗する Square() 関数を作る

float Square(float x) { return x*x; }

戻り値の型 名前(引数の型 引数名) { 何らかの処理 return 戻り値 ; }

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

2. 関数の作り方

float Square(float x) { return x*x; } void setup() { float s = Square(12); println(s); } void draw() { }

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

2. 関数の作り方 絶対値を返す Abs() 関数を作る

float Abs(float x) { if(x<0) { return –x; } else { return x; } }

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

2. 関数の作り方

float Abs(float x) { if(x<0) { return –x; } else { return x; } } void setup() { println(Abs(-3.5)); } void draw() { }

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

2. 関数の作り方 与えられた 2 つの値のうち大きい方の値

を返す Max() 関数

float Max(float x, float y) { if(x>y) { return x; } else { return y; } }

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

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() { }

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

2. 関数の作り方 与えられた 2 つの値のうち大きい方の値

を返す Min() 関数

float Min(float x, float y) { if(x>y) { return y; } else { return x; } }

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

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() { }

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

2. 関数の作り方 与えられた 3 つの値のうち最大の値を返

す Max3() 関数

float Max3(float x, float y, float z) { return Max(x, Max(y, z)); }

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

2. 関数の作り方 与えられた中心位置と半径の円を描く

Circle() 関数

戻り値が必要ない場合、戻り値の型は void(ヴォイド)で、return しない

void Circle(float x, float y, float r) { ellipse(x,y,r*2,r*2); }

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

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); }

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

3. max(), min() 関数

Processing で最初から使える関数

大きい方の値、小さい方の値を返す

max(a,b) min(a,b);

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

4. dist() 関数

2 点間 (x1,y1) – (x2,y2) の距離を返す

dist(0,0,1,1) = 1.41421…

dist(x1,y1,x2,y2)

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

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); }

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

5. 時刻関数

現在の時、分、秒を int 型の値で返す

hour() minute(); second();

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

6. クラスとは

いくつかの変数 1 つの型

int y

int x

int w

int h

Rectangle 型

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

7. クラスの作り方

class クラス名 { いくつかのメンバ変数 }

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

7. クラスの作り方

class Rectangle { float x, y, w, h; }

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

8. クラスを使う

new を使って作成

Rectangle r = new Rectangle(); class Rectangle { float x, y, w, h; } void setup() { size(600,400); } void draw() { }

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

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); }

. を使ってメンバにアクセス . を使ってメンバにアクセス

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

9. コンストラクタ

class クラス名 { メンバ変数 クラス名( 引数 ) { 何らかの処理 } }

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

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; } }

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

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); }

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

10. メンバ関数

メンバ関数は、メンバ変数を扱う関数

class クラス名 { メンバ変数 メンバ関数 }

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

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); } }

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

10. メンバ関数

Rectangle r = new Rectangle(200,100,300,200); class Rectangle { //////////// //////////// } void setup() { size(600,400); println(r.area()); } void draw() { r.draw(); }

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

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(); } }

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

12. そのほかの便利な機能

String は文字列を代入できる型

String message = “Hello”; println(message);

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

12. そのほかの便利な機能

color は色を代入できる型であり、色を作

成する関数でもある

color c = color(255,0,0); background(c);

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

12. そのほかの便利な機能

class Rectangle { ////// void draw(color c) { fill(c); rect(x,y,w,h); } }

void draw() { r.draw( color(255,0,0) ); }

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

簡単アプリ開発にチャレンジ

http://p.tl/ishH