Skip to content

Instantly share code, notes, and snippets.

@ifree
Forked from gradbot/atan2.as
Created October 28, 2012 22:54
Show Gist options
  • Save ifree/3970290 to your computer and use it in GitHub Desktop.
Save ifree/3970290 to your computer and use it in GitHub Desktop.
atan2(y, x) in AGAL using atan(x) = Pi / 2 * x / (1 + x)
context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, Vector.<Number>([
1,
0,
Math.PI,
2 * Math.PI,
1e-10,
Math.PI / 2,
0, // not used
0, // not used
]));
// ft4.x = atan2(ft2.y, ft2.x) Uses: ft3, ft4, ft5
"add ft2.x, ft2.x, fc1.x \n"+ // fudge to prevent div zero
"div ft3.x, ft2.y, ft2.x \n"+ // ft2.x = ydiff / xdiff
"neg ft3.y, ft3.x \n"+ // ft2.y = -ydiff / xdiff
"mul ft4.y, fc1.y, ft3.x \n"+ // ft4.x = atan(ft2.x)
"add ft4.z, fc0.x, ft3.x \n"+ // atan(x) = Pi / 2 * x / (1 + x)
"div ft5.x, ft4.y, ft4.z \n"+
"mul ft4.y, fc1.7, ft3.y \n"+ // ft4.y = atan(ft2.y)
"add ft4.z, fc0.x, ft3.y \n"+ // atan(x) = Pi / 2 * x / (1 + x)
"div ft5.y, ft4.y, ft4.z \n"+
"slt ft4.x, ft2.x, fc0.y \n"+ // x < 0 ft4.x
"slt ft4.y, ft2.y, fc0.y \n"+ // y < 0 ft4.y
"sub ft4.z, fc0.x, ft4.x \n"+ // x >= 0 ft4.z
"sub ft4.w, fc0.x, ft4.y \n"+ // y >= 0 ft4.w
"mul ft3.x, ft4.z, ft4.w \n"+ // x > 0 && y > 0 ft3.x
"mul ft3.y, ft4.x, ft4.w \n"+ // x < 0 && y > 0 ft3.y
"mul ft3.z, ft4.x, ft4.y \n"+ // x < 0 && y < 0 ft3.z
"mul ft3.w, ft4.z, ft4.y \n"+ // x > 0 && y < 0 ft3.w
"sub ft4.x, ft5.x, fc0.z \n"+ // a - Pi ft4.x
"neg ft4.y, ft5.y \n"+ // -a ft4.y
"mov ft4.z, ft5.x \n"+ // a ft4.z
"sub ft4.w, fc0.z, ft5.y \n"+ // Pi - a ft4.w
"mul ft4, ft4, ft3 \n"+ // multiply grid of possibilities
"add ft4.xy, ft4.xz, ft4.yw \n"+ // add possibilities
"add ft4.x, ft4.x, ft4.y \n"+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment