forked from smeruelo/mooc-ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
w5_4.2_ascii_art.ml
51 lines (42 loc) · 1.11 KB
/
w5_4.2_ascii_art.ml
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
type image = int -> int -> bool;;
let all_white = fun x y -> false;;
let all_black = fun x y -> true;;
let checkers = fun x y -> y/2 mod 2 = x/2 mod 2;;
let square cx cy s = fun x y ->
let minx = cx - s / 2 in
let maxx = cx + s / 2 in
let miny = cy - s / 2 in
let maxy = cy + s / 2 in
x >= minx && x <= maxx && y >= miny && y <= maxy
;;
let disk cx cy r = fun x y ->
let x' = x - cx in
let y' = y - cy in
(x' * x' + y' * y') <= r * r
;;
type blend =
| Image of image
| And of blend * blend
| Or of blend * blend
| Rem of blend * blend
;;
let display_image width height f_image =
for row = 0 to height do
for col = 0 to width do
match f_image col row with
| false -> print_char ' '
| true -> print_char '#'
done;
print_newline ()
done
;;
let rec render blend x y =
match blend with
| Image b -> b x y
| And (b1, b2) -> ((render b1) x y) && ((render b2) x y)
| Or (b1, b2) -> ((render b1) x y) || ((render b2) x y)
| Rem (b1, b2) -> ((render b1) x y) && not ((render b2) x y)
;;
let display_blend width height blend =
display_image width height (render blend)
;;