Quote:
Originally Posted by Ether
Using symmetry for the [full-circle] chords problem, you can reduce the length computation to:
Code:
sum += sin(pi*abs(random-random));
... and when the iterations are complete, multiply the sum by 2.
|
In case there are any readers wondering where the above code came from, here's a brief explanation:
Code:
// naive implementation
q1 := 2*pi*random; q2 := 2*pi*random;
theta := abs(q2-q1);
len := 2*sin(theta/2.0);
sum := sum + len;
// get rid of q1 and q2
theta := 2*pi*abs(random-random);
len := 2*sin(theta/2.0);
sum := sum + len;
// get rid of theta
len := 2*sin(pi*abs(random-random));
sum := sum + len;
// get rid of len
sum := sum + 2*sin(pi*abs(random-random));
// move factor of 2 outside the loop
sum := sum + sin(pi*abs(random-random));
Gus pointed out that sin(pi*abs(random-random)) could then be optimized to sin(pi*random).
Some readers may be wondering why. It is due to the interaction between the symmetry of sin(x) around x=pi/2
and the shape of the pi*abs(random-random) distribution.
The distribution of pi*abs(random-random) has the shape f(x):=1-x/pi. That function is symmetric about the point [pi/2,0.5] and has the property that, for any value "a" between -pi/2 and pi/2, f(pi/2-a)+f(pi/2+a) is always equal to 1. This interacts with the symmetry of sin(pi/2ħa) to make the distribution sin(pi*abs(random-random)) be the same as sin(pi*random).