Skip to main content

curves

Modules

ModuleDescription
modeling/curves/bezierRepresents a bezier easing function.
modeling/curvesCurves are n-dimensional mathematical constructs that define a path from point 0 to point 1.

Functions

NameDescription
lengths(segments, bezier)Divides the bezier curve into line segments and returns the cumulative length of those segments as an array. Utility function used to calculate the curve's approximate length and determine the equivalence between arc length and time.
distanceBetween(a, b)Calculates the Euclidean distance between two n-dimensional points.

Typedefs

NameDescription
bezierRepresents a bezier easing function.

modeling/curves/bezier

Represents a bezier easing function.

See: bezier for data structure information.

modeling/curves/bezier.arcLengthToT([options], bezier)

Convert a given arc length along a bezier curve to a t value. Useful for generating equally spaced points along a bezier curve.

Kind: static method of modeling/curves/bezier
Returns: a number in the [0, 1] interval or NaN if the arcLength is negative or greater than the total length of the curve.

ParamTypeDefaultDescription
[options]Objectoptions for construction
[options.distance]Number0the distance along the bezier curve for which we want to find the corresponding t value.
[options.segments]Number100the number of segments to use when approximating the curve length.
bezierObjecta bezier curve.

Example

const points = [];
const segments = 9; // this will generate 10 equally spaced points
const increment = bezier.length(100, bezierCurve) / segments;
for(let i = 0; i <= segments; i++) {
const t = bezier.arcLengthToT({distance: i * increment}, bezierCurve);
const point = bezier.valueAt(t, bezierCurve);
points.push(point);
}
return points;

modeling/curves/bezier.create(points)

Creates an object representing a bezier easing curve. Curves can have both an arbitrary number of control points, and an arbitrary number of dimensions.

Kind: static method of modeling/curves/bezier
Returns: bezier - a new bezier data object

ParamTypeDescription
pointsArrayAn array with at least 2 elements of either all numbers, or all arrays of numbers that are the same size.

Example

const b = bezier.create([0,10]) // a linear progression from 0 to 10
const b = bezier.create([0, 0, 10, 10]) // a symmetrical cubic easing curve that starts slowly and ends slowly from 0 to 10
const b = bezier.create([0,0,0], [0,5,10], [10,0,-5], [10,10,10]]) // a cubic 3 dimensional easing curve that can generate position arrays for modelling
// Usage
let position = bezier.valueAt(t,b) // where 0 < t < 1
let tangent = bezier.tangentAt(t,b) // where 0 < t < 1

modeling/curves/bezier.length(segments, bezier)

Approximates the length of the bezier curve by sampling it at a sequence of points, then adding up all the distances. This is equivalent to flattening the curve into lines and adding up all the line lengths.

Kind: static method of modeling/curves/bezier
Returns: an approximation of the curve's length.

ParamTypeDescription
segmentsNumberthe number of segments to use when approximating the curve length.
bezierObjecta bezier curve.

Example

const b = bezier.create([[0, 0], [0, 10]]);
console.log(length(100, b)) // output 10

modeling/curves/bezier.tangentAt(t, bezier)

Calculates the tangent at a specific position along a bezier easing curve. For multidimensional curves, the tangent is the slope of each dimension at that point. See the example called extrudeAlongPath.js

Kind: static method of modeling/curves/bezier
Returns: arraynumber - the tangent at the requested position.

ParamTypeDescription
tnumber: the position of which to calculate the bezier's tangent value; 0 < t < 1
bezierObject: an array with at least 2 elements of either all numbers, or all arrays of numbers that are the same size.

Example

const b = bezier.create([[0,0,0], [0,5,10], [10,0,-5], [10,10,10]]) // a cubic 3 dimensional easing curve that can generate position arrays for modelling
let tangent = bezier.tangentAt(t, b)

modeling/curves/bezier.valueAt(t, bezier)

Calculates the value at a specific position along a bezier easing curve. For multidimensional curves, the tangent is the slope of each dimension at that point. See the example called extrudeAlongPath.js to see this in use. Math and explanation comes from https://www.freecodecamp.org/news/nerding-out-with-bezier-curves-6e3c0bc48e2f/

Kind: static method of modeling/curves/bezier
Returns: arraynumber - the value at the requested position.

ParamTypeDescription
tnumber: the position of which to calculate the value; 0 < t < 1
bezierObject: a bezier curve created with bezier.create().

Example

const b = bezier.create([0,0,0], [0,5,10], [10,0,-5], [10,10,10]]) // a cubic 3 dimensional easing curve that can generate position arrays for modelling
let position = bezier.valueAt(t,b) // where 0 < t < 1

modeling/curves

Curves are n-dimensional mathematical constructs that define a path from point 0 to point 1.

Example

const { bezier } = require('@jscad/modeling').curves

lengths(segments, bezier)

Divides the bezier curve into line segments and returns the cumulative length of those segments as an array. Utility function used to calculate the curve's approximate length and determine the equivalence between arc length and time.

Kind: global function
Returns: an array containing the cumulative length of the segments.

ParamTypeDescription
segmentsNumberthe number of segments to use when approximating the curve length.
bezierObjecta bezier curve.

Example

const b = bezier.create([[0, 0], [0, 10]]);
const totalLength = lengths(100, b).pop(); // the last element of the array is the curve's approximate length

distanceBetween(a, b)

Calculates the Euclidean distance between two n-dimensional points.

Kind: global function
Returns: Number - - distance.

ParamTypeDescription
aArrayfirst operand.
bArraysecond operand.

Example

const distance = distanceBetween([0, 0], [0, 10]); // calculate distance between 2D points
console.log(distance); // output 10

bezier

Represents a bezier easing function.

Kind: global typedef
Properties

NameTypeDescription
pointsArrayThe control points for the bezier curve. The first and last point will also be the start and end of the curve
pointTypestringA reference to the type and dimensionality of the points that the curve was created from
dimensionsnumberThe dimensionality of the bezier
permutationsArrayA pre-calculation of the bezier algorithm's co-efficients
tangentPermutationsArrayA pre-calculation of the bezier algorithm's tangent co-efficients