I'm trying to modify a model that starting from horizontal coordinates (https://en.wikipedia.org/wiki/Horizontal_coordinate_system) gives us the cartesian directions (or the unit vector).
Having as an input: azimuth (az) and elevation (el) of a point (for example the sun) on the horizon and x,y,z of the observer.
Looking for Cartesian direction (dir_x, dir_y, dir_z).
I have looked for answer here, but I did not find what I was asking (this question for example is also having the radius as input and therefore is working in spherical coordinates and not in horizontal).
Browsing online and remembering polar coordinates derivation and trigonometry anyway I think I found a solution (the code is in MATLAB):
% Convert azimuth and elevation from degrees to radians
az_rad = deg2rad(az);
el_rad = deg2rad(el);
% Derive the components of the point on the horizon
x_sun = cos(az_rad) * cos(el_rad);
y_sun = sin(az_rad) * cos(el_rad);
z_sun = sin(el_rad);
% Calculate the direction vectors along the three axes
dir_x = [x - x_sun, y, z];
dir_y = [x, y - y_sun, z];
dir_z = [x, y, z - z_sun];
% Normalize the direction vectors (norm(v) returns the Euclidean norm of vector v)
dir_x = dir_x / norm(dir_x);
dir_y = dir_y / norm(dir_y);
dir_z = dir_z / norm(dir_z);
% Print the direction vectors
disp("Direction along the X-axis:");
disp(dir_x);
disp("Direction along the Y-axis:");
disp(dir_y);
disp("Direction along the Z-axis:");
disp(dir_z);
The problems with this code are:
- I am not sure this algorithm is right 1.2) I have no way of finding out if this work how it should be by trial
- I want to find a formal mathematical derivation of this results as a proof but I'm not able to (I have read a lot of astronomy and 3D modelling book chapters looking for this)
- If there are alternatives I'm happy to see them :) .
Thank you!