Cubic Splines in Typst

I implemented cubic splines in Typst: Philipp Dobler / typst-splines · GitLab
I don’t plan on making it a package, but I thought it might still be useful for some people.
The following example shows how to create a spline from a set of data points and plot it using lilaq

#import "lib.typ": *
#import "@preview/lilaq:0.3.0" as lq

#{
  let x = lq.linspace(0, 2 * calc.pi, num: 5)
  let y = x.map(x => calc.sin(x))

  let f = cubic-splines(x, y)
  let xs = lq.linspace(x.at(0), x.at(-1), num: 200)

  lq.diagram(
    width: 12cm,
    height: 9cm,
    lq.scatter(x, y, size: 8pt, label: "Samples"),
    lq.plot(xs, xs.map(x => f(x)), mark: none, label: "Cubic Spline"),
    lq.plot(
      xs,
      xs.map(x => calc.sin(x)),
      label: "Reference",
      mark: none,
      stroke: (dash: "dashed"),
    ),
  )
}

7 Likes