cetz-plot icon indicating copy to clipboard operation
cetz-plot copied to clipboard

Better function sampler with adaptive algorithm to choose additional sample points

Open szdytom opened this issue 8 months ago • 1 comments

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.

Example of adaptive algorithm

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.

szdytom avatar Mar 08 '25 01:03 szdytom

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.

liuguangxi avatar Mar 14 '25 04:03 liuguangxi