{"url":"https://api.github.com/gists/8027637","forks_url":"https://api.github.com/gists/8027637/forks","commits_url":"https://api.github.com/gists/8027637/commits","id":"8027637","node_id":"MDQ6R2lzdDgwMjc2Mzc=","git_pull_url":"https://gist.github.com/8027637.git","git_push_url":"https://gist.github.com/8027637.git","html_url":"https://gist.github.com/8027637","files":{".block":{"filename":".block","type":"text/plain","language":null,"raw_url":"https://gist.githubusercontent.com/mbostock/8027637/raw/703d310b399098a243a76a50bc209167e924cfd2/.block","size":17,"truncated":false,"content":"license: gpl-3.0\n"},"README.md":{"filename":"README.md","type":"text/plain","language":"Markdown","raw_url":"https://gist.githubusercontent.com/mbostock/8027637/raw/4dd8b1299e50b29872cf4c7cff8d074955657ac1/README.md","size":1306,"truncated":false,"content":"This page demonstrates a simple approximate algorithm for finding the closest point on any given SVG path element. Although the algorithm is not guaranteed to return the best answer, the answer is reasonably good, and the accuracy is tunable at the expense of performance. It is based on Mike Kamermans’ excellent [Primer on Bézier Curves](https://pomax.github.io/bezierinfo/#projections).\n\nA coarse linear scan of the path provides an initial guess. Then, a binary search improves the guess to the desired level of precision (here, about 1px). The coarseness of the initial scan is configurable; for paths where there may be multiple close points at different lengths along the path, such as at intersections, a finer initial scan is needed to avoid converging on a suboptimal answer.\n\nKnowing the closest path to a given point is useful for [multi-line charts](/mbostock/3884955) in the same way the [Voronoi tessellation](/mbostock/4060366) is useful for [scatterplots](/njvack/1405439): it makes it easier to select or highlight elements using the mouse. Instead of requiring the user to hover over a line precisely, you can use this algorithm to find the line closest to the mouse. Alternatively, you can compute a [Voronoi diagram for lines](/mbostock/8027835) by sampling points along each path.\n"},"index.html":{"filename":"index.html","type":"text/html","language":"HTML","raw_url":"https://gist.githubusercontent.com/mbostock/8027637/raw/4b9fef28ce464abd23049687c52336e91396f670/index.html","size":2607,"truncated":false,"content":"\n\n\n