Search code examples
javascriptmathgeometryphysicskinematics

How can I determine which half of an elliptical path a point is currently on?


Here is a static frame from an atom animation I'm working on in JavaScript, modelled off this image.

enter image description here

Here is the code used to determine the position of an electron in its orbit based on time:

// Get position along elliptical path.
var x = Math.cos( this.timer.delta() * this.speed ) * ( this.pathWidth / 2 );
var y = Math.sin( this.timer.delta() * this.speed ) * ( this.pathHeight / 2 );

What I'd like to do is place the electron above the nucleus when on the orange part of the path, and below the nucleus during the green segment.

When this.timer.delta() == 0, the electron is at the extreme-right end, and then proceeds to travel counter-clockwise.

I'm looking for help with the following two things:

1) Finding the point in time in which the electron will be at the far left of its orbit.

2) Determining which half of the path an electron is currently on for any given time.

Ideally, the solutions should work regardless of the value of this.speed (which is number multiplier for speeding up or slowing down the animation).


Solution

  • It all depends on the angle - and the angle is this part in your expression:

    this.timer.delta() * this.speed
    

    So you can simply determine whether the angle is between zero and PI - and if it is - the nucleus is "in front".

    Of course simply checking whether y is non-negative does the same trick.