source: Deliverables/Dissemination/final-review/wp5/Pics/goto.ml @ 3282

Last change on this file since 3282 was 3282, checked in by regisgia, 7 years ago
  • WP5 slides for the final review.
File size: 2.0 KB
Line 
1
2open Mlpost
3
4
5(* Helpers *)
6
7let dx = Num.cm 0.2
8let dy = Num.cm 0.1
9
10let tex = Box.tex ~dx ~dy
11
12let texttt s = "\\texttt{" ^ s ^ "}"
13
14let code s = tex ~stroke:(Some Color.black) (texttt s)
15
16let pathp = Path.pathp ~style:Path.jLine
17
18
19(* Boxes *)
20
21let lbl = code "lbl:"
22let cost = code "\\textcolor{magenta}{$\\lbl$:}"
23let etc = tex (texttt "...")
24let goto = code "goto lbl;"
25
26let boxes with_cost =
27  let cost = if with_cost then [cost] else [] in
28  Box.hbox ~padding:(Num.cm 1.) ([lbl] @ cost @ [etc ; goto])
29
30
31(* Arrows *)
32
33let edge = 0.5
34
35let entry c =
36  let p2 = Box.west c in
37  let p1 = Point.shift (Point.pt (Num.cm (-.edge), Num.zero)) p2 in
38  [Arrow.simple ~dashed:Dash.evenly (pathp [p1 ; p2])]
39
40let arrow c1 c2 =
41  let p1 = Box.east c1 in
42  let p2 = Box.west c2 in
43  Arrow.simple (pathp [p1 ; p2])
44
45let loop c1 c2 =
46  let p1 = Box.south c1 in
47  let shift = Point.pt (Num.zero, Num.cm (-0.3)) in
48  let p2 = Point.shift shift p1 in
49  let p4 = Box.south c2 in
50  let p3 = Point.pt (Point.xpart p4, Point.ypart p2) in
51  let path = pathp [p1 ; p2 ; p3 ; p4] in
52  [Arrow.simple path]
53
54let right_arrows cells =
55  let get i = Box.nth i cells in
56  let n = Array.length (Box.elts cells) in
57  let rec aux i =
58    if i >= n-1 then []
59    else (arrow (get i) (get (i+1))) :: (aux (i+1)) in
60  aux 0
61
62let arrows cells =
63  let get i = Box.nth i cells in
64  let last = (Array.length (Box.elts cells)) - 1 in
65  let arrows =
66    (entry (get 0)) @ (loop (get last) (get 0)) @ (right_arrows cells) in
67  Box.group (List.map Box.pic arrows)
68
69
70(* Goto *)
71
72let goto with_cost =
73  let cells = boxes with_cost in
74  let arrows = arrows cells in
75  Box.group [cells ; arrows]
76
77
78(* Main *)
79
80let result1 = goto false
81let result2 = goto true
82
83
84(* Output pictures *)
85
86let results = [result1 ; result2]
87let results = List.map Box.draw results
88
89let iteri f =
90  let rec aux i = function
91    | [] -> []
92    | e :: l -> f i e ; aux (i+1) l
93  in
94  aux 1
95
96let _ =
97  iteri (fun i b -> Metapost.emit ("goto" ^ (string_of_int i)) b) results
Note: See TracBrowser for help on using the repository browser.