A CubicSpline2d is a cubic Bézier curve
in 2D defined by four control points. This module contains functionality for
Construct a spline from its four control points:
exampleSpline =
CubicSpline2d.fromControlPoints
( Point2d.fromCoordinates ( 1, 1 )
, Point2d.fromCoordinates ( 3, 4 )
, Point2d.fromCoordinates ( 5, 1 )
, Point2d.fromCoordinates ( 7, 4 )
)Construct a spline in Hermite form, from the position and derivative values at its start and end points, like so:
The spline is based on a parameter that ranges from 0 to 1; as a result, in most cases the length of each derivative vector should be roughly equal to the length of the resulting spline.
Convert a quadratic spline into the equivalent cubic spline (every quadratic spline can be represented exactly as a cubic spline).
quadraticSpline =
QuadraticSpline2d.fromControlPoints
( Point2d.fromCoordinates ( 0, 0 )
, Point2d.fromCoordinates ( 3, 0 )
, Point2d.fromCoordinates ( 3, 3 )
)
CubicSpline2d.fromQuadraticSpline quadraticSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 0, 0 )
--> , Point2d.fromCoordinates ( 2, 0 )
--> , Point2d.fromCoordinates ( 3, 1 )
--> , Point2d.fromCoordinates ( 3, 3 )
--> )Get the control points of a spline as a tuple.
( p1, p2, p3, p4 ) =
CubicSpline2d.controlPoints exampleSpline
--> p1 = Point2d.fromCoordinates ( 1, 1 )
--> p2 = Point2d.fromCoordinates ( 3, 4 )
--> p3 = Point2d.fromCoordinates ( 5, 1 )
--> p4 = Point2d.fromCoordinates ( 7, 4 )Get the start point of a spline. This is equal to the spline's first control point.
CubicSpline2d.startPoint exampleSpline
--> Point2d.fromCoordinates ( 1, 1 )Get the end point of a spline. This is equal to the spline's last control point.
CubicSpline2d.endPoint exampleSpline
--> Point2d.fromCoordinates ( 7, 4 )Get the start derivative of a spline. This is equal to three times the vector from the spline's first control point to its second.
CubicSpline2d.startDerivative exampleSpline
--> Vector2d.fromComponents ( 6, 9 )Get the end derivative of a spline. This is equal to three times the vector from the spline's third control point to its fourth.
CubicSpline2d.endDerivative exampleSpline
--> Vector2d.fromComponents ( 6, 9 )Get a point along a spline, based on a parameter that ranges from 0 to 1. A parameter value of 0 corresponds to the start point of the spline and a value of 1 corresponds to the end point.
CubicSpline2d.pointOn exampleSpline 0
--> Point2d.fromCoordinates ( 1, 1 )
CubicSpline2d.pointOn exampleSpline 0.5
--> Point2d.fromCoordinates ( 4, 2.5 )
CubicSpline2d.pointOn exampleSpline 1
--> Point2d.fromCoordinates ( 7, 4 )Get the deriative value at a point along a spline, based on a parameter that ranges from 0 to 1. A parameter value of 0 corresponds to the start derivative of the spline and a value of 1 corresponds to the end derivative.
CubicSpline2d.derivative exampleSpline 0
--> Vector2d.fromComponents ( 6, 9 )
CubicSpline2d.derivative exampleSpline 0.5
--> Vector2d.fromComponents ( 6, 0 )
CubicSpline2d.derivative exampleSpline 1
--> Vector2d.fromComponents ( 6, 9 )Evaluate a spline at a given parameter value, returning the point on the spline at that parameter value and the derivative with respect to that parameter value;
CubicSpline2d.evaluate spline tis equivalent to
( CubicSpline2d.pointOn spline t
, CubicSpline2d.derivative spline t
)but is more efficient.
Reverse a spline so that the start point becomes the end point, and vice versa.
CubicSpline2d.reverse exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 7, 4 )
--> , Point2d.fromCoordinates ( 5, 1 )
--> , Point2d.fromCoordinates ( 3, 4 )
--> , Point2d.fromCoordinates ( 1, 1 )
--> )Scale a spline about the given center point by the given scale.
CubicSpline2d.scaleAbout Point2d.origin 2 exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 2, 2 )
--> , Point2d.fromCoordinates ( 6, 8 )
--> , Point2d.fromCoordinates ( 10, 2 )
--> , Point2d.fromCoordinates ( 14, 8 )
--> )Rotate a spline counterclockwise around a given center point by a given angle (in radians).
exampleSpline
|> CubicSpline2d.rotateAround Point2d.origin
(degrees 90)
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( -1, 1 )
--> , Point2d.fromCoordinates ( -4, 3 )
--> , Point2d.fromCoordinates ( -1, 5 )
--> , Point2d.fromCoordinates ( -4, 7 )
--> )Translate a spline by a given displacement.
displacement =
Vector2d.fromComponents ( 2, 3 )
CubicSpline2d.translateBy displacement exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 3, 4 )
--> , Point2d.fromCoordinates ( 5, 7 )
--> , Point2d.fromCoordinates ( 7, 4 )
--> , Point2d.fromCoordinates ( 9, 7 )
--> )Mirror a spline across an axis.
CubicSpline2d.mirrorAcross Axis2d.x exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 1, -1 )
--> , Point2d.fromCoordinates ( 3, -4 )
--> , Point2d.fromCoordinates ( 5, -1 )
--> , Point2d.fromCoordinates ( 7, -4 )
--> )Take a spline defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame.
localFrame =
Frame2d.atPoint (Point2d.fromCoordinates ( 1, 2 ))
CubicSpline2d.relativeTo localFrame exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 0, -1 )
--> , Point2d.fromCoordinates ( 2, 2 )
--> , Point2d.fromCoordinates ( 4, -1 )
--> , Point2d.fromCoordinates ( 6, 2 )
--> )Take a spline considered to be defined in local coordinates relative to a given reference frame, and return that spline expressed in global coordinates.
localFrame =
Frame2d.atPoint (Point2d.fromCoordinates ( 1, 2 ))
CubicSpline2d.placeIn localFrame exampleSpline
--> CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 2, 3 )
--> , Point2d.fromCoordinates ( 4, 6 )
--> , Point2d.fromCoordinates ( 6, 3 )
--> , Point2d.fromCoordinates ( 8, 6 )
--> )Split a spline into two roughly equal halves. Equivalent to splitAt 0.5.
CubicSpline2d.bisect exampleSpline
--> ( CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 1, 1 )
--> , Point2d.fromCoordinates ( 2, 2.5 )
--> , Point2d.fromCoordinates ( 3, 2.5 )
--> , Point2d.fromCoordinates ( 4, 2.5 )
--> )
--> , CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 4, 2.5 )
--> , Point2d.fromCoordinates ( 5, 2.5 )
--> , Point2d.fromCoordinates ( 6, 2.5 )
--> , Point2d.fromCoordinates ( 7, 4 )
--> )
--> )Split a spline at a particular parameter value (in the range 0 to 1), resulting in two smaller splines.
CubicSpline2d.splitAt 0.75 exampleSpline
--> ( CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 1, 1 )
--> , Point2d.fromCoordinates ( 2.5, 3.25 )
--> , Point2d.fromCoordinates ( 4, 2.125 )
--> , Point2d.fromCoordinates ( 5.5, 2.6875 )
--> )
--> , CubicSpline2d.fromControlPoints
--> ( Point2d.fromCoordinates ( 5.5, 2.6875 )
--> , Point2d.fromCoordinates ( 6, 2.875 )
--> , Point2d.fromCoordinates ( 6.5, 3.25 )
--> , Point2d.fromCoordinates ( 7, 4 )
--> )
--> )A spline that has been parameterized by arc length.
Build an arc length parameterization of the given spline, within
a given tolerance. Generally speaking, all operations on the resulting
ArcLengthParameterized value will be accurate to within the given arc length
tolerance.
tolerance =
1.0e-4
parameterizedSpline =
CubicSpline2d.arcLengthParameterized
tolerance
exampleSplineFind the total arc length of a spline. This will be accurate to within the
tolerance given when calling arcLengthParameterized.
arcLength : Float
arcLength =
CubicSpline2d.arcLength parameterizedSpline
arcLength
--> 7.0952Try to get the point along a spline at a given arc length. For example, to
get the point a quarter of the way along exampleSpline:
CubicSpline2d.pointAlong parameterizedSpline
(arcLength / 4)
--> Just (Point2d.fromCoordinates ( 2.2681, 2.2114 ))Note that this is not the same as evaulating at a parameter value of 1/4:
CubicSpline2d.pointOn exampleSpline 0.25
--> Point2d.fromCoordinates ( 2.5, 2.3125 )If the given arc length is less than zero or greater than the arc length of the
spline, Nothing is returned.
Try to get the tangent direction along a spline at a given arc length. To
get the tangent direction a quarter of the way along exampleSpline:
CubicSpline2d.tangentAlong parameterizedSpline
(arcLength / 4)
--> Just (Direction2d.fromAngle (degrees 26.5611))If the given arc length is less than zero or greater than the arc length of the
spline (or if the derivative of the spline happens to be exactly zero at the
given arc length), Nothing is returned.
Try to get the parameter value along a spline at a given arc length. If the
given arc length is less than zero or greater than the arc length of the spline,
returns Nothing.
CubicSpline2d.arcLengthToParameterValue
parameterizedSpline
(arcLength / 4)
--> Just 0.2113Try to get the arc length along a spline at a given parameter value. If the
given parameter value is less than zero or greater than one, returns Nothing.
CubicSpline2d.parameterValueToArcLength
parameterizedSpline
0.25
--> 2.0269Low level functionality that you are unlikely to need to use directly.
Find the magnitude of the derivative to a spline at a particular parameter value;
CubicSpline2d.derivativeMagnitude spline tis equivalent to
Vector2d.length (CubicSpline2d.derivative spline t)but more efficient since it avoids any intermediate Vector2d allocation.
Find an upper bound on the magnitude of the second derivative of a spline.