@baku89/pave / CubicBezier
CubicBezier
A collection of functions to handle a cubic Bézier segment represented as BareSegmentC.
The projection strategy follows bezier-js (MIT, https://github.com/Pomax/bezierjs).
Functions
bounds()
bounds(
arg): [vec2,vec2]
Defined in: CubicBezier.ts:46
Calculates the rect of this Bezier curve.
Parameters
| Parameter | Type |
|---|---|
arg | BareSegmentC |
Returns
[vec2, vec2]
curvature()
curvature(
bezier,loc):number
Defined in: CubicBezier.ts:121
Signed curvature κ = (B′ × B″) / ‖B′‖³ at the specified location. Positive when the curve turns towards its normal side. Returns 0 where the speed vanishes.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
number
cuspTimes()
cuspTimes(
bezier):number[]
Defined in: CubicBezier.ts:198
The t values in (0, 1) where the speed vanishes, so that the curve turns back on itself. A cubic has at most one unless it is degenerate to a line.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
number[]
derivative()
derivative(
bezier,loc):vec2
Defined in: CubicBezier.ts:88
Calculates the curve tangent at the specified t value. Note that this yields a not-normalized vector.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
vec2
divideAtTimes()
divideAtTimes(
segment,times):VertexC[]
Defined in: CubicBezier.ts:370
Parameters
| Parameter | Type |
|---|---|
segment | BareSegmentC |
times | Iterable<number> |
Returns
VertexC[]
extremaTimes()
extremaTimes(
bezier):number[]
Defined in: CubicBezier.ts:135
Sorted, unique t values in (0, 1) where x(t) or y(t) has an extremum. Splitting at these times yields pieces that are monotone along both axes.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
number[]
fromQuadraticBezier()
fromQuadraticBezier(
start,control,point):SegmentC
Defined in: CubicBezier.ts:25
Parameters
| Parameter | Type |
|---|---|
start | vec2 |
control | vec2 |
point | vec2 |
Returns
inflectionTimes()
inflectionTimes(
bezier):number[]
Defined in: CubicBezier.ts:148
Sorted t values in (0, 1) where the curvature changes sign (B′ × B″ = 0).
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
number[]
isStraight()
isStraight(
bezier,tolerance):boolean
Defined in: CubicBezier.ts:400
Parameters
| Parameter | Type | Default value |
|---|---|---|
bezier | BareSegmentC | undefined |
tolerance | number | 0 |
Returns
boolean
isZero()
isZero(
bezier):boolean
Defined in: CubicBezier.ts:387
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
boolean
length()
length(
bezier):number
Defined in: CubicBezier.ts:39
Calculates the length of the Bezier curve. Length is calculated using numerical approximation, specifically the Legendre-Gauss quadrature algorithm.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
number
normal()
normal(
bezier,loc):vec2
Defined in: CubicBezier.ts:114
Calculates the curve normal at the specified t value. Note that this yields a normalized vector.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
vec2
of()
of(
start,control1,control2,point):SegmentC
Defined in: CubicBezier.ts:16
Parameters
| Parameter | Type |
|---|---|
start | vec2 |
control1 | vec2 |
control2 | vec2 |
point | vec2 |
Returns
offset()
offset(
bezier,distance,tolerance):Path
Defined in: CubicBezier.ts:483
Returns the raw parallel curve of the Bézier at the given signed distance along its normal, as an open path of cubic Béziers fitted within tolerance. Where the distance exceeds the radius of curvature the result folds back on itself; use Path.offset for a trimmed result.
Parameters
| Parameter | Type | Default value |
|---|---|---|
bezier | BareSegmentC | undefined |
distance | number | undefined |
tolerance | number | 0.05 |
Returns
point()
point(
bezier,loc):vec2
Defined in: CubicBezier.ts:80
Calculates the point on the curve at the specified t value.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
vec2
project()
project(
bezier,origin):object
Defined in: CubicBezier.ts:290
Finds the on-curve point closest to the specific off-curve point
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
origin | vec2 |
Returns
object
| Name | Type |
|---|---|
distance? | number |
position | vec2 |
t? | number |
secondDerivative()
secondDerivative(
bezier,loc):vec2
Defined in: CubicBezier.ts:96
Calculates the second derivative d²B/dt² at the specified t value.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
vec2
selfIntersectionTimes()
selfIntersectionTimes(
bezier):null| [number,number]
Defined in: CubicBezier.ts:171
The two t values at which the curve passes through the same point, or null if it has no loop. Solved in closed form: with B(t) = at³ + bt² + ct + d, dividing B(s) - B(t) by s - t leaves an equation in s + t and st.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
Returns
null | [number, number]
tangent()
tangent(
bezier,loc):vec2
Defined in: CubicBezier.ts:107
Calculates the curve tangent at the specified t value. Unlike derivative, this yields a normalized vector.
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
vec2
tangentTimes()
tangentTimes(
bezier,direction):number[]
Defined in: CubicBezier.ts:220
Sorted t values in (0, 1) where the tangent is parallel to direction. These are the points where a line along direction touches the curve, and where the curve is farthest along the perpendicular of direction.
Parameters
| Parameter | Type | Description |
|---|---|---|
bezier | BareSegmentC | - |
direction | vec2 | The direction to compare the tangent with. Its length does not matter. |
Returns
number[]
tangentTimesFrom()
tangentTimesFrom(
bezier,origin):number[]
Defined in: CubicBezier.ts:236
Sorted t values in (0, 1) where the line from origin to the curve is tangent to it. The condition (B(t) − origin) × B′(t) = 0 is a quintic, so the roots are found by bracketing sign changes and bisecting.
Parameters
| Parameter | Type | Description |
|---|---|---|
bezier | BareSegmentC | - |
origin | vec2 | The point the tangents pass through |
Returns
number[]
toTime()
toTime(
bezier,loc):number
Defined in: CubicBezier.ts:59
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
loc | SegmentLocation |
Returns
number
trim()
trim(
bezier,start,end):SegmentC
Defined in: CubicBezier.ts:330
Parameters
| Parameter | Type |
|---|---|
bezier | BareSegmentC |
start | SegmentLocation |
end | SegmentLocation |