source: Deliverables/Dissemination/final-review/wp5/Pics/call.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: 4.2 KB
Line 
1
2open Mlpost
3
4
5(* Helpers *)
6
7let tex = Box.tex ~dx:(Num.cm 0.1) ~dy:(Num.cm 0.05)
8
9let texttt s = "\\texttt{" ^ s ^ "}"
10
11let string_of_color c = match c with
12  | _ when c = Color.orange -> "orange"
13  | _ when c = Color.blue -> "blue"
14  | _ when c = Color.white -> "white"
15  | _ -> "black"
16
17let textcolor color s =
18  "\\textcolor{" ^ (string_of_color color) ^ "}{" ^ s ^ "}"
19
20let code s = tex (texttt s)
21
22let middle p1 p2 =
23  let p = Point.add p1 p2 in
24  Point.mult (Num.of_float 0.5) p
25
26let path points = Path.pathp ~style:Path.jLine points
27
28let arrow points =
29  Box.pic (Arrow.simple (path points))
30
31let point_x_y px py = Point.pt (Point.xpart px, Point.ypart py)
32
33
34(* Boxes *)
35
36let cell1 = code "x++;"
37let cell2 = code "f(\\&x);"
38let cell3 = code "y = x;"
39let cell4 = code "void f(int* x) \\{"
40let cell5 = code "\\quad ...\\textcolor{white}{A}"
41let cell6 = code "\\quad return; \\}"
42
43let place_below c1 c2 = Box.place `Southwest ~pos:`Northwest c1 c2
44
45let cell2 = place_below cell1 cell2
46let cell3 = place_below cell2 cell3
47
48let cell4 = Box.place `East ~pos:`West ~padding:(Num.cm 1.5) cell1 cell4
49
50let place c1 c2 =
51  let target = point_x_y (Box.west cell4) (Box.east c1) in
52  let target = Box.center target (Box.empty ()) in
53  Box.place `Center ~pos:`West target c2
54
55let cell5 = place cell2 cell5
56let cell6 = place cell3 cell6
57
58
59(* Arrows *)
60
61let arrow1 =
62  let p1 = Box.east cell2 in
63  let p4 = Box.west cell4 in
64  let x = Point.xpart (middle p1 p4) in
65  let p2 = Point.pt (x, Point.ypart p1) in
66  let p3 = Point.pt (x, Point.ypart p4) in
67  arrow [p1 ; p2 ; p3 ; p4]
68
69let arrow2 =
70  let p1 = Box.west cell6 in
71  let p2 = Box.east cell3 in
72  arrow [p1 ; p2]
73
74let delta_x = 0.1
75let delta_y = 0.025
76let shift_x x = Point.shift (Point.pt (Num.cm x, Num.zero))
77let shift_y y = Point.shift (Point.pt (Num.zero, Num.cm y))
78let shift_right = shift_x delta_x
79let shift_left = shift_x (-.delta_x)
80let shift_up = shift_y delta_y
81let shift_down = shift_y (-.delta_y)
82
83let scope x pos ?tex color p1 p2 =
84  let p1 = shift_down p1 in
85  let p0 = shift_x x p1 in
86  let p2 = shift_up p2 in
87  let p3 = shift_x x p2 in
88  let path = Box.pic (Path.draw ~color (path [p0 ; p1 ; p2 ; p3])) in
89  match tex with
90    | None -> path
91    | Some tex ->
92      let tex = Box.tex (textcolor color tex) in
93      let target = Box.center p1 (Box.empty ()) in
94      let tex = Box.place `Center ~pos target tex in
95      Box.group [tex ; path]
96
97let scope_left = scope delta_x `Northeast
98let scope_right = scope (-.delta_x) `Northwest
99
100let pscope1 = Box.north_west cell1
101
102let pscope2 =
103  let p = Box.south_west cell2 in
104  point_x_y pscope1 p
105
106let pscope3 =
107  let p = Box.south_west cell3 in
108  point_x_y pscope1 p
109
110let pscope4 = Box.north_east cell4 
111
112let pscope5 =
113  let p = middle (Box.north_east cell5) (Box.south_east cell5) in
114  point_x_y pscope4 p
115
116let pscope6 =
117  let p = Box.south_east cell6 in
118  point_x_y pscope4 p
119
120let scope1 color =
121  scope_left ~tex:"scope$_1$" color pscope1 pscope2
122let scope2 color =
123  scope_right color pscope4 pscope5
124let scope3 color =
125  scope_right ~tex:"scope$_2$" color pscope5 pscope6
126let scope4 color =
127  scope_left color pscope2 pscope3
128
129let scope5 = scope1 Color.orange
130let scope6 = scope2 Color.orange
131let scope7 = scope3 Color.blue
132let scope8 = scope4 Color.blue
133
134let scope1 = scope1 Color.white
135let scope2 = scope2 Color.white
136let scope3 = scope3 Color.white
137let scope4 = scope4 Color.white
138
139let scope9 = scope_left ~tex:"scope$_1$" Color.orange pscope1 pscope3
140let scope10 = scope_right ~tex:"scope$_2$" Color.blue pscope4 pscope6
141
142
143(* Call *)
144
145let call scope =
146  let (arrows, scopes) = match scope with
147    | 1 -> ([arrow1 ; arrow2], [scope1 ; scope2 ; scope3 ; scope4])
148    | 2 -> ([arrow1 ; arrow2], [scope5 ; scope6 ; scope7 ; scope8])
149    | _ -> ([], [scope9 ; scope10]) in
150  Box.group ([cell1 ; cell2 ; cell3 ; cell4 ; cell5 ; cell6] @
151             arrows @ scopes)
152
153
154(* Main *)
155
156let result1 = call 1
157
158let result2 = call 2
159
160let result3 = call 3
161
162
163(* Output pictures *)
164
165let results = [result1 ; result2 ; result3]
166let results = List.map Box.draw results
167
168let iteri f =
169  let rec aux i = function
170    | [] -> []
171    | e :: l -> f i e ; aux (i+1) l
172  in
173  aux 1
174
175let _ =
176  iteri (fun i b -> Metapost.emit ("call" ^ (string_of_int i)) b) results
Note: See TracBrowser for help on using the repository browser.