Macaw.Draw.Shape.Curve (macaw_draw v0.1.0)

Copy Markdown View Source

The curve interpolators of lines and areas (spec/07 §5), copied from visualize's Visualize.Shape.Curve: how a run of points is joined into a path.

generate(type, points, opts) answers a Macaw.Draw.IR.Path. A type is an atom or its string, read by spelling (spec/06 §1.3); types/0 lists the twelve. Options are read only by the curves that take one: tension (:cardinal, :cardinal_closed, default 0) and alpha (:catmull_rom, default 0.5).

iex> alias Macaw.Draw.Shape.Curve
iex> Curve.generate(:step_after, [{0, 0}, {10, 5}, {20, 2}]) |> Macaw.Draw.IR.Path.to_string()
"M0,0H10V5H20V2"
  • [] or one point is the empty path, for every type, and so is an unknown type: the setters that store a type refuse one as {:unknown, :curve, spelling} (§5.1).
  • :basis, :cardinal, :catmull_rom, :monotone_x, :monotone_y and :natural fall back to :linear for two points; the closed loops close a line with Z.
  • Every type is built in work linear in its points (visualize #414, #462): a curve that reads points by position reads a tuple made once, never Enum.at/2.

Points are {x, y} tuples of numbers; a list holding anything else is {[1, i], {:type, :point}} at the first such point's index.

Summary

Types

A refusal: faults with their paths.

Functions

The uniform cubic B-spline, its ends pulled to the data (§5.3, d3-shape's curveBasis).

The B-spline as a closed loop (§5.3, curveBasisClosed).

The cardinal spline of tension (§5.4); 1 is linear.

The cardinal spline as a closed loop (§5.3, curveCardinalClosed).

The Catmull-Rom spline of parameter alpha (§5.4): 0 uniform, 0.5 centripetal, 1 chordal.

generate(type, points, []).

The path of points through the curve type (§5.1): the empty path for fewer than two points or an unknown type; an option of the wrong type is {[key], {:type, :number}}.

Straight lines between the points: M, then L per point.

The Fritsch-Carlson monotone cubic in x (§5.5).

The monotone cubic in y: :monotone_x with the axes swapped (§5.5).

The natural cubic spline, C² with zero second derivative at both ends (§5.6).

Steps at t of the way to each next point (§5.2): 0.5 is :step, 0 :step_before and 1 :step_after. Consecutive horizontals merge; a vertical is drawn per point.

The twelve curve types, in spec/07 §5.1's order.

Types

curve_type()

@type curve_type() ::
  :linear
  | :step
  | :step_before
  | :step_after
  | :basis
  | :cardinal
  | :catmull_rom
  | :monotone_x
  | :monotone_y
  | :natural
  | :cardinal_closed
  | :basis_closed

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

point()

@type point() :: {number(), number()}

Functions

basis(points)

@spec basis([point()]) :: Macaw.Draw.IR.Path.t() | error()

The uniform cubic B-spline, its ends pulled to the data (§5.3, d3-shape's curveBasis).

basis_closed(points)

@spec basis_closed([point()]) :: Macaw.Draw.IR.Path.t() | error()

The B-spline as a closed loop (§5.3, curveBasisClosed).

cardinal(points, tension)

@spec cardinal([point()], number()) :: Macaw.Draw.IR.Path.t() | error()

The cardinal spline of tension (§5.4); 1 is linear.

cardinal_closed(points, tension)

@spec cardinal_closed([point()], number()) :: Macaw.Draw.IR.Path.t() | error()

The cardinal spline as a closed loop (§5.3, curveCardinalClosed).

catmull_rom(points, alpha)

@spec catmull_rom([point()], number()) :: Macaw.Draw.IR.Path.t() | error()

The Catmull-Rom spline of parameter alpha (§5.4): 0 uniform, 0.5 centripetal, 1 chordal.

generate(type, points)

@spec generate(term(), [point()]) :: Macaw.Draw.IR.Path.t() | error()

generate(type, points, []).

generate(type, points, opts)

@spec generate(term(), [point()], keyword()) :: Macaw.Draw.IR.Path.t() | error()

The path of points through the curve type (§5.1): the empty path for fewer than two points or an unknown type; an option of the wrong type is {[key], {:type, :number}}.

linear(points)

@spec linear([point()]) :: Macaw.Draw.IR.Path.t() | error()

Straight lines between the points: M, then L per point.

monotone_x(points)

@spec monotone_x([point()]) :: Macaw.Draw.IR.Path.t() | error()

The Fritsch-Carlson monotone cubic in x (§5.5).

monotone_y(points)

@spec monotone_y([point()]) :: Macaw.Draw.IR.Path.t() | error()

The monotone cubic in y: :monotone_x with the axes swapped (§5.5).

natural(points)

@spec natural([point()]) :: Macaw.Draw.IR.Path.t() | error()

The natural cubic spline, C² with zero second derivative at both ends (§5.6).

step(points, t)

@spec step([point()], number()) :: Macaw.Draw.IR.Path.t() | error()

Steps at t of the way to each next point (§5.2): 0.5 is :step, 0 :step_before and 1 :step_after. Consecutive horizontals merge; a vertical is drawn per point.

types()

@spec types() :: [curve_type()]

The twelve curve types, in spec/07 §5.1's order.