How is a camera/observer vector calculated in PGFPlots
A unit camera vector pointing toward the camera/observer can be useful for calculating e.g. background and foreground. I saw it used first time here:
Parabolic Moebius map on sphere using tikz
and I have myself used it e.g. here:
Quiver plot on surface
-just by copying the vector:
@Jasper asks in the comments how it is derived and I ask this question to make my own answer - finally doing the work myself.
The vector can be derived from definitions of cos sin and the description from the manual:
/pgfplots/view={â¨azimuthâ©}{â¨elevationâ©} (initially {25}{30}) Changes
both view angles of a 3D axis. The azimuth (first argument) is the
horizontal angle which is rotated around the z-axis. For a 3D plot,
the z-axis always points to the top. The elevation (second argument)
is the vertical rotation around the (rotated) x-axis. Positive
elevation values indicate a view from above, negative a view from
below. All values are measured in degree
I draw the camera vector as a red line with a circle/ball at the end. To better visualise the vector, I also draw dashed lines in the x-y plane and up to the end point. Note that this vector points directly toward the camera/observer in the two left graphs below and is therefore only seen as a circle in the origin.
With only \azimuth rotation we have:
Observing the green right angle triangle, we get:
Now we also apply an \elevation:
Observing the blue vertical right angle triangle, we get
As the hypotenuse is one, the cathetus in the x-y plane from before is scaled by cos(\elevation) and we have the result:
Considerations to what happens when cos/sin to \azimuth/\elevation changes sign needs to be done to complete the derivation.