elm-geometry icon indicating copy to clipboard operation
elm-geometry copied to clipboard

Add "PolyCubicSpline2d"

Open prange opened this issue 5 years ago • 2 comments

When drawing svg it is often the case that we want to draw smooth lines using Bezier curves. It would really be handy with a function to convert a polyline to a list of Bezier curves that are connected and where the control points make sense (based on some smoothing parameter perhaps)

and of course it would be really cool to have a corresponding function to render such a datatructure to svg.

What do you think?

prange avatar May 12 '19 21:05 prange

Hey @prange! I've thought for a while it would be good to have a function in elm-geometry for constructing centripetal Catmull-Rom splines, which are a series of smoothly-connected cubic splines through a set of points. The API would probably be something likely

CubicSpline2d.centripetalCatmullRom : List Point2d -> List CubicSpline2d

although it might have to be a bit more complex to allow for different end conditions (free, clamped, closed etc.) Does that seem close to what you're looking for?

ianmackenzie avatar May 13 '19 17:05 ianmackenzie

Or if we want to be a bit more opinionated, something like:

CubicSpline2d.interpolate : List Point2d -> List CubicSpline2d

CubicSpline2d.closed : List Point2d -> List CubicSpline2d

CubicSpline2d.interpolateWith : 
    { start : BoundaryCondition, end : BoundaryCondition }
    -> List Point2d
    -> List CubicSpline2d

-- zero curvature at the boundary
free : BoundaryCondition

-- fixed first derivative at the boundary
fixed : Vector2d -> BoundaryCondition

which you could use as

-- Create a centripetal Catmull-Rom curve through
-- the given points, with free ends
splines1 =
    CubicSpline2d.interpolate points

-- Create a closed-loop Catmull-Rom curve
-- through the given points
splines2 =
    CubicSpline2d.closed points

splines3 =
    CubicSpline2d.interpolateWith
        { start = CubicSpline2d.fixed v1, end = CubicSpline2d.free }
        points

ianmackenzie avatar May 13 '19 18:05 ianmackenzie