2012-02-06 62 views
3

我正在嘗試使某些對象(如12)在處理中不斷旋轉到橢圓路徑中。我得到了一個可以在一個圓圈內旋轉的草圖,並且我想讓它以橢圓形旋轉。我有一些來自處理論壇的指針,但指針中的代碼與我發佈的代碼不同,但我現在還不能理解(三角函數較弱)。在橢圓路徑中旋轉某些元素

我用Google搜索了一下,發現一個帖子試圖用這種算法來實現這一目標:

你需要用一些參數定義橢圓:

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

如果你想繼續前進elipses這意味着你改變主軸和你在橢圓上的位置之間的角度。讓 稱這個角度α。

您的位置(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 r = 300; //radius of the circle 

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

並更換

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 2012-02-06 06:18:36