Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

rabbit.ml 4.56 KB
Newer Older
1
2
3
4
5

(**************************************************************************)
(* Utilities *)

let draw x y =
6
7
8
(*   if Sys.argv.(3) = "dot" then *)
(*     Graphics.plot (truncate x) (truncate y) *)
(*   else *)
9
10
    Graphics.lineto (truncate x) (truncate y)

11
open Data
12
let get_float var sol =  
13
14
15
  match (try List.assoc var sol with Not_found -> assert false) 
  with (F f) -> f | _ -> 
    Printf.printf "*** error when parsing float\n";
16
    flush stdout;
17
    assert false
18
 
19
20
21
22
23
24
let get_bool var sol =  
  match (try List.assoc var sol with Not_found -> assert false) 
  with (B b) -> b | _ -> 
    Printf.printf "*** error when parsing bool\n";
    flush stdout;
    assert false
25
26


27
28
29
30
31
32
33
(**************************************************************************)
let _ = 
  Graphics.open_graph " ";  Graphics.clear_graph ();  
  Graphics.set_window_title "The Crazy Rabbit travel - test";  
  Graphics.moveto 100 100
(*   ignore (Graphics.read_key ()) *)
;;
34

35
(**************************************************************************)
36

37
let init  _ = ()
38
let kill  _ = ()
39
let reset  _ = ()
40
41
let ss _i = ()
let rs _i = ()
42
type var_type = string
43

44
45
46
47
48
49
50
51
52
53
54
55
let (inputs :(string * Data.t) list) = [
  "x", Data.Real;  
  "y", Data.Real;  
  "p1x", Data.Real;  
  "p1y", Data.Real;  
  "p2x", Data.Real;  
  "p2y", Data.Real;  
  "p3x", Data.Real;  
  "p3y", Data.Real;  
  "p4x", Data.Real;  
  "p4y", Data.Real;
  "freeze", Data.Bool
56
]
57
58
59
60
61
62
63

let mems_i = []

let mems_o = [
  "x_min", Data.F 0.0;  "x_max", Data.F 100.0;  
  "y_min", Data.F 0.0;  "y_max", Data.F 100.0
]
64
  
65
let (outputs :(string * Data.t) list) = 
66
  [
67
68
    "x_min", Data.Real;  "x_max", Data.Real;  
    "y_min", Data.Real;  "y_max", Data.Real 
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
let cross_product(ux,uy,vx,vy)  = (ux*.vy-.uy*.vx);;


let image = ref (Graphics.get_image 0 0 (Graphics.size_x ()) (Graphics.size_y ()))


let cross_product(ux,uy,vx,vy) = (ux*.vy-.uy*.vx)
let is_inside(px,py,p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y) = 
  let p1p_x = px-.p1x in
  let p1p_y = py-.p1y in
  let p2p_x = px-.p2x in
  let p2p_y = py-.p2y in
  let p3p_x = px-.p3x in
  let p3p_y = py-.p3y in
  let p4p_x = px-.p4x in
  let p4p_y = py-.p4y in
  let p2p1_x = p1x-.p2x in
  let p2p1_y = p1y-.p2y in
  let p1p4_x = p4x-.p1x in
  let p1p4_y = p4y-.p1y in
  let p4p3_x = p3x-.p4x in
  let p4p3_y = p3y-.p4y in
  let p3p2_x = p2x-.p3x in
  let p3p2_y = p2y-.p3y in
    cross_product(p2p1_x, p2p1_y, p2p_x, p2p_y) < 0.0 &&
    cross_product(p1p4_x, p1p4_y, p1p_x, p1p_y) < 0.0 &&
    cross_product(p4p3_x, p4p3_y, p4p_x, p4p_y) < 0.0 &&
    cross_product(p3p2_x, p3p2_y, p3p_x, p3p_y) < 0.0   
        
100
let (step :  Data.subst list -> Data.subst list)=
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  fun rabbit_outs -> 
    let x_min = (F (float_of_int 0))
    and x_max = (F (float_of_int (Graphics.size_x ())))
    and y_min = (F (float_of_int 0))
    and y_max = (F (float_of_int (Graphics.size_y ()))) in
    let bounds = [("x_min", x_min);("x_max",x_max);("y_min",y_min);("y_max",y_max)] in 


    (* Drawing the new point onto the Graphics window *)
    let x = get_float "x" rabbit_outs
    and y = get_float "y" rabbit_outs
    and p1x = get_float "p1x" rabbit_outs
    and p2x = get_float "p2x" rabbit_outs
    and p3x = get_float "p3x" rabbit_outs
    and p4x = get_float "p4x" rabbit_outs
    and p1y = get_float "p1y" rabbit_outs
    and p2y = get_float "p2y" rabbit_outs
    and p3y = get_float "p3y" rabbit_outs
    and p4y = get_float "p4y" rabbit_outs
120
    and freeze = get_bool "freeze" rabbit_outs
121
    in
122
123
124
125
      if 
        (is_inside(x,y,p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y))
      then
        (
126
          (* ignore (Graphics.read_key ()) *)
127
128
        );

129
130
131
132
133
134
135
136
    let p1xI = truncate p1x
    and p1yI = truncate p1y
    and p2xI = truncate p2x
    and p2yI = truncate p2y
    and p3xI = truncate p3x
    and p3yI = truncate p3y
    and p4xI = truncate p4x
    and p4yI = truncate p4y
137
    in
138
139
140
    let point_list = [(p1xI,p1yI);(p2xI,p2yI);(p3xI,p3yI);(p4xI,p4yI)] in 
    let poly = Array.of_list point_list in

141
142
143
          

      Graphics.draw_image !image 0 0;
144
	   draw x y; 
145
146
147
148
149
150
151
152
153
      image := Graphics.get_image 0 0 (Graphics.size_x ()) (Graphics.size_y ());
      if freeze then (
        Graphics.set_color (Graphics.rgb 255 0 100);
	     Graphics.fill_poly poly
      )
      else (
        Graphics.set_color (Graphics.rgb 0 0 0);
	     Graphics.draw_poly poly
      );      
154
      bounds
155

156
157
158
open RdbgPlugin

let plugin = {
159
  id="ze rabbit plugin";
160
161
  inputs = inputs;
  outputs= outputs;
162
  reset= reset;
163
164
  save_state = ss;
  restore_state = rs;
165
166
167
168
169
170
171
172
173
  kill= kill;
  init_inputs=mems_i;
  init_outputs=mems_o;
  step= step;     
  step_dbg=(fun _ _ -> assert false); 
}

let _ =   
  OcamlRM.reg_plugin "rabbit.cmxs" plugin;
174

175