Bernard

Say hi to Bernard! He's the most infamous bug in Desmos.

Click the link below to see him better.

Taken from https://www.desmos.com/calculator/allbernard:

Bernard is a fractal shape that frequently appears in the Desmos graphing calculator when plotting implicit equations with too great detail as a result of the quadtree root searching algorithm the calculator uses.

~ Alex

I have figured out more specifically what happens to form Bernard, to my best guess, based on the tweet, the linked paper, and the code:

Desmos's implicit plotting works by first defining, for an implicit equation f(x)=g(x), the function F(x)=f(x)-g(x), so it plots F(x)=0. The goal is to construct line segments that accurately approximate the contour line F(x)=0 to the resolution of a pixel or two without too many lines.

It then divides the screen (more precisely, a region slightly larger than the graph paper by a factor of 1/32 to capture details along the graph paper edges) into four equal regions (four quads), then divides again and again recursively on each quad (more precisely, breadth-first). There are a few conditions for whether a given quad should be divided in a single step, where higher steps take precedence:

  1. Do descend to depth 5 (1024 uniformly-sized quads.)

  2. Don't descend if any part of a given quad is outside of the screen

  3. Don't descend if the quad is too small (about 10 pixels by 10 pixels, converted to math units)

  4. Don't descend if the function F is not defined (NaN) at all four vertices of the quad

  5. Descend if the function F is not defined (NaN) at some, but not all, vertex of the quad

  6. Don't descend if the gradients and function values indicate that F is approximately locally linear within the quad, or if the quad suggest that the function doesn't passes through F(x)=0

  7. Otherwise descend

After descending within one quad, it is effectively replaced with four new quads that the procedure is performed on, so the total number of leaf quads increases by 3. I think here's the main cause of Bernard: if the total number of leaf quads exceeds 214 =16384, then Desmos stops descending. Under certain circumstances, this cutoff lines up perfectly so that quads precisely within the region of Bernard are descended one more time than other quads, giving them 4× as many line segments, so Bernard truly is a region of higher detail.

~ fireflame241

Taken from r/desmos command "!bernard":

The algorithm stops if the total number of quads exceeds 214 =16384. Here's a breakdown of how the quads are descended in a high-detail graph:

  • Point 2 above means that the quads on the edge of the screen (124 of them) don't get descended further. This means that there are only 900 quads left to descend into.
  • The quota for the remaining quads is 16384-124=16260. Those quads can divide two more times to get 900*42 =14400 leaves, and 16260-14400=1860 leaves left to descend.
  • Since each descending quad results in 4 leaf quads, each descend creates 3 new quads. Hence, there are 1860/3=620 extra subdivisions, which results in a ratio of 620/14400 quads that performed the final subdivision.
  • This is basically the ratio of the area of Bernard to the area of the graph paper.