2
$\begingroup$

I used the following code to plot all points calculated when plotting a surface:

ListPlot[
 Reap[Plot3D[Sin[x + y^2], {x, -10, 10}, {y, -10, 10}, 
    RegionFunction -> 
     Function[{x, y, z}, x^2 + y^2 <= 36 && Sin[x] > 0.5], 
    EvaluationMonitor :> Sow[{x, y}], MaxRecursion -> 2]][[-1, 1]]]

It shows this output:

out

It seems that Mathematica first sampled nearly uniformly on the xy-plane, then sampled denser near the trimming curves. Could someone tell me what's the name of the algorithm behind Plot3D? It is adaptive and works well, I want to implement my own one to do some tests.

$\endgroup$
3
  • $\begingroup$ Have a look at tutorial/GraphicsAndSound#1271 and search for "adaptive". Or adaptive sampling, as it's often called. $\endgroup$
    – user87932
    Oct 20, 2023 at 18:14
  • $\begingroup$ @jdp I have read the document, but it seems that it only says "The Wolfram Language adaptively samples the functions" but doesn't mention what the algorithm is or give a rough description about the implementation. $\endgroup$ Oct 23, 2023 at 2:09
  • 2
    $\begingroup$ Yes, I imagine the details might be consider proprietary. Michael Trott authored a set of Mathematica "guide books", and in the one for graphics, he implemented his own version. The idea was to sample, and for cases where the angle between lines changed by more than 10 deg, subdivide. the sampling interval You can see if his excerpts from his book are on the web, and see if the code is available. But that seems to be the general idea. $\endgroup$
    – user87932
    Oct 23, 2023 at 2:22

1 Answer 1

3
$\begingroup$

As I mentioned in the comment above, the actual algorithm is likely to be proprietary, so there's no easy way for a non-employee to know exactly how it works. However, Michael Trott published a series of guide books, one of which covers graphics. The general idea is to sample, check how much angles between lines vary, and subdivide the interval if the angle exceeds a threshold.

One of the exercises involved emulating the behavior of some of the built-in plotting functions, including attempting to do "adaptive sampling". He included his solutions to these exercises, so here's Trott's solution to exercise 6.b (for the chapter on 2d plotting). The text can be found on Google books at the following link on page 466:

GoogleBooksTrottGraphicsGuidebookCover

Unfortunately some of the pages are omitted from Google books, but there is an "improved version" of his original attempt to write a function called "smoothcurve", which does the sampling. Here are screenshots showing the code he produced:

enter image description here

enter image description here

Bear in mind that this was done in version 5.2. It may or may not be the same in the current version, and it may or may not be what's actually used internally in Mathematica. But it should give you an idea of what's going on.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.