Curves (curves v0.2.3)

Copy Markdown View Source

The best way to explore this library is through the interactive livebook.

Quickstart

The fastest way to get started is with the predefined bezier curves.

bezier_type = :ease_in
curve = Curves.define_bezier(bezier_type)

t = 0.24 # i.e. 24%  from the beginning to the end of the curve.
{x, y} = Curves.solve!(curve, t)

assert is_float(x)
assert is_float(y)

For a list of all predefined bezier_types, use Curves.Bezier.Predefined.list/0

Custom curves

You can also create a custom bezier curve by passing in a list of {x, y} tuples. They can be any combination of floats and integers.

curve = Curves.define_bezier([
# {x,   y}
  {0,   0},
  {0,   0.5},
  {0.8, 0.4},
  {1,   1}
])

t = 0.248

{x, y} = Curves.solve!(curve, t)

assert is_float(x)
assert is_float(y)

Summary

Types

t()

Float between 0.0 and 1.0, representing a percentage of progress from the first to last point.

Functions

Build a new Bezier Curve struct.

Given a struct, and t, find the point along the curve

The raising version of solve/3

Take n samples, evenly spaced, from the curve.

The raising version of take/3

Types

t()

@type t() :: float()

Float between 0.0 and 1.0, representing a percentage of progress from the first to last point.

Functions

define_bezier(points, opts \\ [])

Build a new Bezier Curve struct.

Examples

iex> c = Curves.define_bezier([{0.1, 0.9}, {0.5, 0.9}, {0.5, 0.1}, {0.75, 0.1}])
iex> is_struct(c, Curves.Bezier.Curve)
true

solve(curve, t, opts \\ [])

@spec solve(Curves.Bezier.Curve.t(), t(), Curves.Utils.Types.opts()) ::
  {:ok, Curves.Utils.Types.point_tuple()} | {:error, term()}

Given a struct, and t, find the point along the curve

Examples

iex> c = Curves.define_bezier([{0.1, 0.9}, {0.5, 0.9}, {0.5, 0.1}, {0.75, 0.1}])
iex> Curves.solve(c, 0.3)
{:ok, {0.3695499897003174, 0.727199912071228}}

Options

  • :float_dtype (default: nil) | If set to an integer, passes results to Float.round(_, precision)

solve!(curve, t, opts \\ [])

The raising version of solve/3

take(curve, n, opts \\ [])

Take n samples, evenly spaced, from the curve.

take!(curve, n, opts \\ [])

The raising version of take/3