A Method for Quickly Sampling Points on a Circle

A Method for Quickly Sampling Points on a Circle

I saw a ๐•\mathbb{X}eet that piqued my interest earlier today. Traditionally, to sample a point on the unit circle, youโ€™d generate u,vโˆˆ[0,2ฯ€)u,v \in [0, 2\pi) and then return (cos(u),sin(v))\left(\operatorname{cos}\big(u), \operatorname{sin}\big(v) \right). Can we do this faster and without trigonometry? I got sniped by this question and tried for about an hour. The code is cursed, but the method that prevailed involved estimating sin(ฮธ)\operatorname{sin}\big(\theta) and cos(ฮธ)\operatorname{cos}\big(\theta) as low-degree polynomials and then rotating the result to avoid too much distortion.

N Std. Trig Std. Norm Approx. Trig N-Gon Chord
10k 0.0028 0.0059 0.0019 0.0064
100k 0.0183 0.0254 0.0064 0.0441
1M 0.1157 0.2143 0.0646 0.2278
10M 1.1920 2.2302 0.6297 2.0177
20M 2.4684 4.5936 1.3968 4.2238

If you read the code youโ€™ll notice that Iโ€™m cheating a bit. The method, ๐™ฐ๐š™๐š™๐š›๐š˜๐šก. ๐šƒ๐š›๐š’๐š\texttt{Approx. Trig} actually samples on the perimeter of an egg-shaped region. We can fix this with one more โ€œrotation bitโ€ or one more term in ๐™ฒ๐™พ๐š‚_๐™ฟ\texttt{COS_P} and ๐š‚๐™ธ๐™ฝ_๐™ฟ\texttt{SIN_P}. That said, absolutely bonkers to be thinking about performance like this in Python. Thereโ€™s an interesting paper, A Low Distortion Map Between Disk and Square that discusses ways to (non-uniformly) map [โˆ’1,1]2[-1, 1]^2 to a point in the unit circle. Once again, we have to give it up for the graphics folks. Hereโ€™s a blog discussing Shirley-Chiu and related methods.