💾 Archived View for jun.skein.city › journal › software › playing-with-bezier.gmi captured on 2024-12-17 at 09:50:02. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Playing With Bezier

I've been recently playing with bezier curves, and there is something truly beautiful in their simplicity. As I had never looked at them closely before, I had presumed the math involved was a little over my head. I am very pleased to be mistaken, and after just a little bit of reading I was able to put together an implementation in a handful of lines of OCaml:

type point = float * float
type line = point * point
type bezier = line list

exception Bad_input

let of_points = 
  let rec aux acc =
    function
    | x :: ((y :: _) as xs) -> aux ((x, y) :: acc) xs
    | _ -> List.rev acc
  in
  function
  | [] | [_] -> raise Bad_input
  | points -> aux [] points

let interpolate_linear ((ax, ay), (bx, by)) i =
  let xl = bx -. ax in
  let yl = by -. ay in
  (ax +. xl *. i, ay +. yl *. i)

let rec derive curve i =
  match curve with
  | [] | [_] -> []
  | a :: ((b :: _) as tail) ->
    (interpolate_linear a i, interpolate_linear b i) :: derive tail i

let rec point_at curve i =
  match curve with
  | [] -> raise Bad_input
  | [l] -> interpolate_linear l i
  | curve -> point_at (derive curve i) i

Recursive derivation of the curve until finally reaching the current point on the curve at that specific interpolation value. A shockingly simple algorithm that allows for the rendering of beautiful, scalable curves. That alongside a simple frame drawer (and PPM exporter) allows for some very nice results.

An example bezier curve, with flood fill on both sides.

There is something truly fantastical to me about how these work. Describing and drawing curves using nothing but straight lines is something profoundly beautiful to me. Because that's precisely what it is, and the fact they can be stored so efficiently as merely control points only exemplifies how amazing they are. I am excited to continue using them as I advance this presentation application I am working on.

Back to Index