1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* only by permission. *)
11
(***********************************************************************)
14
(* Simple turtle graphics *)
19
mutable heading : float };;
21
let t = { x = 0.0; y = 0.0; heading = 0.0 };;
23
let pi180 = 4.0 *. atan 1.0 /. 180.0;;
26
if x >= 0.0 then int_of_float(x +. 0.5) else -(int_of_float(0.5 -. x));;
29
t.x <- float_of_int(size_x() / 2);
30
t.y <- float_of_int(size_y() / 2);
32
moveto (round t.x) (round t.y)
36
t.x <- t.x +. cos(t.heading) *. d;
37
t.y <- t.y +. sin(t.heading) *. d;
38
lineto (round t.x) (round t.y)
42
t.heading <- t.heading +. a *. pi180
45
(* A table of flashy colors *)
48
[| 0xff0000; 0xff6000; 0xffc000; 0xdeff00;
49
0x7eff00; 0x1eff00; 0x1eff00; 0x00ff42;
50
0x00ffa2; 0x00fcff; 0x009cff; 0x003cff;
51
0x2400ff; 0x8400ff; 0xe400ff; 0xff00ba |];;
53
(* The main drawing function *)
55
let rec spir dist angle angle_incr color =
56
if key_pressed() then () else begin
57
set_color colors.(color);
60
spir dist (angle +. angle_incr) angle_incr ((color + 1) land 15)
64
(* The interaction loop *)
67
let (x, y) = current_point() in
69
let (_, height) = text_size s in
74
Printf.sprintf "%6.2f" f
77
let rec loop dist angle_incr =
81
message " - d, D to decrease";
82
message " + i, I to increase";
83
message (format dist ^ " " ^ format angle_incr);
84
message "Distance Angle increment 'q' to quit";
86
spir dist 0.0 angle_incr 0;
88
| '-' -> loop (dist -. 2.0) angle_incr
89
| '+' -> loop (dist +. 2.0) angle_incr
90
| 'd' -> loop dist (angle_incr -. 0.05)
91
| 'D' -> loop dist (angle_incr -. 5.0)
92
| 'i' -> loop dist (angle_incr +. 0.05)
93
| 'I' -> loop dist (angle_incr +. 5.0)
95
| _ -> loop dist angle_incr
104
if !Sys.interactive then () else begin spir(); exit 0 end;;