using tmp variables to avoid sin() and cos() calls inside the for loops