package org.josast.equationKepler;

import org.avmdti.josast.util.MathTools;

/**
 * <b>Description :Solve the kepler Equation with the Newton Method</b>
 * 
 * <p>
 * Projet : JCP <BR><br>
 * <b>JOSAST</b> : Java Open Source Amateur Satellite  Toolbox  <br>
 * The aim of the project is to create a set of tools for amateur
 * satellite purpose. All this tools could be used together to create
 * specific software. <b>JOSAST</b> project is managed by AVMDTI (<A
 * HREF="http://www.avmdti.org">http://www.avmdti.org </a> ) <br>
 * This software is an open source software. Please read the <b><i>JOSAST
 * licence</i></b><BR>(<A
 * HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
 * </p>
 * 
 * <p>
 * for more information contact <a href="mailto:josast@avmdti.org">josast
 * at avmdti.org</a>
 * </p>
 *
 * @author <a href="mailto:mercier.josast@avmdti.org">mercier</a>
 * @version 1.0 <p><p>
 */
public class KeplerNewton implements Kepler {
    //~ Static fields/initializers ----------------------------------------

    /** DOCUMENT ME! */
    static private final double DEFAULTPRECISION = 1.0e-14;

    /** DOCUMENT ME! */
    private static double epsilon = DEFAULTPRECISION;

    //~ Methods -----------------------------------------------------------

    //(Mathematique.RadiansPerDegree / 360000.0);

    /**
     * @see org.josast.equationKepler.Kepler#Calcul(double, double)
     */
    public final double calcul(final double meanAnomaly,
        final double eccentricity) {
        double exentrici; // Eccentric Anomaly utilisé pour les calculs
        double exentrici1; // Eccentric Anomaly utilisé pour les calculs
        double error; // erreur 
        double res; //

        exentrici = meanAnomaly; // Initial guess

        do {
            exentrici1 = exentrici
                - ((exentrici - (eccentricity * Math.sin(exentrici))
                - meanAnomaly) / (1 - (eccentricity * Math.cos(exentrici))));
            error = exentrici1 - exentrici;
            exentrici = exentrici1;
        } while (MathTools.abs(error) >= epsilon);

        // tant que l'erreur est inférieur à une certaine valeur
        res = exentrici1;

        return res;
    }

    /**
     * @see org.josast.equationKepler.Kepler#getEpsilon()
     */
    public final double getEpsilon() {
        return epsilon;
    }

    /**
     * @see org.josast.equationKepler.Kepler#setEpsilon(double)
     */
    public final void setEpsilon(final double epsilon1) {
        epsilon = epsilon1;
    }
}
