There are several situations where there are several methods for calculating the same value.
Now I am thinking of an algorithm for "expanding" a two-dimensional convex polygon. To do this, I want to find which direction outrages each vertex. To get a result that expands a polygon with "skin" of the same thickness around, the amount that is perturbed in this direction also depends on the angle at the vertex. But right now I'm just worried about the direction.
One way is to use atan2: let B be my vertex, A the previous vertex, and C the next vertex. My direction is the "angular mean" angle(BA) and angle(BC) .
Another way involves sqrt: unit(BA)+unit(BC) , where unit(X) is X/length(X) , gives a vector with my direction.
I tend to method number 2, because the values ββof the averaging angle require a little work. But I basically choose between two atan2 calls and two sqrt calls. What is going faster? And if I did it in a shader program?
I am not trying to optimize my program as such, I would like to know how these functions are usually implemented (for example, in standard c libraries), so I can learn, in general, what is the best choice.
From what I know, both sqrt and trig functions require the iterative method to come up with an answer. It is for this reason that we try to avoid them whenever possible. People have come up with "approximate" functions that use lookup and interpolation tables and such to try to get faster results. Of course, I will never worry about this unless I find convincing evidence of a bottleneck in my code because these procedures or routines are strongly related to them, but the differences between sqrt, trigger combinations, and inverse trigger functions can make a difference for discussion,
source share