震える円…ひとつ
一定の範囲内で、円がランダムに振動します。ランダムに震える円のクラスVibateBallを定義し、そのオブジェクトを画面中心に置いただけです。VibrateBallクラスのAPI(Application Programming Interface, 使い方(使うためのインタフェイス))は次のようなものです。
コンストラクタ
VibrateBall(float xx, float yy, int ww, int v, int dd )
xx: x座標
yy: y座標
ww: 震える範囲
v : 移動速度の範囲(-vからvの間)
dd: 直径
メソッド
void display() ボールの位置の計算と描画
このクラスのオブジェクトをsetup()関数の中で生成します。
VibrateBall cir; cir = new VibrateBall(width/2,height/2, wide, v, d);そしてdraw()関数の中で、描画します。
cir.display();
クリックで停止⇔再開
【リスト VibrateOne】
VibrateBall cir;
int v = 5; //移動速度の最大値
float x, y ; // 位置(座標)
int d = 20; // 円の直径
int wide = 3; //動く範囲幅
void setup() {
size(150, 150); //描画するための画面
frameRate(30); //フレームレートを少しゆっくり
fill(0);
cir = new VibrateBall(width/2,height/2, wide, v, d);
}
void draw() {
background(255); //モニタ画面の背景を白でクリア
cir.display();
}
【リスト クラスVibrateBall】
class VibrateBall{
int max; // 移動速度の最大値
int min; // 移動速度の最小値
float x, y ; // 位置(座標)
float originX, originY;
int d; // 円の直径
float sx, sy; // 移動速度
int wide; //動く範囲幅
VibrateBall(float xx, float yy, int ww, int v, int dd){
x = originX = xx;
y = originY = yy;
d = dd;
max = v;
min = -v;
wide = ww;
}
void display() {
sx = random(min, max); //変化量はランダムに決める
sy = random(min, max);
x = x+sx; //新しい位置
y = y+sy;
if(x>originX+wide){//右の範囲を超えたかの判定
x = originX+wide;
sx = -sx;
}else if(x<originX-wide){//左の範囲を超えたかの判定
x = originX-wide;
sx = -sx;
}
if(y>originY+wide){//上の範囲を超えたかの判定
y = originY+wide;
sy = -sy;
}else if(y<originY-wide){//下の範囲を超えたかの判定
y = originY-wide;
sy = -sy;
}
ellipse(x, y, d, d);
}
}