Import std.math.curves
std.math.curves
Bezier curves, splines, and paths through points, in 2D and 3D.
Curves and splines in 2D and 3D: a point t of the way along a quadratic or cubic Bezier, a Hermite curve, a Catmull-Rom segment, or a uniform cubic B-spline segment; and Curve2 and Curve3, which join a run of points into one curve, through them or near them, and go along it by its length, as a path's followers and its evenly spaced fence posts need.
Examples
Local a = Vec2(0, 0)
Local c = Vec2(2, 0)
Expect(QuadraticBezier(a, Vec2(1, 1), c, 0.5) == Vec2(1, 0.5))
Expect(CatmullRom(Vec2(-1, 0), a, c, Vec2(3, 0), 0) == a)
Expect(Hermite(a, Vec2(1, 0), c, Vec2(1, 0), 1) == c)
Expect(IsClose(BSpline(a, a, a, a, 0.5), a))
Local up = Curve3.CatmullRom([Vec3(0, 0, 0), Vec3(0, 10, 0)])
Expect(IsClose(up.Length, 10, 0.01))std/math/curves.fb
Types
Curve2
class
Public Class Curve2
A curve in 2D from a Function of t from 0 to 1, which it goes along by length too, from a table of samples' lengths, its straight lines' between them, so more samples follow its bends closer.
Examples
Local road = Curve2.CatmullRom([Vec2(0, 0), Vec2(10, 0), Vec2(20, 0)])
Expect(IsClose(road.Length, 20, 0.01))
Expect(IsClose(road.PointAtDistance(5), Vec2(5, 0), 0.01))
Expect(road.Sample(5).Count == 5)-
The curve
pointgives for t from 0 to 1, measured atsamplesplaces along it. Fewer than one sample panics. -
@PanicsAtCaller Public Static Method CatmullRom(points: Vec2[], closed: Bool = False, samples: Int = 64): Curve2static method #The Catmull-Rom spline through the
points, each of them on it, around to the first whenclosed, measured atsamplesplaces along each segment. Fewer than twopoints, or than one sample, panics. -
@PanicsAtCaller Public Static Method BSpline(points: Vec2[], closed: Bool = False, samples: Int = 64): Curve2static method #The uniform cubic B-spline the
pointsshape, near them rather than through them: an open one starts and ends at its ends, and a closed one goes around. It's measured atsamplesplaces along each segment. Fewer than twopoints, or than one sample, panics. -
@PanicsAtCaller Public Static Method Bezier(points: Vec2[], samples: Int = 64): Curve2static method #The cubic Beziers one after another, each from the last one's end: a start, then two control
pointsand an end for each, so 3n + 1pointsin all, measured atsamplesplaces along each. Any other count ofpoints, or fewer than one sample, panics. -
How long the curve is, as its samples measure it.
-
The point the curve's Function gives for
t, kept from 0 to 1. A NaNtpanics. -
The point so far along the curve, kept from 0 to its Length. A NaN
distancepanics. -
countpoints along the curve the same distance apart, from its start to its end. Fewer than two panics.
Curve3
class
Public Class Curve3
A curve in 3D from a Function of t from 0 to 1, which it goes along by length too, from a table of samples' lengths, its straight lines' between them, so more samples follow its bends closer.
-
The curve
pointgives for t from 0 to 1, measured atsamplesplaces along it. Fewer than one sample panics. -
@PanicsAtCaller Public Static Method CatmullRom(points: Vec3[], closed: Bool = False, samples: Int = 64): Curve3static method #The Catmull-Rom spline through the
points, each of them on it, around to the first whenclosed, measured atsamplesplaces along each segment. Fewer than twopoints, or than one sample, panics. -
@PanicsAtCaller Public Static Method BSpline(points: Vec3[], closed: Bool = False, samples: Int = 64): Curve3static method #The uniform cubic B-spline the
pointsshape, near them rather than through them: an open one starts and ends at its ends, and a closed one goes around. It's measured atsamplesplaces along each segment. Fewer than twopoints, or than one sample, panics. -
@PanicsAtCaller Public Static Method Bezier(points: Vec3[], samples: Int = 64): Curve3static method #The cubic Beziers one after another, each from the last one's end: a start, then two control
pointsand an end for each, so 3n + 1pointsin all, measured atsamplesplaces along each. Any other count ofpoints, or fewer than one sample, panics. -
How long the curve is, as its samples measure it.
-
The point the curve's Function gives for
t, kept from 0 to 1. A NaNtpanics. -
The point so far along the curve, kept from 0 to its Length. A NaN
distancepanics. -
countpoints along the curve the same distance apart, from its start to its end. Fewer than two panics.
Functions
QuadraticBezier
function
Public Function QuadraticBezier(a: Vec2, b: Vec2, c: Vec2, t: Float): Vec2
The point t of the way along the quadratic Bezier from a to c, pulled toward b.
QuadraticBezier
function
Public Function QuadraticBezier(a: Vec3, b: Vec3, c: Vec3, t: Float): Vec3
The point t of the way along the quadratic Bezier from a to c, pulled toward b.
CubicBezier
function
Public Function CubicBezier(a: Vec2, b: Vec2, c: Vec2, d: Vec2, t: Float): Vec2
The point t of the way along the cubic Bezier from a to d, pulled toward b, then c.
Examples
Local a = Vec2(0, 0)
Local d = Vec2(1, 0)
Local arch = Function(t: Float) => CubicBezier(a, Vec2(0, 1), Vec2(1, 1), d, t)
Expect(arch(0) == a && arch(1) == d)
Expect(IsClose(arch(0.5), Vec2(0.5, 0.75)))
CubicBezier
function
Public Function CubicBezier(a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: Float): Vec3
The point t of the way along the cubic Bezier from a to d, pulled toward b, then c.
Hermite
function
Public Function Hermite(start: Vec2, startTangent: Vec2, end: Vec2, endTangent: Vec2, t: Float): Vec2
The point t of the way along the Hermite curve from start, going in its tangent's direction,
to end, arriving in its: a cubic Bezier with control points a third of each tangent along.
Hermite
function
Public Function Hermite(start: Vec3, startTangent: Vec3, end: Vec3, endTangent: Vec3, t: Float): Vec3
The point t of the way along the Hermite curve from start, going in its tangent's direction,
to end, arriving in its: a cubic Bezier with control points a third of each tangent along.
CatmullRom
function
Public Function CatmullRom(a: Vec2, b: Vec2, c: Vec2, d: Vec2, t: Float): Vec2
The point t of the way from b to c on the uniform Catmull-Rom spline through a, b,
c, and d: a Hermite curve whose tangents at b and c point from the points on either
side.
CatmullRom
function
Public Function CatmullRom(a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: Float): Vec3
The point t of the way from b to c on the uniform Catmull-Rom spline through a, b,
c, and d: a Hermite curve whose tangents at b and c point from the points on either
side.
BSpline
function
Public Function BSpline(a: Vec2, b: Vec2, c: Vec2, d: Vec2, t: Float): Vec2
The point t of the way along the segment of the uniform cubic B-spline that a, b, c, and
d shape, which passes near b and c rather than through them, and joins the next segment
smoothly.
BSpline
function
Public Function BSpline(a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: Float): Vec3
The point t of the way along the segment of the uniform cubic B-spline that a, b, c, and
d shape, which passes near b and c rather than through them, and joins the next segment
smoothly.
Report an issue with this page or with FutureBlitz on GitHub.