I have another item to add to the pile of ways to calculate sine without trig. Here are the previous ways before we start:
Four Ways to Calculate Sine Without Trig
A Fifth Way to Calculate Sine Without Trig
This method is called Bhaskara I’s sine approximation formula and it’s just a numerical way of approximating sine.
The below is some glsl code from @paniq that has been adapted to take 0 to 1 as input, which corresponds to 0 to 2*pi radians, or 0 to 360 degrees, and returns the normalized vector of that angle. The x component of the vector is the cosine of the angle and the y component of the vector is the sine of the angle. Useful for packing 2d normals into a color channel (;
// https://en.wikipedia.org/wiki/Bhaskara_I%27s_sine_approximation_formula // x is 0..1 corresponding to 0..360 degrees vec2 CosSin(float x) { vec2 si = fract(vec2(0.5,1.0) - x*2.0)*2.0 - 1.0; vec2 so = sign(0.5-fract(vec2(0.25,0.5) - x)); return (20.0 / (si*si + 4.0) - 4.0) * so; }
Here’s a shadertoy to compare/contrast this technique versus reality (or, reality as per the video card). Spoiler alert – the shadertoy is ridiculous, they are basically the same.
Shadertoy: Bhaskara Cos Sin Approximation
More from paniq:
i also wrote an approximate atan to go with it Shadertoy: Pseudo-Polar Mapping see the ALTMETHOD branch. also changed the sin/cos computation to ensure the sin/cos vector is perfectly normalized.