File: cubes.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (215 lines) | stat: -rw-r--r-- 5,744 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(* Js_of_ocaml example
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2010 Jérôme Vouillon
 * Laboratoire PPS - CNRS Université Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)
open Js_of_ocaml
open Js_of_ocaml_lwt

let n = 12

let h = 20.

let w = floor ((h *. sqrt 3. /. 2.) +. 0.5)

(****)

let _ = Random.self_init ()

let create_cubes v = Array.init n (fun _ -> Array.init n (fun _ -> Array.make n v))

let get a i j k = i < 0 || j < 0 || k < 0 || (i < n && j < n && k < n && a.(i).(j).(k))

let update a =
  let i = Random.int n in
  let j = Random.int n in
  let k = Random.int n in
  if a.(i).(j).(k)
  then
    if not (get a (i + 1) j k || get a i (j + 1) k || get a i j (k + 1))
    then (
      a.(i).(j).(k) <- false;
      true)
    else false
  else if get a (i - 1) j k && get a i (j - 1) k && get a i j (k - 1)
  then (
    a.(i).(j).(k) <- true;
    true)
  else false

(****)

module Html = Dom_html

let top = Js.string "#a8a8f6"

let left = Js.string "#d9d9d9"

let right = Js.string "#767676"

let on_cube c i j k f =
  let x = float (i - k + n - 1) *. w in
  let y = (float (n - 1 - j) *. h) +. (float (i + k) *. h /. 2.) in
  c##save;
  c##translate (Js.float x) (Js.float y);
  f c;
  c##restore

let draw_top c =
  c##.fillStyle := top;
  c##beginPath;
  c##moveTo (Js.float w) (Js.float 0.);
  c##lineTo (Js.float (2. *. w)) (Js.float (h /. 2.));
  c##lineTo (Js.float w) (Js.float h);
  c##lineTo (Js.float 0.) (Js.float (h /. 2.));
  c##fill

let top_edges c =
  c##beginPath;
  c##moveTo (Js.float 0.) (Js.float (h /. 2.));
  c##lineTo (Js.float w) (Js.float 0.);
  c##lineTo (Js.float (2. *. w)) (Js.float (h /. 2.));
  c##stroke

let draw_right c =
  c##.fillStyle := right;
  c##beginPath;
  c##moveTo (Js.float w) (Js.float h);
  c##lineTo (Js.float w) (Js.float (2. *. h));
  c##lineTo (Js.float (2. *. w)) (Js.float (1.5 *. h));
  c##lineTo (Js.float (2. *. w)) (Js.float (h /. 2.));
  c##fill

let right_edges c =
  c##beginPath;
  c##moveTo (Js.float w) (Js.float (2. *. h));
  c##lineTo (Js.float w) (Js.float h);
  c##lineTo (Js.float (2. *. w)) (Js.float (h /. 2.));
  c##stroke

let draw_left c =
  c##.fillStyle := left;
  c##beginPath;
  c##moveTo (Js.float w) (Js.float h);
  c##lineTo (Js.float w) (Js.float (2. *. h));
  c##lineTo (Js.float 0.) (Js.float (1.5 *. h));
  c##lineTo (Js.float 0.) (Js.float (h /. 2.));
  c##fill

let left_edges c =
  c##beginPath;
  c##moveTo (Js.float w) (Js.float h);
  c##lineTo (Js.float 0.) (Js.float (h /. 2.));
  c##lineTo (Js.float 0.) (Js.float (1.5 *. h));
  c##stroke

let remaining_edges c =
  c##beginPath;
  c##moveTo (Js.float 0.) (Js.float (float n *. 1.5 *. h));
  c##lineTo (Js.float (float n *. w)) (Js.float (float n *. 2. *. h));
  c##lineTo (Js.float (float n *. 2. *. w)) (Js.float (float n *. 1.5 *. h));
  c##lineTo (Js.float (float n *. 2. *. w)) (Js.float (float n *. 0.5 *. h));
  c##stroke

let tile c a (top, right, left) =
  for i = 0 to n - 1 do
    let j = ref (n - 1) in
    for k = 0 to n - 1 do
      while !j >= 0 && not a.(i).(!j).(k) do
        decr j
      done;
      on_cube c i !j k top
    done
  done;
  for j = 0 to n - 1 do
    let i = ref (n - 1) in
    for k = 0 to n - 1 do
      while !i >= 0 && not a.(!i).(j).(k) do
        decr i
      done;
      on_cube c !i j k right
    done
  done;
  for i = 0 to n - 1 do
    let k = ref (n - 1) in
    for j = 0 to n - 1 do
      while !k >= 0 && not a.(i).(j).(!k) do
        decr k
      done;
      on_cube c i j !k left
    done
  done

let create_canvas () =
  let d = Html.window##.document in
  let c = Html.createCanvas d in
  c##.width := (n * 2 * truncate w) + 1;
  c##.height := (n * 2 * truncate h) + 1;
  c

let redraw ctx canvas a =
  let c = canvas##getContext Html._2d_ in
  c##setTransform
    (Js.float 1.)
    (Js.float 0.)
    (Js.float 0.)
    (Js.float 1.)
    (Js.float 0.)
    (Js.float 0.);
  c##clearRect
    (Js.float 0.)
    (Js.float 0.)
    (Js.float (float canvas##.width))
    (Js.float (float canvas##.height));
  c##setTransform
    (Js.float 1.)
    (Js.float 0.)
    (Js.float 0.)
    (Js.float 1.)
    (Js.float 0.5)
    (Js.float 0.5);
  c##.globalCompositeOperation := Js.string "lighter";
  tile c a (draw_top, draw_right, draw_left);
  c##.globalCompositeOperation := Js.string "source-over";
  tile c a (top_edges, right_edges, left_edges);
  remaining_edges c;
  ctx##drawImage_fromCanvas canvas (Js.float 0.) (Js.float 0.)

let ( >>= ) = Lwt.bind

let rec loop c c' a =
  Lwt_js.sleep 0.2
  >>= fun () ->
  let need_redraw = ref false in
  for _i = 0 to 99 do
    need_redraw := update a || !need_redraw
  done;
  if !need_redraw then redraw c c' a;
  loop c c' a

let start _ =
  let c = create_canvas () in
  let c' = create_canvas () in
  Dom.appendChild Html.window##.document##.body c;
  let c = c##getContext Html._2d_ in
  c##.globalCompositeOperation := Js.string "copy";
  let a = create_cubes true in
  redraw c c' a;
  ignore (loop c c' a);
  Js._false

let _ = Html.window##.onload := Html.handler start