2012-02-06 8 views
3

処理中に楕円のパスを連続的に回転させるようなオブジェクト(例:12)を作成しようとしています。私は円で回転するスケッチを得て、それを楕円で回転させたいと思っています。私は処理フォーラムからいくつかのポインタを持っていますが、ポインタのコードは私が投稿したコードとは異なり、私はまだ理解できませんでした(三角法で弱いです)。楕円のパス内のいくつかの要素を回転する

私は少しググと、このアルゴリズムでこれを達成しようとしている記事を見つけました:

あなたはいくつかのパラメータを使用して楕円を定義する必要があります。あなたが移動する場合

x, y: center of the ellipse 
a, b: semimajor and semiminor axes 

これは、長軸と楕円の位置との間の角度を に変更することを意味します。 はこの角度をアルファにします。

あなたの位置(X、Y)は、次のとおりです。

X = x + (a * Math.cos(alpha)); 
Y = y + (b * Math.sin(alpha)); 

あなたは、あなたの位置を再計算し、αおよび 減少/増加する必要が左または右に移動させるために。出典: http://answers.unity3d.com/questions/27620/move-object-allong-an-ellipsoid-path.html

私はスケッチにどのように統合しますか?ありがとうございました。

は、ここに私のスケッチです:

void setup() 
{ 
    size(1024, 768); 
    textFont(createFont("Arial", 30)); 
} 

void draw() 
{ 
    background(0); 
    stroke(255); 

    int cx = 500; 
    int cy = 350; 
    int r = 300; //radius of the circle 
    float t = millis()/4000.0f; //increase to slow down the movement 

    ellipse(cx, cy, 5, 5); 

    for (int i = 1 ; i <= 12; i++) { 
     t = t + 100; 
     int x = (int)(cx + r * cos(t)); 
     int y = (int)(cy + r * sin(t)); 

     line(cx, cy, x, y); 
     textSize(30); 
     text(i, x, y); 

     if (i == 10) { 
      textSize(15); 
      text("x: " + x + " y: " + y, x - 50, y - 20); 
     } 
    } 
} 

答えて

3

int a = 350; // major axis of ellipse 
int b = 250; // minor axis of ellipse 

int r = 300; //radius of the circle 

を交換し、

int x = (int)(cx + r * cos(t)); 
int y = (int)(cy + r * sin(t)); 
を置き換えます

int x = (int)(cx + a * cos(t)); 
int y = (int)(cy + b * sin(t)); 
+0

ありがとう、アンドリュー!できます。 :) – Scott

関連する問題