d3 / d3-shape

Graphical primitives for visualization, such as lines and areas.
https://d3js.org/d3-shape
ISC License
2.48k stars 310 forks source link

curveCubicX? #118

Open danburzo opened 6 years ago

danburzo commented 6 years ago

Issue was originally in d3-interpolate#49

I'm proposing the addition of a new type of curve for d3-shape — aptly called curveCubicX by @mbostock on the original thread — that treats the Y coordinate as a function of the X coordinate, such that for any X coordinate there's a single corresponding Y value.

In the image below, the proposed curveCubicX in comparison with curveNatural and curveBasis:

36937290-cffd7590-1f19-11e8-82ce-8ede07b6e619

(the cubic curve is generated with a cubic spline interpolator through small line segments joining 1000 samples of <x, f(x)>)

The algorithm for the interpolator is described on Wikipedia (also of note is its talk page, which makes it apparent that the page suffered various incarnations of the algorithm, of various readability levels).

There are also some demos and implementations of the algorithm:

However, these are all oriented towards finding the interpolator function, and any SVG paths are drawn with sampling the function, and I am severely out of my comfort zone in regards to obtaining the Bézier control points needed to draw the SVG path, so I'm hoping for some pointers or ideas on the matter...

As for the utility: I started with trying to recreate the Curves interface in Adobe Photoshop, which seems to be using such a cubic spline to interpolate the values. For data visualization, on the one hand it maintains Y being a function of X which makes sense in most types of charts, and the spline does pass through all the points, but on the other hand it tends to overshoot much more than other types of curves.

danburzo commented 6 years ago

Back from the field: I could not find any literature on how to implement this as a Bézier spline (it's possibly hidden in terms/explanations I don't understand) — but I did manage to independently re-implement curveNatural and curveBasis based on this explanation :). Unfortunately I'm lacking the theoretical foundation to have any intuition on how to use the algorithms for parametric splines (descriptions of which abound) to a non-parametric (explicit) spline... hopefully someone on Math StackExchange has some ideas.