Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active January 20, 2019 12:45
Show Gist options
  • Save unitycoder/5a37cfc57cd921fd0936 to your computer and use it in GitHub Desktop.
Save unitycoder/5a37cfc57cd921fd0936 to your computer and use it in GitHub Desktop.
float atan2Approximation for C# and Shader
// c#
float atan2Approximation(float y, float x) // http://http.developer.nvidia.com/Cg/atan2.html
{
float t0, t1, t2, t3, t4;
t3 = Mathf.Abs(x);
t1 = Mathf.Abs(y);
t0 = Mathf.Max(t3, t1);
t1 = Mathf.Min(t3, t1);
t3 = 1f / t0;
t3 = t1 * t3;
t4 = t3 * t3;
t0 = - 0.013480470f;
t0 = t0 * t4 + 0.057477314f;
t0 = t0 * t4 - 0.121239071f;
t0 = t0 * t4 + 0.195635925f;
t0 = t0 * t4 - 0.332994597f;
t0 = t0 * t4 + 0.999995630f;
t3 = t0 * t3;
t3 = (Mathf.Abs(y) > Mathf.Abs(x)) ? 1.570796327f-t3 : t3;
t3 = (x < 0) ? 3.141592654f-t3:t3;
t3 = (y < 0) ? -t3 : t3;
return t3;
}
// shader
float atan2Approximation(float y, float x) // http://http.developer.nvidia.com/Cg/atan2.html
{
float t0, t1, t2, t3, t4;
t3 = abs(x);
t1 = abs(y);
t0 = max(t3, t1);
t1 = min(t3, t1);
t3 = float(1) / t0;
t3 = t1 * t3;
t4 = t3 * t3;
t0 = - 0.013480470;
t0 = t0 * t4 + 0.057477314;
t0 = t0 * t4 - 0.121239071;
t0 = t0 * t4 + 0.195635925;
t0 = t0 * t4 - 0.332994597;
t0 = t0 * t4 + 0.999995630;
t3 = t0 * t3;
t3 = (abs(y) > abs(x)) ? 1.570796327 - t3 : t3;
t3 = (x < 0) ? 3.141592654 - t3 : t3;
t3 = (y < 0) ? -t3 : t3;
return t3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment