I’m working on some basic celestial navigation concepts, using Processing and Android. This particular problem is in Processing and is an attempt to port c/c++ code to Processing/Java.
I started with reading a paper from this link: VectorSolution
His example says the Altitude should be 22.041 and the Azimuth should be 138.2, for the assumed position and geographic position I used as input in the code I post below.
Something just isn’t right. I have a vague idea that C uses pointers and Java doesn’t worry about it until it causes an exception.
Here’s my code:
import controlP5.*;
//From NavigationalAlgorithms Vectorsolution
/*
File: vector.cpp
Cálculo vectorial
Resultado test: OK
This file contains proprietary information of Andrés Ruiz Gonzalez
Andrés Ruiz. San Sebastian - Donostia. Gipuzkoa
Copyright (c) 1999 - 2007
*/
double Hc;
double Z;
double GHA = 347.78;
double dec = -16.72;
double Be = 40;
double Le = 28;
//ControlP5 cp5;
//ControlP5 controlP5;
//Textarea myTextarea;
void setup() {
// fullscreen();
size(127, 75, OPENGL);
/*
myTextarea = cp5.addTextarea("txt")
.setPosition(450,10)
.setSize(800,700)
.setFont(createFont("arial",14))
.setLineHeight(14)
.setColor(color(255))
.setColorBackground(color(255,0))
.setColorForeground(color(255,100));
*/
}
void draw() {
AltitudeAzimuth( GHA, dec, Be, Le);
println("Altitude: ", Hc);
println(" Azimuth: ", Z);
}
double Mod(double x[]) {
return(Math.sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]));
}
double[] Add(double x[], double y[]) {
double z[] = new double[3];
for (int i = 0; i < 3; i++) {
z[i] = x[i] + y[i];
}
return z ;
}
double[] aVector(double a, double x[]) {
double z[] = new double[3];
z[0] = a * x[0];
z[1] = a * x[1];
z[2] = a * x[2];
return (z);
}
double[] Unit(double x[]) {
return (aVector(1.0/Mod(x), x));
}
double Dot(double x[], double y[]) {
return (x[0]*y[0]+x[1]*y[1]+x[2]*y[2]);
}
double[] Cross(double x[], double y[]) {
double z[] = new double[3];
z[0] = x[1]*y[2] - z[2]*y[1];
z[1] = x[2]*y[0] - x[0]*y[2];
z[2] = x[0]*y[1] - x[1]*y[0];
return (z);
}
double[] VectorSpherical2Cartesian(double B, double L) {
double v[] = new double[3];
v[0] = Math.cos(B) * Math.cos(L);
v[1] = Math.cos(B) * Math.sin(L);
v[2] = Math.sin(B);
println(B);
println(L);
return(v);
}
void AltitudeAzimuth(double GHA, double dec, double Be, double Le) {
double GP[];
double AP[];
double Np[] = {0, 0, 1};
double U1[], U2[], U3[], Az[];
GP = VectorSpherical2Cartesian(dec, GHA);
AP = VectorSpherical2Cartesian(Be, Le);
println("AP : ", AP[0], " ", AP[1], " ", AP[2]);
Hc = 90.0 - Math.acos(Dot(AP, GP));
println((Hc));
U1 = Unit(Cross(AP, Np));
println("U1 : ", U1[0], " ", U1[1], " ", U1[2]);
U2 = Cross(U1, AP);
println("U2 : ", U2[0], " ", U2[1], " ", U2[2]);
U3 = Unit(Cross(AP, GP));
println("U3 : ", U3[0], " ", U3[1], " ", U3[2]);
Az = Cross(U3, AP);
println("Az : ", Az[0], " ", Az[1], " ", Az[2]);
Z = Math.acos(Dot(U2, Az));
// println(Z);
if (Dot(U1, Az) < 0.0) {
Z = 360.0 - Z;
}
// println(Z);
}