File: graph_example.ml

package info (click to toggle)
ocaml-graphics 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: ansic: 2,394; ml: 1,078; makefile: 14
file content (254 lines) | stat: -rw-r--r-- 7,706 bytes parent folder | download | duplicates (3)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*              Damien Doligez, projet Para, INRIA Rocquencourt           *)
(*                                                                        *)
(*   Copyright 1999 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(* To run this example:
   ********************
   1. Select all the text in this window.
   2. Drag it to the toplevel window.
   3. Watch the colors.
   4. Drag the mouse over the graphics window and click here and there.
   5. Type any key to the graphics window to stop the program.
*)

open Graphics

;;
open_graph " 480x270"

let xr = (size_x () / 2) - 30

and yr = (size_y () / 2) - 26

and xg = (size_x () / 2) + 30

and yg = (size_y () / 2) - 26

and xb = size_x () / 2

and yb = (size_y () / 2) + 26

let point x y =
  let dr = ((x - xr) * (x - xr)) + ((y - yr) * (y - yr))
  and dg = ((x - xg) * (x - xg)) + ((y - yg) * (y - yg))
  and db = ((x - xb) * (x - xb)) + ((y - yb) * (y - yb)) in
  if dr > dg && dr > db then set_color (rgb 255 (255 * dg / dr) (255 * db / dr))
  else if dg > db then set_color (rgb (255 * dr / dg) 255 (255 * db / dg))
  else set_color (rgb (255 * dr / db) (255 * dg / db) 255);
  fill_rect x y 2 2

;;
for y = (size_y () - 1) / 2 downto 0 do
  for x = 0 to (size_x () - 1) / 2 do
    point (2 * x) (2 * y)
  done
done

let n = 0x000000

and w = 0xFFFFFF

and b = 0xFFCC99

and y = 0xFFFF00

and o = 0xCC9966

and v = 0x00BB00

and g = 0x888888

and c = 0xDDDDDD

and t = transp

let caml =
  make_image
    [|
      [|
        t; t; t; t; t; t; t; t; t; t; t; n; n; n; n; n; n; t; t; t; t; t; t; t;
        t; t; t; t; t; t; t; t;
      |];
      [|
        t; t; t; t; t; t; t; t; t; t; n; n; n; n; n; n; n; n; n; t; t; t; t; t;
        t; t; t; t; t; t; t; t;
      |];
      [|
        t; t; t; t; t; t; t; t; n; n; n; n; n; n; n; n; n; n; n; n; t; t; t; t;
        t; t; t; t; t; t; t; t;
      |];
      [|
        n; n; n; n; n; n; t; n; n; n; n; n; b; b; b; b; b; b; b; n; n; t; t; t;
        t; t; n; n; n; n; n; t;
      |];
      [|
        n; o; o; o; o; o; n; n; n; n; b; b; b; b; b; b; b; b; b; b; b; n; n; n;
        n; n; n; n; n; n; n; t;
      |];
      [|
        n; o; o; o; o; o; o; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n;
        n; n; n; n; n; n; n; t;
      |];
      [|
        n; o; o; o; o; o; o; o; n; n; n; g; g; g; n; n; n; n; n; n; n; n; n; n;
        n; n; n; n; n; n; t; t;
      |];
      [|
        n; n; o; o; o; o; o; o; o; n; n; n; c; c; n; n; n; n; n; n; n; n; n; n;
        n; n; n; n; n; n; t; t;
      |];
      [|
        t; n; n; o; o; o; o; o; o; o; n; n; n; c; n; n; n; n; n; n; n; b; b; n;
        n; n; n; n; n; t; t; t;
      |];
      [|
        t; t; n; n; n; o; o; o; o; o; o; n; n; n; n; n; n; n; n; n; b; b; b; b;
        n; n; n; n; t; t; t; t;
      |];
      [|
        t; t; t; t; n; n; o; o; o; o; o; o; n; n; n; n; n; n; n; n; b; b; b; b;
        b; b; n; n; t; t; t; t;
      |];
      [|
        t; t; t; t; t; n; n; o; o; o; o; o; o; n; n; n; n; n; n; o; o; b; b; b;
        b; b; b; n; n; t; t; t;
      |];
      [|
        t; t; t; t; t; n; n; o; o; o; o; o; o; b; b; b; b; b; n; n; o; o; b; b;
        b; b; b; b; n; n; t; t;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; o; b; b; b; b; b; b; b; n; n; o; o; b;
        b; b; b; b; b; n; n; t;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; b; b; b; b; b; b; b; b; b; n; n; o; o;
        b; b; b; b; b; b; n; n;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; b; b; b; b; b; n; n; b; b; b; n; n; o;
        o; b; b; b; b; b; n; n;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; b; b; b; b; b; n; n; b; b; b; b; n; n;
        o; o; b; o; b; b; n; n;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; b; b; b; b; b; n; n; b; b; b; b; b; n;
        n; o; o; o; o; o; n; n;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; b; b; b; b; b; n; n; b; b; b; b; b; b;
        n; n; o; o; o; o; n; n;
      |];
      [|
        t; t; t; t; n; n; n; o; o; o; o; o; b; b; b; b; n; n; b; b; b; b; b; b;
        b; n; n; o; o; n; n; n;
      |];
      [|
        t; t; t; t; n; n; n; n; o; o; o; o; o; b; b; b; n; n; n; b; b; b; b; b;
        b; b; n; n; o; n; b; n;
      |];
      [|
        t; t; t; t; t; n; n; n; o; o; o; o; o; o; b; b; n; n; n; b; b; b; b; b;
        b; b; b; n; n; n; b; n;
      |];
      [|
        t; t; t; t; t; t; n; n; o; o; o; o; o; o; o; y; v; y; n; b; b; b; b; b;
        b; b; b; n; n; b; b; n;
      |];
      [|
        t; t; t; t; t; t; t; n; o; o; o; o; o; v; y; o; o; n; n; n; b; b; b; b;
        b; b; b; n; n; b; b; n;
      |];
      [|
        t; t; t; t; t; t; t; n; o; o; o; y; v; o; o; o; o; n; n; n; n; b; b; b;
        b; b; b; n; n; b; b; n;
      |];
      [|
        t; t; t; t; t; t; n; n; o; v; y; o; y; o; o; o; o; o; o; n; n; n; b; b;
        b; b; b; n; n; b; b; n;
      |];
      [|
        t; t; t; t; t; t; n; o; y; y; o; o; v; o; o; o; o; o; o; o; n; n; n; b;
        b; b; n; n; n; b; n; t;
      |];
      [|
        t; t; t; t; t; n; n; v; o; v; o; o; o; o; o; o; o; o; o; o; o; n; n; n;
        b; n; n; n; n; b; n; t;
      |];
      [|
        t; t; t; t; t; n; v; o; o; v; o; o; o; o; o; o; o; o; o; o; o; o; n; n;
        n; n; n; n; n; n; t; t;
      |];
      [|
        t; t; t; t; n; n; o; o; o; o; o; o; o; o; o; o; o; o; o; o; o; n; n; n;
        n; n; n; t; t; t; t; t;
      |];
      [|
        t; t; t; t; n; o; o; o; o; o; o; o; o; o; o; o; o; o; o; o; n; n; t; t;
        t; t; t; t; t; t; t; t;
      |];
      [|
        t; t; t; t; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n; n; t; t; t;
        t; t; t; t; t; t; t; t;
      |];
    |]

(*
let x = ref 0 and y = ref 0;;
let bg = get_image !x !y 32 32;;
while true do
  let st = wait_next_event [Mouse_motion; Button_down] in
  if not st.button then draw_image bg !x !y;
  x := st.mouse_x;
  y := st.mouse_y;
  blit_image bg !x !y;
  draw_image caml !x !y;
done;;
*)

;;
set_color (rgb 0 0 0);
remember_mode false;
try
  while true do
    let st = wait_next_event [ Mouse_motion; Button_down; Key_pressed ] in
    synchronize ();
    if st.keypressed then raise Exit;
    if st.button then (
      remember_mode true;
      draw_image caml st.mouse_x st.mouse_y;
      remember_mode false);
    let x = st.mouse_x + 16 and y = st.mouse_y + 16 in
    moveto 0 y;
    lineto (x - 25) y;
    moveto 10000 y;
    lineto (x + 25) y;
    moveto x 0;
    lineto x (y - 25);
    moveto x 10000;
    lineto x (y + 25);
    draw_image caml st.mouse_x st.mouse_y
  done
with Exit -> ()

(* To run this example:
   ********************
   1. Select all the text in this window.
   2. Drag it to the toplevel window.
   3. Watch the colors.
   4. Drag the mouse over the graphics window and click here and there.
   5. Type any key to the graphics window to stop the program.
*)