Finding the distance to the Sun is quite straightforward once we know the true anomaly of the Sun, which we did in the previous post in the quest to find the Sun’s position.

In fact the first half of the code is pretty much identical.

The one thing left to do is find the distance using the formula

Distance = (Semi-major axis * (1 – v * v)) / (1 + (eccentricity * Cos(v)))

which gives a distance in the same units as the semi-mahor axis, which is usually kilometres.

		public static double CalcSunDistance(DateTime dDate, DateTime dEpoch)
		{
			double fD;
			double fN;
			double fM;
			double fE;
			double fTanV2;
			double fV;
			double fDistance;
			double fSolarMEL;
			double fSolarPL;
			double fSunEarthEcc;
			double fAcc;
			double fOblique;
			double fSMA;

			fAcc = 0.0000001;
			fD = UraniaTime.GetDaysBetween(dDate, dEpoch);
			fSolarMEL = GetSolarMEL(dEpoch, true);
			fSolarPL = GetSolarPerigeeLong(dEpoch, true);
			fSunEarthEcc = GetSunEarthEcc(dEpoch, true);
			fOblique = GetEarthObliquity(dEpoch, true);
			fSMA = 149598500.0;

			fN = (360.0 / 365.242191) * fD;
			fN = Trig.PutIn360Deg(fN);
			fM = fN + fSolarMEL - fSolarPL;
			fM = Trig.PutIn360Deg(fM);
			fM = Trig.DegToRad(fM);
			fE = CalcEccentricAnomaly(fM, fM, fSunEarthEcc, fAcc);
			fTanV2 = Math.Sqrt((1.0 + fSunEarthEcc) / (1.0 - fSunEarthEcc)) * Math.Tan(fE / 2.0);
			fV = Math.Atan(fTanV2) * 2.0;
			fDistance = (fSMA * (1.0 - (fSunEarthEcc * fSunEarthEcc))) / (1.0 + (fSunEarthEcc * Math.Cos(fV)));

			return fDistance;
		}
   
Share