cetz-plot
cetz-plot copied to clipboard
Better function sampler with adaptive algorithm to choose additional sample points
Maybe we shoule initially evaluate f at a number of equally spaced sample points specified by samples, then use an adaptive algorithm to choose additional sample points, subdividing a given interval at most max-recursion times.
Since it is connected with straight lines between sampled points, I think we can test for every index i if $\left|y_i - \frac{y_{i-1}+y_{i+1}}{2}\right|\le \text{error threshold}$, if not, add 2 more samples at $\frac{x_{i-1}+x_i}2$ and $\frac{x_i+x_{i+1}}2$, then do recursion down and repeat this process.
Perhaps we can broaden our thinking further. For smooth curves, we can even approximate them using piecewise cubic Bezier curves, which will result in much fewer sampling points.