Advertisement
SkullCoder

challenges

Jan 2nd, 2019
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 8.59 KB | None | 0 0
  1. use "vectorfractal.sml";
  2.  
  3.  
  4.  
  5. val wh=500
  6.  
  7. (* Sägezahn Gradient *)
  8.  
  9. val grad = createImg wh wh (fn (x,y) => x+y*wh);
  10. saveBMP "Grad.bmp" (toColorImage grad);
  11.  
  12. (* viele Kreise *)
  13.  
  14. fun sq x = x*x
  15. val radial = createImg wh wh (fn (x,y) => 256*(sq (x-wh div 2) + sq (y-wh div 2)) * 4 div wh*wh);
  16. saveBMP "Radial.bmp" (toColorImage radial);
  17.  
  18. val wh=512
  19.  
  20.  
  21. (* Hopalong y-sgn(x)*|bx-c|^0.5 a-x *)
  22.  
  23. val a = ~11.0;
  24. val b = 0.3;
  25. val c = ~0.5;
  26. val num=100000;
  27. val zoom=20.0;
  28.  
  29. val hopalong = #3(iter num
  30.         (0.0,0.0, createImg wh wh (fn (x,y) => 0))
  31.         (fn (x,y,img) => (
  32.             y-sign(x)*sqrt(abs(b*x-c)),
  33.             a-x,
  34.             setPixel img
  35.                 (fn _ => 255)
  36.                 (rnd (x*zoom) + wh div 2)
  37.                 (rnd (y*zoom) + wh div 2)
  38.             handle _ => img)
  39.         )
  40.     )
  41. ;
  42. saveBMP "Hopalong.bmp" (toColorImage hopalong);
  43.  
  44. (* Hopalong farbig *)
  45.  
  46. val hopalong = #3(iter num
  47.         (0.0,0.0, createImg wh wh (fn (x,y) => (0,0,0)), 0.0)
  48.         (fn (x,y,img,col) => (
  49.             y-sign(x)*sqrt(abs(b*x-c)),
  50.             a-x,
  51.             setPixel img
  52.                 (fn _ => hsvToRgb(col,0.8,0.8))
  53.                 (rnd (x*zoom) + wh div 2)
  54.                 (rnd (y*zoom) + wh div 2)
  55.             handle _ => img,
  56.             col+0.1)
  57.         )
  58.     )
  59. ;
  60. saveBMP "HopalongCol.bmp" hopalong;
  61.  
  62.  
  63. (* Mandelbrot Fraktal z^2+c *)
  64. val verbose=true
  65. val mandelbrot = fn maxIt => fn wh => createImg wh wh (fn (x,y) =>
  66.         let
  67.             val cx = Real.fromInt(x)/Real.fromInt(wh)*4.0-2.0
  68.             val cy = Real.fromInt(y)/Real.fromInt(wh)*4.0-2.0
  69.             val _ = if verbose andalso x =0 andalso y mod 100 = 0 then print(Int.toString(y)^"\n") else ()
  70.         in
  71.             10 * #1(first' 0 (0.0,0.0)
  72.                 (fn (i,(x,y)) => i>maxIt orelse x*x+y*y>4.0)
  73.                 (fn (i,(x,y)) => (x*x-y*y+cx,2.0*x*y+cy)) )
  74.         end
  75.     );
  76. saveBMP "mandelbrot.bmp" (toColorImage (mandelbrot 20 512));
  77.  
  78. (* val mand=mandelbrot 100 8192;
  79. saveGrayBMP "mandelbrot_t.png" mand; *)
  80. (* savePNG "mandelbrot.png" (colorGradient flameGradient (normalize2 (toRealImage (mandelbrot 100 4096)))); *)
  81.  
  82. (* Burning Ship *)
  83.  
  84. val ship = fn maxIt => fn wh => createImg wh wh (fn (x,y) =>
  85.         let
  86.             val cx = Real.fromInt(x)/Real.fromInt(wh)*0.25-1.875
  87.             val cy = Real.fromInt(y)/Real.fromInt(wh)*0.25-0.125
  88.         in
  89.              #1(first' 0 (0.0,0.0)
  90.                 (fn (i,(x,y)) => i>maxIt orelse x*x+y*y>4.0)
  91.                 (fn (i,(x,y)) => (abs(x*x-y*y+cx),abs(2.0*x*y)+cy)) )
  92.         end
  93.     );
  94. saveBMP "ship.bmp" (toColorHSVImage 0.8 0.8 (toRealImage (ship 250 512)));
  95. (* savePNG "ship.png" (colorGradient blueGradient (normalize2 (toRealImage (ship 20 2048)))); *)
  96.  
  97. (* Complex Plane *)
  98.  
  99. val complex = createImg wh wh (fn (x,y) =>
  100.         (hsvToRgb o complexToHsv)(Real.fromInt(x)/Real.fromInt(wh)*2.0-1.0,
  101.             Real.fromInt(y)/Real.fromInt(wh)*2.0-1.0)
  102.     );
  103. saveBMP "complex.bmp" complex;
  104.  
  105.  
  106. (* Newton *)
  107.  
  108. val maxIt=20;
  109. (*
  110. val func = A(M(V "z", M(V "z",V "z")),C(1.0,0.0))
  111. val func = A(M(M(V "z",V "z"), M(V "z",V "z")),C(1.0,0.0))
  112. saveBMP "newtonZ4.bmp" newton;
  113. val func = A(P(V "z",8),S(M(C(15.0,0.0),P(V "z",4)),C(16.0,0.0)))
  114. saveBMP "newtonZ8+15z4-16.bmp" newton;
  115. val func = A(
  116.         A(
  117.           S(
  118.             S(
  119.                 P(V "z",5),
  120.                 M(C(0.0,3.0),P(V "z",3))
  121.              ),
  122.              M(C(5.0,2.0),P(V "z",2))
  123.             ),
  124.           M(C(3.0,0.0),V "z")),
  125.         C(1.0,0.0))
  126. val fileName="newtonz5-3iz3-(5+2i)z2+3z+1.bmp"
  127. val func = S(F("sin", V "z"),C(1.0,0.0))
  128. val fileName = "newtonSin.bmp"
  129. *)
  130. val func = S(P(V "z",3),C(1.0,0.0))
  131. val fileName = "newtonz3.bmp"
  132. val deriv = diff "z" func;
  133.  
  134. val newton = createImg wh wh (fn (x,y) =>
  135.         let
  136.             val cx = Real.fromInt(x)/Real.fromInt(wh)*2.0-1.0
  137.             val cy = Real.fromInt(y)/Real.fromInt(wh)*2.0-1.0
  138.         in
  139.             (hsvToRgb o complexToHsv)
  140.                 (iter maxIt (cx,cy)
  141.                     (fn (x,y) =>
  142.                         (eval (update empty "z" (x,y)) (S(V "z",D(func,deriv))))
  143.                         handle _ => (x,y)
  144.                     )
  145.                 )
  146.         end
  147.     );
  148. saveBMP fileName newton;
  149.  
  150.  
  151. (* Flame *)
  152.  
  153. (* Perlin Noise *)
  154.  
  155. fun randomGrid w h oc = createImg w h (fn (x,y) => if x mod oc = 0 andalso y mod oc = 0 then randomReal() else 0.0);
  156. fun fillGrid oc img = imgMap (
  157.         fn (x,y,old) =>
  158.             let
  159.                 val xi= (x div oc) * oc
  160.                 val yi= (y div oc) * oc
  161.                 val a = getPixel img xi yi
  162.                 val b = getPixel img (xi+oc) yi
  163.                 val c = getPixel img xi (yi+oc)
  164.                 val d = getPixel img (xi+oc) (yi+oc)
  165.                 val px= Real.fromInt(x-xi)/Real.fromInt(oc)
  166.                 val py= Real.fromInt(y-yi)/Real.fromInt(oc)
  167.             in
  168.                 (d*py+b*(1.0-py))*px+
  169.                 (c*py+a*(1.0-py))*(1.0-px)
  170.             end handle _ => old
  171.     ) img
  172. ;
  173.  
  174. val perlin = crop (wh,wh) (case List.map (fn x => imageMap (fn a => a*Real.fromInt(x)) (fillGrid x (randomGrid (wh+1) (wh+1) x))) [2,4,8,16,32,64] of
  175.         x::xs => List.foldl (fn (v,a) => zipWith op+ v a) x xs
  176.         | _ => raise Subscript)
  177. ;
  178. saveBMP "perlinColor.bmp" (colorGradient rgbGradient (normalize2 perlin));
  179. saveBMP "perlinColor2.bmp" (colorGradient landGradient (normalize2 perlin));
  180. saveBMP "landscape.bmp" (colorGradient (#[(0,0,0),(34,139,34),(139,69,19),(100,100,100),(255,255,255)]) (normalize2 perlin));
  181. saveBMP "perlin.bmp" (toColorImage (normalize2 perlin));
  182.  
  183.  
  184. val width=Real.fromInt(wh)
  185. val height=width
  186.  
  187. val marble = imgMap (fn (x,y,a) =>
  188.     rnd(255.0*Real.abs(
  189.         Math.sin(Math.pi*(
  190.             Real.fromInt(x)*5.0/width+
  191.             Real.fromInt(y)*10.0/height+
  192.             3.0*Real.fromInt(a)/256.0
  193.         ))
  194.     ))
  195.     ) (normalize2 perlin)
  196. val _ = saveBMP "marble.bmp" (toColorImage marble)
  197.  
  198. val wood = imgMap (fn (x,y,a) =>
  199.     case rnd(128.0*Real.abs(
  200.         Math.sin(2.0*12.0*Math.pi*(
  201.             Math.sqrt(
  202.                 Math.pow(Real.fromInt(x)/width-0.5,2.0)+
  203.                 Math.pow(Real.fromInt(y)/height-0.5,2.0)
  204.             )+
  205.             0.05*Real.fromInt(a)/256.0
  206.         ))
  207.     )) of c => (80+c,30+c,30)
  208.     ) (normalize2 perlin)
  209. val _ = saveBMP "wood.bmp" wood
  210.  
  211. val fire = imgMap (fn (x,y,a) =>
  212.     Int.min(255,rnd(1.3*Real.fromInt(a*y div wh)))
  213.     ) (stretch (1.0,0.3) (normalize2 perlin))
  214. val _ = saveBMP "fire.bmp" (colorGradient flameGradient fire)
  215.  
  216. val sea = imgMap (fn (x,y,a) =>
  217.     rnd(255.0*Real.abs(Real.rem(
  218.             5.0*Real.fromInt(y)/height+
  219.             0.2*Math.sin(Math.pi*5.0*Real.fromInt(x)/width)+
  220.             1.0*Real.fromInt(a)/256.0
  221.         ,
  222.     1.0)))
  223.     ) (normalize2 perlin)
  224. ;
  225. val _ = saveBMP "sea.bmp" (colorGradient seaGradient sea)
  226.  
  227. (* Snowflake *)
  228.  
  229. (* don't cheat *)
  230.  
  231.  
  232. (*IFS*)
  233.  
  234. val wh=512
  235. val width=Real.fromInt(wh)
  236. val height=width
  237.  
  238.  
  239. val sierpinski = #[
  240.     fn (x,y) => (x/2.0,y/2.0),
  241.     fn (x,y) => (x/2.0+0.5,y/2.0),
  242.     fn (x,y) => (x/2.0+0.25,y/2.0+0.5)
  243. ];
  244. val farn = #[
  245.     fn (x,y) => (0.0,0.16*x),
  246.     fn (x,y) => (0.2*x-0.26*y,0.23*x+0.22*y+1.6),
  247.     fn (x,y) => (~0.15*x+0.28*y,0.26*x+0.24*y+0.44),
  248.     fn (x,y) => (0.85*x+0.04*y,~0.04*x+0.85*y+1.6)
  249. ]
  250. ;
  251. val koch = #[
  252.     fn (x,y) => (x/3.0,y/3.0),
  253.     fn (x,y) => (x/6.0-Math.sqrt(3.0)*y/6.0+1.0/3.0, Math.sqrt(3.0)*x/6.0+y/6.0),
  254.     fn (x,y) => (~x/6.0+Math.sqrt(3.0)*y/6.0+2.0/3.0,Math.sqrt(3.0)*x/6.0+y/6.0),
  255.     fn (x,y) => (x/3.0+2.0/3.0,y/3.0)
  256. ]
  257. ;
  258. val koch2 = #[
  259.     fn (x,y) => (x/3.0,y/3.0),
  260.     fn (x,y) => (~y/3.0+1.0/3.0,x/3.0),
  261.     fn (x,y) => (x/3.0+1.0/3.0,y/3.0+1.0/3.0),
  262.     fn (x,y) => (~y/3.0+2.0/3.0,x/3.0),
  263.     fn (x,y) => (x/3.0+2.0/3.0,y/3.0)
  264. ]
  265. ;
  266. val IFS = fn f => fn ((xmin,xmax),(ymin,ymax)) =>  #2(iter 100000
  267.         ((randomReal(),randomReal()), createImg wh wh (fn (x,y) => 0),0)
  268.         (fn (p as (x,y),img,i) => (
  269.             (Vector.sub(f,Random.randRange(0,Vector.length f-1) rand)) p,
  270.             if i<20 then img else
  271.                 setPixel2 img
  272.                     (fn _ => 255)
  273.                     (rnd ((x-xmin)/(xmax-xmin)*width), rnd ((y-ymin)/(ymax-ymin)*height))
  274.                 handle _ => img,
  275.             i+1)
  276.         )
  277.     )
  278. ;
  279. saveBMP "flame_sierpinski.bmp" (toColorImage (IFS sierpinski ((0.0,1.0),(0.0,1.0))));
  280. saveBMP "flame_farn.bmp" (toColoredImage (0,1,0) (IFS farn ((~2.0,2.0),(0.0,10.0))));
  281. saveBMP "flame_koch.bmp" (toColorImage (IFS koch ((0.0,1.0),(0.0,1.0))));
  282. saveBMP "flame_koch2.bmp" (toColorImage (IFS koch2 ((0.0,1.0),(0.0,1.0))));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement