-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractals.ml
423 lines (367 loc) · 12.3 KB
/
fractals.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(*****************************************************************************)
(*#load "graphics.cma" ;; (* à ajouter si 4.02.3 *)*)
#use "topfind";;
#require "graphics";;
open Graphics ;;
open Random;; (* loads the Random module *)
self_init();; (* initializes the random generator *)
(*
** The function int: int -> int can now be called.
** int bound returns a random integer between
** 0 (inclusive) and bound (exclusive).
*)
(*****************************************************************************)
(* *)
(* The funcions in the next section are needed by most of the fractals *)
(* *)
(*****************************************************************************)
(*
** draw_line (x, y) (z, t)
** Draw a line from point (x,y)
** to point (z,t) with type :
** val draw_line : int * int -> int * int -> unit = <fun>
*)
let draw_line (x, y) (z, t) =
moveto x y ;
lineto z t ;;
(*
** draw_triangle p1 p2 p3
** Draw a triangle by drawing lines between
** all three points p1 p2 p3. Type :
** val draw_triangle : int * int -> int * int -> int * int -> unit = <fun>
*)
let draw_triangle p1 p2 p3 =
draw_line p1 p2;
draw_line p1 p3;
draw_line p2 p3;;
(*
** square p1 p2 p3 p4
** Draw a square by drawing lines between
** all four points p1 p2 p3 p4. Type :
** val square : int * int -> int * int -> int * int -> int * int -> unit = <fun>
*)
let draw_square p1 p2 p3 p4 =
draw_line p1 p2;
draw_line p2 p3;
draw_line p3 p4;
draw_line p4 p1;;
(*
** ftoi x
** Transform a float into a int by rounding it. Type :
** val ftoi : float -> int = <fun>
*)
let ftoi x = int_of_float (Float.round x) ;;
(*
** itof x
** Transform a int into a float. Type :
** val itof : int -> float = <fun>
*)
let itof x = float_of_int x ;;
(*****************************************************************************)
(*
** mountain n (x,y) (z,t)
** displays a moutain of order n
** between (x,y) and (z,t)
*)
let mountain n (x, y) (z, t) =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let rec aux n (x, y) (z, t) =
if n <= 0 then draw_line (x,y) (z,t)
else
let m = (x + z) / 2 and
h = (y + t) / 2 + int(abs(z - x)/5 + 20) in
begin
aux (n-1) (x,y) (m,h);
aux (n-1) (m,h) (z,t);
end in
aux n (x, y) (z, t) ;;
(*open_graph " 900x500" ;*)
(*clear_graph ();*)
(*mountain 9 (10,200) (800,200) ;;*)
(*****************************************************************************)
(*
** dragon n (x,y) (z,t)
** displays a dragon of order n
** starting from (x,y) and (z,t)
*)
let dragon n (x, y) (z, t) =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let rec aux n (x, y) (z, t) =
if n <= 0 then draw_line (ftoi x, ftoi y) (ftoi z, ftoi t)
else
let u = (x+.z)/.2. +. (t-.y)/.2. and
v = (y+.t)/.2. -. (z-.x)/.2. in
begin
aux (n-1) (x, y) (u,v);
aux (n-1) (z, t) (u,v);
end
in aux n (itof x, itof y) (itof z, itof t) ;;
(*open_graph " 600x600" ;*)
(*clear_graph () ;*)
(*dragon 19 (150, 150) (350, 350) ;;*)
(*
** dragon_bonus n (x,y) (z,t)
** displays a dragon of order n
** starting from (x,y) and (z,t)
*)
let dragon_bonus n (x, y) (z, t) =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let rec aux n (x, y) (z, t) c =
if n <= 0 then draw_line (ftoi x, ftoi y) (ftoi z, ftoi t)
else
let u = (x+.z)/.2. +. (t-.y)/.2. and
v = (y+.t)/.2. -. (z-.x)/.2. in
begin
(* Change l'ordre des appels recursif en fonction du cote (gauche | droite) *)
if c = 'D' then (
aux (n-1) (x, y) (u,v) 'D';
aux (n-1) (z, t) (u,v) 'G';)
else (
aux (n-1) (z, t) (u,v) 'D';
aux (n-1) (x, y) (u,v) 'G';)
end
in aux n (itof x, itof y) (itof z, itof t) 'D' ;;
(*open_graph " 600x600" ; *)
(*clear_graph () ; *)
(*dragon_bonus 19 (150, 150) (350, 350) ;; *)
(*****************************************************************************)
(*
** koch_curve n (x,y) (z,t)
** displays a koch curve of order n
** from starting positions (x,y) and (z,t)
*)
(*
(mx,my)
.
/\
/ \
(x,y) ._______./ \._______. (z,t)
(x1,y1) (x2,y2)
*)
let koch_curve n (x,y) (z,t) =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let fsquare x = x *. x in
let pi = 4.0 *. atan 1.0 in
let rec aux n (x,y) (z,t) =
if n <= 0 then draw_line (ftoi x, ftoi y) (ftoi z, ftoi t)
else (
let l = sqrt(fsquare(z-.x) +. fsquare(t-.y)) /. 3. in
let difx = z -. x and
dify = t -. y in
let x1 = x +. (difx *. (1. /. 3.)) and
y1 = y +. (dify *. (1. /. 3.)) and
x2 = x +. (difx *. (2. /. 3.)) and
y2 = y +. (dify *. (2. /. 3.)) in
let mx = x1 +. (l *. (cos (pi/.3. +. (atan2 dify difx)))) and
my = y1 +. (l *. (sin (pi/.3. +. (atan2 dify difx)))) in
aux (n-1) (x,y) (x1,y1);
aux (n-1) (x1, y1) (mx, my);
aux (n-1) (mx,my) (x2, y2);
aux (n-1) (x2,y2) (z,t);
) in
aux n (itof x, itof y) (itof z, itof t) ;;
(*open_graph " 1020x300" ;*)
(*clear_graph () ;*)
(*koch_curve 6 (5,5) (1000,5) ;;*)
(*
** koch_snowflake n (x,y) (z,t)
** displays a koch snowflake of order n
** from starting position (x,y) and size d
*)
let koch_snowflake n (x,y) d =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let triangle_height = ftoi ((sqrt 3. /. 2.) *. itof d) in
begin
koch_curve n (x,y) (x+d/2, y - triangle_height);
koch_curve n (x-d/2, y - triangle_height) (x,y);
koch_curve n (x+d/2, y - triangle_height) (x-d/2, y - triangle_height);
end ;;
(*open_graph " 800x800" ;*)
(*clear_graph () ;*)
(*koch_snowflake 6 (400,700) 400;;*)
(*****************************************************************************)
(*
** carpet n (x,y)
** displays a Sierpinski carpet of order n
** from starting point (x,y)
*)
let carpet n (x,y) =
if n < 0 then invalid_arg "[Error] n cannot be negative !";
let rec aux (x,y) l =
if l >= 3. then
let l3 = l /. 3. in begin
(* Place a white square in the center *)
fill_rect (ftoi (x+.l3)) (ftoi (y+.l3)) (ftoi l3-1) (ftoi l3-1);
aux (x, y) l3;
aux (x +. l3, y) l3;
aux (x +. l3 *. 2., y) l3;
aux (x +. l3 *. 2., y +. l3) l3;
aux (x +. l3 *. 2., y +. l3 *. 2.) l3;
aux (x +. l3, y +. l3 *. 2.) l3;
aux (x, y +. l3 *. 2.) l3;
aux (x, y +. l3) l3;
end
in begin
set_color black;
fill_rect x y (n-1) (n-1);
set_color white;
aux (itof x, itof y) (itof n);
set_color black
end;;
(*open_graph " 300x300" ;*)
(*clear_graph () ;*)
(*carpet 243 (10,10) ;;*)
(*****************************************************************************)
(*
** sierpinski n (x,y) size
** displays a sierpinski triangle
** from (x,y) with size size
*)
(* _
/ \
/ \
/ \
(x2,y2) ./_______\. (x3,y3)
/\ /\
/ \ / \
/ \ / \
./______\./______\
(x,y) (x1,y1)
*)
let sierpinski n (x,y) size =
if n < 0 || size < 0 then invalid_arg "[Error] n or size cannot be negative !";
let rec aux n (x,y) l =
let triangle_height = (sqrt 3. /. 2.) *. l in
let l2 = l /. 2. in
if n > 0 && l >= 1. then
let (x1, y1) = (x +. l2, y) and
(x2, y2) = (x +. l2 /. 2., y +. triangle_height /. 2.) and
(x3, y3) = (x +. l2 +. l2 /. 2., y +. triangle_height /. 2.) in
begin
(* draw the center reversed triangle *)
draw_triangle (ftoi x1, ftoi y1) (ftoi x2, ftoi y2) (ftoi x3, ftoi y3);
aux (n-1) (x,y) l2;
aux (n-1) (x1,y1) l2;
aux (n-1) (x2,y2) l2;
end in
begin
let triangle_height = ftoi ((sqrt 3. /. 2.) *. itof size) in
draw_triangle (x,y) (x+size, y) (x+size/2, y + triangle_height);
aux n (itof x, itof y) (itof size);
end ;;
(*open_graph " 1100x600" ;*)
(*clear_graph ();*)
(*sierpinski 10 (5, 5) 300;;*)
(*****************************************************************************)
(*
** four_circles r (x,y) limit
** displays a circle of radius r from starting position (x,y)
** and with the drawing limit limit
*)
let four_circles r (x,y) limit =
if r < 0 || limit < 0 then invalid_arg "[Error] r or limit cannot be negative !";
let rec aux r (x,y) l =
if r >= (itof limit) then begin
draw_circle (ftoi x) (ftoi y) (ftoi r);
let new_r = r /. 2. in
aux new_r (x-.new_r, y) l;
aux new_r (x+.new_r, y) l;
aux new_r (x, y-.new_r) l;
aux new_r (x, y+.new_r) l;
end in
aux (itof r) (itof x, itof y) limit ;;
(*open_graph " 600x600" ;*)
(*clear_graph ();*)
(*four_circles 200 (300,300) 10;;*)
(*****************************************************************************)
(*
** arrow r (x,y) orient limit
** displays an arrow of radius r from starting position (x,y)
** with orientation orient and with the drawing limit limit
*)
let rec arrow r (x,y) orient limit =
if r < 0 || limit < 0 then invalid_arg "[Error] r or limit cannot be negative !";
if r > limit then
begin
fill_circle x y r;
let r2 = r/2 in
match orient with
| 'N' -> ( arrow r2 (x,y+r+r2) 'N' limit;
arrow r2 (x-r-r2,y) 'W' limit;
arrow r2 (x+r+r2,y) 'E' limit; )
| 'S' -> ( arrow r2 (x,y-r-r2) 'S' limit;
arrow r2 (x-r-r2,y) 'W' limit;
arrow r2 (x+r+r2,y) 'E' limit; )
| 'E' -> ( arrow r2 (x,y+r+r2) 'N' limit;
arrow r2 (x,y-r-r2) 'S' limit;
arrow r2 (x+r+r2,y) 'E' limit; )
| 'W' -> ( arrow r2 (x,y+r+r2) 'N' limit;
arrow r2 (x-r-r2,y) 'W' limit;
arrow r2 (x,y-r-r2) 'S' limit; )
| _ -> invalid_arg "orient must be either 'N' , 'S' , 'E' or 'W'"
end ;;
(*open_graph " 600x600" ;*)
(*clear_graph ();*)
(*arrow 100 (300,300) 'N' 0;;*)
(*****************************************************************************)
(*
** pytagora_tree n (x,y) size
** displays a pytagora tree of order n
** with starting position (x,y)
** and size n
*)
let pytagora_tree n (x,y) size =
if n < 0 || size < 0 then invalid_arg "[Error] n or size cannot be negative !";
let rec aux n (x1,y1) (x2,y2) =
if n > 0 then
let difx = x2 -. x1 and
dify = y2 -. y1 in
(* p3 = top of the triangle *)
let x3 = x1 +. 0.5 *. (difx -. dify) and
y3 = y1 +. 0.5 *. (dify +. difx) in
begin
let dx = x3 -. x1 and dy = y3 -. y1 in
(* p4 p5 = left square outer point*)
let x4 = x3 -. dy and y4 = y3 +. dx and
x5 = x1 -. dy and y5 = y1 +. dx in begin
draw_square (ftoi x1, ftoi y1) (ftoi x3, ftoi y3) (ftoi x4, ftoi y4) (ftoi x5, ftoi y5);
aux (n-1) (x5, y5) (x4, y4);
end;
let dx = x3 -. x2 and dy = y3 -. y2 in
(* p6 p7 = right square outer point*)
let x6 = x2 +. dy and y6 = y2 -. dx and
x7 = x3 +. dy and y7 = y3 -. dx in begin
draw_square (ftoi x2, ftoi y2) (ftoi x6, ftoi y6) (ftoi x7, ftoi y7) (ftoi x3, ftoi y3);
aux (n-1) (x7, y7) (x6, y6);
end;
end
in begin
draw_rect x y size size;
aux n (itof x, itof (y+size)) (itof (x+size), itof (y+size));
end;;
(*open_graph " 1000x800" ;*)
(*clear_graph ();*)
(*pytagora_tree 15 (450,5) 100;;*)
(*****************************************************************************)
(*
** vicsek_star n (x,y) size
** displays a vicsek star of order n
** from starting position (x,y) and of size size
*)
let vicsek_star n (x,y) size =
if n < 0 || size < 0 then invalid_arg "[Error] n or size cannot be negative !";
let rec aux n (x,y) l =
if n = 0 || l < 3 then fill_rect x y (l-1) (l-1)
else
let l3 = ftoi (itof l /. 3.) in begin
aux (n-1) (x, y) l3;
aux (n-1) (x + l3*2, y) l3;
aux (n-1) (x + l3, y + l3) l3;
aux (n-1) (x, y + l3*2) l3;
aux (n-1) (x + l3*2, y + l3*2) l3;
end
in aux n (x, y) size;;
(*open_graph " 510x510" ;*)
(*clear_graph ();*)
(*vicsek_star 5 (5,5) 500;;*)
(*****************************************************************************)