Skip to content

Commit

Permalink
touch examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pqwy committed Nov 6, 2017
1 parent 90e7821 commit a3f1219
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
10 changes: 8 additions & 2 deletions benchmarks/speed.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,20 @@ let b_construct () =
Unmark.time ~tag:("make " ^ n) ~measure ~n:1000
(fun () -> I.string A.empty s));

[0x40; 0x262d] |> List.iter (fun x ->
List.iter (fun x ->
let u = Uchar.of_int x in
for i = 0 to 2 do
let n = 10. ** float i |> truncate in
let tag = Format.sprintf "repeat U+%04X x %d" x n in
Unmark.time ~tag ~measure ~n:1000
(fun () -> I.uchar A.empty u n 1)
done)
done) [0x40; 0x262d];

Unmark.time ~tag:"pxmatrix" ~measure:`Cputime ~n:100
(fun () -> pxmatrix 200 200 @@ fun _ _ -> A.black);

()


let () =
List.iter (fun f -> f ()) [
Expand Down
42 changes: 42 additions & 0 deletions examples/almondbread.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
open Notty
open Common

let iter = 200

let member x y =
let rec go cx cy x y n =
let xx = x *. x and yy = y *. y in
if n = 0 || xx +. yy > 4. then n else
go cx cy (xx -. yy +. cx) (2. *. x *. y +. cy) (n - 1) in
float (iter - go x y 0. 0. iter) /. float iter

let pi2 = 2. *. 3.14159
let pi2_3 = pi2 /. 3.

let mandelbrot x y =
(* let esc = 1. -. member x y in *)
(* 23. *. esc *. esc |> truncate |> A.gray *)
match member x y with
| 1. -> A.gray 0
| esc ->
let t = esc *. pi2 in
let f d = (sin (t +. d) *. 128. +. 128.) |> truncate in
A.rgb_888 ~b:(f (-.pi2_3)) ~g:(f 0.) ~r:(f pi2_3)

let xlate dx dy f x y = f (x -. dx) (y -. dy)
let scale k f = let k1 = 1./.k in fun x y -> f (x *. k1) (y *. k1)
let rot a f =
let sina = sin a and cosa = cos a in fun x y ->
f (x *. cosa -. y *. sina) (x *. sina +. cosa *. y)

let render_unit f (w, h) =
let sw = 1. /. float w
and sh = 1. /. float (2 * h) in
pxmatrix w h (fun x y -> f (float x *. sw) (float y *. sh))

let () =
let pix =
render_unit @@
rot (-1.570795) @@ xlate (1.6) (-0.5) @@
mandelbrot in
Notty_unix.(output_image_size @@ fun (w, h) -> pix (w, h - 1) |> eol)
6 changes: 6 additions & 0 deletions examples/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ module Images = struct
grid [ [a; hbar; b]; [vbar; i; vbar]; [d; hbar; c] ]
end

let halfblock = ""

let pxmatrix w h f = I.tabulate w h @@ fun x y ->
let y = y * 2 in
I.string A.(bg (f x y) ++ fg (f x (y + 1))) halfblock

module Term = Notty_unix.Term

let simpleterm ~imgf ~f ~s =
Expand Down
10 changes: 5 additions & 5 deletions examples/inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ let output_subst ~prev i =
rewind (h - 1); output_image i

let cmyk = function
| 0 -> A.rgb_888 ~r:0 ~g:255 ~b:255
| 1 -> A.rgb_888 ~r:255 ~g:0 ~b:255
| 2 -> A.rgb_888 ~r:255 ~g:255 ~b:0
| 3 -> A.rgb_888 ~r:0 ~g:0 ~b:0
| _ -> A.rgb_888 ~r:255 ~g:255 ~b:255
| 0 -> A.rgb ~r:0 ~g:5 ~b:5
| 1 -> A.rgb ~r:5 ~g:0 ~b:5
| 2 -> A.rgb ~r:5 ~g:5 ~b:0
| 3 -> A.rgb ~r:0 ~g:0 ~b:0
| _ -> A.rgb ~r:5 ~g:5 ~b:5

let () =

Expand Down
14 changes: 6 additions & 8 deletions examples/life.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ let erem x y = (x mod y + y) mod y
let square (w, h) (a, b as ab) =
if a < 0 || a >= w || b < 0 || b >= h then (-1, -1) else ab
let torus (w, h) (a, b) = (erem a w, erem b h)
let moebius (w, _) (a, b as ab) = if a < 0 || a >= w then (erem a w, -b) else ab
let moebius (w, h) (a, b as ab) =
if a < 0 || a >= w then (erem a w, h - b - 1) else ab

let neigh topo (a, b) = [
(a-1, b); (a+1, b); (a-1, b-1); (a-1, b+1)
Expand Down Expand Up @@ -91,20 +92,17 @@ let rec loop term (e, t) (dim, n, life as st) =
Term.image term (render dim n life) >>= fun () ->
loop term (e, timer ())
(dim, n + 1, step (torus dim) life)
| `Mouse ((`Press _|`Drag), (x, y), _) ->
| `Mouse ((`Press `Left|`Drag), (x, y), _) ->
loop term (event term, t)
(dim, n, CSet.add (torus dim (x-1, y-1)) life)
(dim, n, CSet.add (torus dim (x, y)) life)
| `Resize dim ->
let life = CSet.map (torus dim) life in
Term.image term (render dim n life) >>= fun () ->
loop term (event term, t) (dim, n, life)
| _ -> loop term (event term, t) st

let main () =
let tc = Unix.(tcgetattr stdin) in
Unix.(tcsetattr stdin TCSANOW { tc with c_isig = false });
let t = Term.create () in
let size = Term.size t in
loop t (event t, timer ()) (size, 0, glider)
let t = Term.create () in
loop t (event t, timer ()) (Term.size t, 0, glider)

let () = Lwt_main.run @@ main ()
1 change: 1 addition & 0 deletions pkg/pkg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let () =
bin ~cond:ex0 "examples/emoji";
bin ~cond:ex0 "examples/inline";
bin ~cond:ex0 "examples/rain";
bin ~cond:ex0 "examples/almondbread";
bin ~cond:ex1 "examples/sierpinski_lwt";
bin ~cond:ex1 "examples/life";
bin ~cond:ex1 "examples/linear";
Expand Down

0 comments on commit a3f1219

Please sign in to comment.