Más contenido relacionado
La actualidad más candente (20)
Similar a Processingによるプログラミング入門 第6回 (20)
Processingによるプログラミング入門 第6回
- 7. 2. 関数の作り方
与えられた数を二乗する Square() 関数を作る
float Square(float x)
{
return x*x;
}
戻り値の型 名前(引数の型 引数名)
{
何らかの処理
return 戻り値 ;
}
- 9. 2. 関数の作り方
絶対値を返す Abs() 関数を作る
float Abs(float x)
{
if(x<0)
{
return –x;
}
else
{
return x;
}
}
- 10. 2. 関数の作り方
float Abs(float x)
{
if(x<0)
{
return –x;
}
else
{
return x;
}
}
void setup()
{
println(Abs(-3.5));
}
void draw()
{
}
- 11. 2. 関数の作り方
与えられた 2 つの値のうち大きい方の値
を返す Max() 関数
float Max(float x, float y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}
- 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. 2. 関数の作り方
与えられた 2 つの値のうち大きい方の値
を返す Min() 関数
float Min(float x, float y)
{
if(x>y)
{
return y;
}
else
{
return x;
}
}
- 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. 2. 関数の作り方
与えられた 3 つの値のうち最大の値を返
す Max3() 関数
float Max3(float x, float y, float z)
{
return Max(x, Max(y, z));
}
- 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. 3. max(), min() 関数
Processing で最初から使える関数
大きい方の値、小さい方の値を返す
max(a,b)
min(a,b);
- 19. 4. dist() 関数
2 点間 (x1,y1) – (x2,y2) の距離を返す
dist(0,0,1,1) = 1.41421…
dist(x1,y1,x2,y2)
- 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);
}
- 25. 8. クラスを使う
new を使って作成
Rectangle r = new Rectangle();
class Rectangle
{
float x, y, w, h;
}
void setup()
{
size(600,400);
}
void draw()
{
}
- 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);
}
. を使ってメンバにアクセス. を使ってメンバにアクセス
- 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. 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);
}
- 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. 10. メンバ関数
Rectangle r = new Rectangle(200,100,300,200);
class Rectangle
{
////////////
////////////
}
void setup()
{
size(600,400);
println(r.area());
}
void draw()
{
r.draw();
}
- 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();
}
}