I saw a eet that piqued my interest earlier today. Traditionally, to sample a point on the unit circle, youโd generate and then return . 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 and 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, actually samples on the perimeter of an egg-shaped region. We can fix this with one more โrotation bitโ or one more term in and . 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 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.