#open "graphics";; open_graph " 1000x800" ;; let simplexe = let z = 0. and u = 1. in let pt = [|(z,z,z);(z,z,u);(z,u,z);(u,z,z)|] in [[pt.(0);pt.(1);pt.(2)];[pt.(0);pt.(1);pt.(3)];[pt.(0);pt.(2);pt.(3)];[pt.(1);pt.(2);pt.(3)]] let rec projection (x,y,z) = (x,y) let draw_poly l = let (dx,dy) = hd l in moveto dx dy ; let rec foo = function | [] -> lineto dx dy | (x,y)::q -> lineto x y ; foo q in foo l let rec retypage (x,y) = (int_of_float (x*.200.)+200,int_of_float (y*.200.)+200) let rec affichage (a,c) = set_color c ; fill_poly (vect_of_list a) ; set_color black ; draw_poly a let rec rot1 (x,y,z) t = (x*.(cos t)-.y*.(sin t),y*.(cos t)+.x*.(sin t),z) let rec rot2 (x,y,z) t = (x,y*.(cos t)-.z*.(sin t),z*.(cos t)+.y*.(sin t)) let rec coupe l l' = function | [] -> (l,l') | a::q -> coupe l' (a::l) q let rec fusionne cmp = function | ([],a) -> a | (a,[]) -> a | (a::q,x::t) -> if cmp a x then x::(fusionne cmp (a::q,t)) else a::(fusionne cmp (q,x::t)) let rec tri cmp = function | [] -> [] | [a] -> [a] | l -> let (a,b) = coupe [] [] l in fusionne cmp (tri cmp a,tri cmp b) let colorise a = (rgb (random__int 255) (random__int 255) (random__int 255)) let bari l = let (x,y,z) = it_list (fun (x,y,z) (a,b,c) -> (x+.a,y+.b,z+.c)) (0.,0.,0.) l in (x/.(float_of_int (list_length l)),y/.(float_of_int (list_length l)),z/.(float_of_int (list_length l))) let flatten l = let rec foo res = function | [] -> res | a::q -> foo (a@res) q in foo [] l let flatten l = let rec ajoute l = function | [] -> l | a::q -> ajoute (a::l) q in let rec foo res = function | [] -> res | a::q -> foo (ajoute res a) q in foo [] l let rec entremele l l' = match (l,l') with | ([],a) -> [] | (a,[]) -> [] | (a::q,x::t) -> (a,x)::entremele q t let decoupe l' = let intern l= let bar = bari l in let final = hd l in let rec foo res = function | [] -> res | [a] -> [a;bar;final]::res | a::b::q -> foo ([a;bar;b]::res) (b::q) in (foo [] l) in flatten (map intern l') let tout = (* auto_synchronize false ;*) let forme = decoupe(decoupe (decoupe (decoupe simplexe))) in (* let forme = simplexe in*) let d = map colorise forme in for i = 1 to 100000 do clear_graph () ; let a = map (map (fun x -> rot2 (rot1 x(float_of_int i/.100. )) (float_of_int i/.1000.))) forme in let c = tri (fun (f,_) (g,_) -> let ((_,_,a),(_,_,b)) = (bari f,bari g) in a < b) (combine (a,d)) in let b = map (fun (l,c) -> (map retypage l,c)) (map (fun (l,c) -> (map projection l,c)) c) in map affichage b; (*; synchronize () *) (* decoupage -> rotation -> tri -> projection -> retypage -> colorisation -> affichage *) done ; 1 let carre = clear_graph(); for x = 0 to 255 do for y = 0 to 255 do set_color (rgb x y (255 - y)); plot x y; done; done;; carre();; let add (x,y) (a,b) = (a+.x,y+.b) let mul (x,y) (a,b) = (x*.a-.y*.b,a*.y+.b*.x) let conj (x,y) = (x,-.y) let modu a = fst (mul a (conj a)) let diverge m c = let rec foo v = function | 0 -> 0 | i -> if modu v >= 10.0 then i else foo (add (mul v v) c) (i-1) in m-(foo (0.,0.) m) let dessine = let eps = 0.03 in let x = -1.75 in let (ymax,ymin,xmax,xmin) = (0.+.eps,0.-.eps,x+.eps,x-.eps) in set_color black ; clear_graph () ; for x = 0 to 600 do for y = 0 to 400 do let n = diverge 42 (((float_of_int x)/.600.)*.(xmax-.xmin)+.xmin,((float_of_int y)/.400.)*.(ymax-.ymin)+.ymin) in if n < 42 then (set_color (rgb (n*2) (n*3) n) ; plot x y) done done let church_int n f x = let rec foo = function | 0 -> x | i -> f (foo (i-1)) in foo n let int_church n = n (fun x -> x+1) 0 let add n m f x = n f (m f x) let mul n m f x = n (m f) x let exp n m = m n let t = init_vect 100 (fun x -> church_int x) let c = int_church (add t.(24) t.(18)) let c = int_church (mul t.(24) t.(18)) let c = int_church (exp (church_int 2) (church_int 4)) let eval s = let rec lit_entier v i = if s.[i] >= `0` && s.[i] <= `9` then lit_entier (v*10+(int_of_char s.[i]-int_of_char`0`)) (i+1) else (i,v) in let rec lit_expr i = if s.[i] <> `(` then lit_entier 0 i else let (s1,v1) = lit_expr (i+1) in let (s2,v2) = lit_expr (s1+1) in (s2+1,match s.[s1] with | `+` -> v1+v2 | `-` -> v1-v2 | `*` -> v1*v2 | `/` -> v1/v2) in snd (lit_expr 0) let c = (eval "(((4*6)+(6*3))+(18/4))")