一方向運動リストに戻る    印刷する

一方向の円運動…ひとつ(軌跡あり)

ひとつの円が、円周上を一定速度で、軌跡を残しながら運動します。 座標の計算は、軌跡なしの一方向の円運動と同じです。

【リスト Circle2】の動きは、【リスト 三角関数:円周上を移動】とも同じですが、この三角関数のセクションのサンプルは、次のように、まずtranslate()関数を使って座標の原点を画面の中央(円の中心)に移動して、そこを原点として座標を計算している点が異なります。
 translate(width/2, height/2);  //座標原点を画面の中央へ移動
 x = r * cos(radians(angle));   //rは円周の半径
 y = r * sin(radians(angle));
 ellipse(x, y, 10, 10);
 angle = angle + da; //da度ずつ右へ回る
これに対し、下の【リスト Circle2】では、座標原点は画面の左上角のままで、座標計算の時に、width/2.0、height/2.0を加えることで、円の中心を(width/2.0, height/2.0)に調整しています。

Your browser does not support the canvas tag.

クリックで停止⇔再開
【リスト Circle2】
float angle; // 位置(角度)
float da = 3; // 角度の変化量
float x, y;
float d = 10; // 円の直径
float len = 60; // 軌跡の円の半径

void setup() {
  size(150, 150);   //描画するための画面
  fill(0);
  background(255);   //画面の背景を白でクリア
}

void draw() {
     x = len * cos(radians(angle)) + width/2.0;
     y = len * sin(radians(angle)) + height/2.0;
     ellipse(x, y, d, d);

     if (angle > 360) {  //1周したら
        angle = 0;       //angleをゼロに戻し、
        background(255); //画面背景を白でクリア
     } else {
        angle = angle + da;
     }
}

arigat アットマーク acm.org / copyright © info