package tiny_libs

  1. Overview
  2. Docs
From-scratch libraries for teaching: graphics, audio, compression, crypto, networking and more

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.6.tar.gz
md5=7c636383d146d30ac6f2fa234a6253c8
sha512=c79f3823c5f8f57e5038eb640d487c61168b84aa07c61999d6622ef9fd0c890e2b03b4c6a7cdbbe9352a49e25dda00ac7bb14693cee8e3d7beeed251351a2af0

doc/src/tiny_libs.graphics_animation/Timing.ml.html

Source file Timing.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(* Claude Code
 *
 * Copyright (C) 2026 Yoann Padioleau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * (LGPL) as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *)

(* See Timing.mli *)

type t = { x1 : float; y1 : float; x2 : float; y2 : float }

let clamp01 v = Float.max 0. (Float.min 1. v)
let bezier x1 y1 x2 y2 = { x1 = clamp01 x1; y1; x2 = clamp01 x2; y2 }
let linear = bezier 0. 0. 1. 1.
let ease_in = bezier 0.42 0. 1. 1.
let ease_out = bezier 0. 0. 0.58 1.
let ease_in_out = bezier 0.42 0. 0.58 1.
let default = bezier 0.25 0.1 0.25 1.

(* one coordinate of the curve at s, its ends 0 and 1: its polynomial's
 * coefficients, Horner's way *)
let coord (p1 : float) (p2 : float) (s : float) : float =
  let c = 3. *. p1 in
  let b = (3. *. (p2 -. p1)) -. c in
  let a = 1. -. c -. b in
  ((((a *. s) +. b) *. s) +. c) *. s

let slope (p1 : float) (p2 : float) (s : float) : float =
  let c = 3. *. p1 in
  let b = (3. *. (p2 -. p1)) -. c in
  let a = 1. -. c -. b in
  (((3. *. a *. s) +. (2. *. b)) *. s) +. c

(* the s where x(s) = t *)
let solve (c : t) (t : float) : float =
  let rec newton s k =
    if k = 0 then None
    else
      let e = coord c.x1 c.x2 s -. t in
      if Float.abs e < 1e-7 then Some s
      else
        let d = slope c.x1 c.x2 s in
        if Float.abs d < 1e-6 then None else newton (s -. (e /. d)) (k - 1)
  in
  match newton t 8 with
  | Some s when s >= 0. && s <= 1. -> s
  | _ ->
      let rec bisect lo hi k =
        let s = (lo +. hi) /. 2. in
        if k = 0 then s else if coord c.x1 c.x2 s < t then bisect s hi (k - 1) else bisect lo s (k - 1)
      in
      bisect 0. 1. 40

let at (c : t) (t : float) : float =
  let t = clamp01 t in
  if t = 0. || t = 1. then t else coord c.y1 c.y2 (solve c t)