using System;
using System.Collections.Generic;
using System.Text;

using Motion6D;
using CommonMathOperations;

namespace CelestialMechanics
{
    class EllipticalOrbit : Orbit
    {
        private double omega;
        private double bigSemiAxis;
        private double M;
        private OneVariableFunction func;

        const double div = 1 / (2 * Math.PI);

        double tau;

        internal EllipticalOrbit(double p, double e, double mu, double tau)
        {
            this.p = p;
            this.e = e;
            bigSemiAxis = p / (1 - e);
            omega = Math.Pow(mu / (bigSemiAxis * bigSemiAxis * bigSemiAxis), 1 / 3);
            func = function;

        }

        

  /*      internal EllipticalOrbit(double p, double e, double inclination, double ascendingNode, double argOfPeriapsis, double tau, double mu)
            : base(p, e, inclination, ascendingNode, argOfPeriapsis, tau, mu)
        {
            bigSemiAxis = p / (1 - e);
            omega = Math.Pow(mu / (bigSemiAxis * bigSemiAxis * bigSemiAxis), 1 / 3);
            func = function;
        }
*/

        public override void GetPlanar(double time, out double x, out double y)
        {
            M = 2 * Math.PI * omega * (time - tau);
            M = M - Math.Truncate(M);
            if (M > 0.5)
            {
                M = 1 - M;
            }
            M *= div;
            double ang = Solver.Solve(M, func, 15, 1e-15);
            double c = Math.Cos(ang);
            double r = p / (1 - e * c);
            x = r * c;
            y = r * Math.Sin(ang);
        }

        double function(double x)
        {
            return M + e * Math.Sin(x);
        }

    }
}
