Calculating sine and cosine together with vectors

Very often you want to find the sine AND cosine of an angle. It is used all the time in computer graphics for all kinds of rotations.
The most obvious way to do it is something like this:

float sin_angle = sin(angle);
float cos_angle = cos(angle);

But trigonometric functions are not that fast, and here we are using two of them. However, if you are working with a system capable of vector processing, such as GLSL shaders, then you can find both sine and cosine in a single function call:

#define HALFPI 1.57079633
vec2 sin_cos_angle = sin(vec2(angle,HALPI-angle));

It’s quite obvious in hindsight but I have to admit I just never thought of that. I recently saw it on some code on ShaderToy but I can’t remember where. I’d like to give credit so I will continue to search for the person whose code I saw.

Published by

samuraigoblin

Just a programmer who loves sushi.

One thought on “Calculating sine and cosine together with vectors”

Leave a Reply

Your email address will not be published. Required fields are marked *