Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.19 KB | None | 0 0
  1. type color = W | B | R | G;;
  2.  
  3. type clock = color array;;
  4.  
  5. let color_at clock i =
  6.     if i >= 1 && i <= 5
  7.     then clock.(i-1)
  8.     else raise (Invalid_argument "color_at")
  9. ;;
  10.  
  11. let all_white = Array.make 5 W;;
  12.  
  13. type time = {
  14.     hh: int;
  15.     mm:int
  16. };;
  17.  
  18. let valid_time t =
  19.     t.hh >= 0 && t.hh < 24 && t.mm >= 0 && t.mm<60
  20. ;;
  21.  
  22. let normal_time t =
  23.     t.hh >= 0 && t.hh < 12 && t.mm >= 0 && t.mm<60
  24. ;;
  25.  
  26. let rounded_time t =
  27.     {t with mm = 5 * (t.mm / 5)}
  28. ;;
  29.  
  30. let valid_to_normal t =
  31.     if valid_time t
  32.     then {t with hh = t.hh mod 12 }
  33.     else invalid_arg "valid_to_normal"
  34. ;;
  35.  
  36. let rec fib = function
  37.     | 0 -> 0
  38.     | 1 -> 1
  39.     | n -> fib (n-1) + fib (n-2)
  40. ;;
  41.  
  42. let time (h,m) =
  43.     let
  44.         t = {hh=h;mm=m}
  45.     in
  46.         if valid_time t
  47.         then t
  48.         else invalid_arg "time"
  49. ;;
  50.  
  51. let time_of_clock c =
  52.     let h = ref 0 (* horas *)
  53.     and m = ref 0 in (* minutos *)
  54.     for i = 1 to 5 do
  55.         let v =
  56.             fib i
  57.         in match color_at c i with
  58.                 | W -> ()
  59.                 | B -> h := !h + v; m := !m + v
  60.                 | R -> h := !h + v
  61.                 | G -> m := !m + v
  62.         done;
  63.     { hh = !h; mm = 5 * !m }
  64. ;;
  65.  
  66.  
  67. let rec combinations elms s =
  68.     if s <= 0
  69.     then [[||]]
  70.     else List.concat (List.map (fun x -> List.map (fun y -> Array.append [|y|] x) elms ) (combinations elms (s - 1)))
  71. ;;
  72.  
  73. let colors = [R;G;B;W];;
  74.  
  75. let possibleClocks = combinations colors 5;;
  76.  
  77. let all_clocks_for_time tt =
  78.     List.filter (fun c -> time_of_clock c = rounded_time (valid_to_normal tt) ) possibleClocks
  79. ;;
  80.  
  81. let a_clock_for_time tt =
  82.     List.find (fun c -> time_of_clock c = rounded_time (valid_to_normal tt) ) possibleClocks
  83. ;;
  84.  
  85. let random_clock_for_time t =
  86.     let all =
  87.         all_clocks_for_time t
  88.     in
  89.         let n =
  90.             List.length all
  91.         in
  92.             List.nth all (Random.int n)
  93. ;;
  94.  
  95. open Graphics;;
  96. (*open Unix;;*)
  97.  
  98.  
  99. let color = function
  100.     | W -> rgb 245 247 250
  101.     | B -> rgb 0 122 255
  102.     | R -> rgb 255 59 48
  103.     | G -> rgb 76 217 100
  104. ;;
  105.  
  106. type sq = {
  107.     x: int;
  108.     y: int;
  109.     s: int
  110. };;
  111.  
  112. let sq = Array.make 5 { x = 0; y = 0; s = 0 };;
  113.  
  114. sq.(0) <- { x = 2; y = 3; s = 1 };  
  115. sq.(1) <- { x = 2; y = 4; s = 1 };  
  116. sq.(2) <- { x = 0; y = 3; s = 2 };  
  117. sq.(3) <- { x = 0; y = 0; s = 3 };  
  118. sq.(4) <- { x = 3; y = 0; s = 5 };;
  119.  
  120. let drawSquareS s i c =
  121.     set_color c;
  122.     let sq =
  123.         sq.(i)
  124.     in
  125.         fill_rect (sq.x * s) (sq.y * s) (sq.s * s) (sq.s * s)
  126. ;;
  127.  
  128. let drawlineS s () =
  129.     let k =
  130.         s
  131.     in
  132.         set_color (rgb 60 59 61);
  133.         set_line_width 2;
  134.         for i = 0 to 4 do
  135.             let sq = sq.(i) in
  136.                 draw_rect (sq.x * k) (sq.y * k) (sq.s * k) (sq.s * k)
  137.         done
  138. ;;
  139.  
  140. let draw_clockS s (c:clock) =
  141.     for i = 0 to 4 do
  142.         drawSquareS s i (color c.(i))
  143.     done;
  144.     drawlineS s ()
  145. ;;
  146.  
  147. let print_current_time s =
  148.     let current_tm =
  149.             Unix.localtime (Unix.time ())
  150.         in
  151.             let tm =
  152.                 time (current_tm.tm_hour mod 12, current_tm.tm_min)
  153.             in draw_clockS s (a_clock_for_time tm )
  154. ;;
  155.  
  156. let open_clock size =
  157.     let st =
  158.         " " ^ string_of_int (8* size) ^ "x" ^ string_of_int (5 * size)
  159.     in
  160.         open_graph st;
  161.         set_window_title "Fibonacci Clock";
  162.         drawlineS size ();
  163.         draw_clockS size
  164. ;;
  165.  
  166. let close_clock () = close_graph ();;
  167.  
  168. let run_clock size =
  169.     try
  170.         open_clock size
  171.     with
  172.         | Graphic_failure ("fatal I/O error") ->
  173.             print_string "Hello!";
  174.     print_current_time size
  175. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement