To create a value of type state, we need some auxiliary functions.

We define the function +: to add a pair of floats.

let (+:) (x1, y1) (x2, y2) = (x1 +. x2, y1 +. y2)

We define a function color_of_int which converts an integer into a value of type Graphics.color.

let color_of_int = function
  | 0 -> Graphics.black
  | 1 -> Graphics.magenta
  | 2 -> Graphics.green
  | 3 -> Graphics.red
  | 4 -> Graphics.blue
  | _ -> Graphics.black

We can now define a function which creates a new state.

let new_state () =
  signal pos default (0., 0.) gather (+:) in
  signal vel default (0., 0.) gather (+:) in
  emit pos (0., float (Graphics.size_y () / 2));
  { color = (color_of_int (Random.int 5));
    pos = pos;
    vel = vel; }

The flow of positions is initialized on the left side of the graphical window by an emission on the pos signal.