if(lightsource.Circular == true)
{
jitter_u = jitter_u / (lightsource.Area_Size1 - 1) - 0.5;//normalize between -0.5 and 0.5
jitter_v = jitter_v / (lightsource.Area_Size2 - 1) - 0.5;
//this entire block is skipped if jitter_u==0 or jitter_v==0 (jitter_u and jitter_v are already correct for final result)
if(jitter_u!=0 && jitter_v!=0)
{//save time by not doing anything that is not necessary
//remember signs
int usign=jitter_u>0?1:-1;//this is set only once - the entire code inside this "if" uses them
int vsign=jitter_v>0?1:-1;//they are inside because we don't need usign/vsign if jitter_u==0 or jitter_v==0
jitter_u=fabs(jitter_u);//avoid taking absolute values many times after
jitter_v=fabs(jitter_v);
if(jitter_u==jitter_v)//diagonals: a shortcut to avoid expensive division and (fake) trigonometry
{
//if this happens, radius/phi/phi2 are not calculated at all
jitter_u*=usign*sqrt(0.5);//use the signs to calculate the correct values (using what we defined above)
jitter_v*=vsign*sqrt(0.5);
}else
{ //added a bracket to make it obvious that it is all inside the outer "if" (maybe this was confusing before)
//another if: two cases, each have its own radius/phi/phi2 because the second
//case is the mirror across diagonal from the first case (u and v reversed, sin and cos reversed)
if(jitter_u>jitter_v)//comparing the absolute values
{
double radius=jitter_u;//local because it is not the same for > and <
//map tangent of the angle to direct angle.
double phi=0.25*M_PI*jitter_v/jitter_u;
double phi2=phi*phi;//precompute square
//we put the signs back that we remembered before
jitter_u=usign*radius*(1-0.5*phi2);//cosine approximation (max 1.5% error)
jitter_v=vsign*radius*phi*(1-(1/6.)*phi2);//sine approximation (max 0.25% error)
}else
{
double radius=jitter_v;//local because it is not the same for > and <
//map tangent of the angle to direct angle.
double phi=0.25*M_PI*jitter_u/jitter_v;//also local because it is exactly the opposite from above
double phi2=phi*phi;
jitter_u=usign*radius*phi*(1-(1/6.)*phi2);//sin and cos are reversed compared to the previous "if" case
jitter_v=vsign*radius*(1-0.5*phi2);
}
}//end of "else" for the jitter_u==jitter_v test
}
//use the calculated result
jitterAxis1 = axis1 * jitter_u;
jitterAxis2 = axis2 * jitter_v;
}