4

I am trying to make some objects, say 12, to rotate in an ellipse path continuously in Processing. I got a sketch which does rotation in a circle and I want to make it to rotate in a ellipse. I have some pointer from processing forum but the code from the pointer is different from the code that I posted and I couldn't understand yet (weak in trigonometry).

I googled a bit and found a post trying to achieve this with this algorithm:

You need to define your ellipse with a few parameters:

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

If you want to move on the elipses this means that you change the angle between the major axes and your position on the ellipse. Lets call this angle alpha.

Your position (X,Y) is:

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

In order to move left or right you need to increase/decrease alpha and then recalculate your position. Source: http://answers.unity3d.com/questions/27620/move-object-allong-an-ellipsoid-path.html

How do I integrate it into my sketch? Thank you.

Here's my sketch:

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);
        }
    }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Scott
  • 85
  • 1
  • 3
  • 8

1 Answers1

3

Replace

int r = 300; //radius of the circle

with

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

and replace

int x = (int)(cx + r * cos(t));
int y = (int)(cy + r * sin(t));

with

int x = (int)(cx + a * cos(t));
int y = (int)(cy + b * sin(t));
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116