A scatter plot: Delaunay, pixel space and a distance limit
A pointer is never exactly on a data point. A bisector finds the nearest day on sorted data in a few steps, a Delaunay triangulation finds the nearest dot of a scatter plot, and unsorted data gives a wrong answer with no error.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 4 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write nearestDay(rows, date, maxDays) that returns the nearest row or null, and handles an empty array.
Write isSortedBy(rows, accessor) and use it to throw a clear error Rows must be sorted by date before building a chart.
Explain it without notes
Why does bisector.center give the day under the pointer, but bisector.left can crash on the right edge?
What happens if you call a bisector on unsorted data, and why is that worse than an error?
Why build the Delaunay index in pixel space?
Console
(nothing was printed)Expected console output
pointer (300, 100) is 16.4 C and 2880 rides
nearest in pixels: 2025-04-09 (16.7 C, 2709 rides) | 14 px away
nearest in data units: 2025-04-23 (16.9 C, 2883 rides) | 17 px away
empty corner (5, 5): find says row 359 -> 197 px away | with a 30 px limit: -1
on the cloud (300, 100) with a 30 px limit: 98
empty list: NaN | one point: 0
two identical points, asking at each of the three: 0, 0, 2
quadtree.find with a radius: 2025-04-09 | in an empty corner: undefined