# Horizontal to Equatorial Conversion - Code

Following this tutorial I have been able to convert from Equatorial to Horizontal Coordinates and am now searching for a way to do the reverse.

I understand this kind of question has been asked and answered using mathematics, but I have been unsuccessful in implementing the provided algorithm in code. Below is my C Sharp code for Equatorial -> Horizontal conversion, which I'm looking to reverse.

public static HorizontalCoords EquatorialToHorizontal(EquitorialCoords equatorialCoords, GeographicCoords geographicCoords, double localSiderealTime)
{

double ha = (localSiderealTime - equatorialCoords.rightAscention) * hours2deg;

double x_sid = Cos_d(ha) * Cos_d(equatorialCoords.declination);
double y_sid = Sin_d(ha) * Cos_d(equatorialCoords.declination);
double z_sid = Sin_d(equatorialCoords.declination);

double x_hor = x_sid * Sin_d(geographicCoords.latitude) - z_sid * Cos_d(geographicCoords.latitude);
double y_hor = y_sid;
double z_hor = x_sid * Cos_d(geographicCoords.latitude) + z_sid * Sin_d(geographicCoords.latitude);

double az = Atan2_d(y_hor, x_hor) + 180;
double alt = Atan2_d(z_hor, Sqrt(x_hor * x_hor + y_hor * y_hor));

return new HorizontalCoords(alt, az);
}

public static EquatorialCoords HorizontalToEquatorial(HorizontalCoords horizontalCoords, GeographicCoords geographicCoords, double localSiderealTime)
{
//*hmmmmmm what goes here..
}

• This looks like a case of "DRY": You should probably write a general procedure to transform between spherical coordinates, as much of the code will be repeated. Jul 13 '20 at 7:52

HorizontalToEquatorial would be like EquatorialToHorizontal in reverse, something like this:

• (x, y, z)_hor = rectangular form of (alt, az)

• (x, y, z)_sid = (x, y, z)_hor rotated by geographic latitude

• (ha, dec) = spherical form of (x, y, z)_sid

• ra = local sidereal time - ha

• Ok that makes sense, I guess the part I'm finding challenging is the exact code for the conversion. Jul 14 '20 at 2:27
• @PeterHayman What have you tried? Dec 11 '20 at 2:21
• for now im using matricies to invert the calculation Dec 12 '20 at 3:30