The Cycle's math node has an "Inverse tangent" option. This function however cannot discriminate between two opposite points on the circle, as they have the same tangent. Usually, this is solved with a function called atan2, which receives two inputs, the x and y of the vector, and returns the angle. Can I construct something similar in Cycle's nodes?
EDIT July 2018
If you are using Blender 2.8 or a recent build of 2.79, the math node now has the atan2 operation since this commit by Lukas Stockner.
This is in no way the most efficient way of doing it, but just a reminder that knowing a bit of math, you can painstakingly craft functions using NodeGroups.
- The Maximum Math node acts as an OR boolean operation.
- The Minimum Math node acts as an AND boolean operation.
- The Substract Math node acts as a NOT boolean operation with 1 as first input and your value as second.
- The Mix Colors node acts as a switch with a boolean as first input, which returns second input if condition == False, or third input if condition == True.
Very old question, I know. However, here's an alternative (and simpler) node-based solution to produce the atan2(x,y) function.
The difference between 'arctangent' and 'atan2' is that the 'arctangent' only provides unique values for one half of the rotation - the other have returns duplicate results. In order to extract the second half of the rotation (as is available from
atan2(X,Y)) we need to use the sign of one of the input values (in this case
X) to determine when we are in the second half of the full rotation.
The Less Than node detects the change in sign while the Multiply changes the 0.0/1.0 output of the Less Than into either 0.0 or 'pi' (which represents a 'half turn' (180 degrees) since we're working in radians rather than degrees). This is then added into the result of
arctangent(Y/X) and, finally, adjusted by a quarter of a turn (pi/2 radians) to match the output from 'atan2(...)'.
In order to test this I compared it to the OSL example provided by @sambler to prove that it produces the same result as the OSL 'atan2(X,Y)' function.