Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use "vectorfractal.sml";
- val wh=500
- (* Sägezahn Gradient *)
- val grad = createImg wh wh (fn (x,y) => x+y*wh);
- saveBMP "Grad.bmp" (toColorImage grad);
- (* viele Kreise *)
- fun sq x = x*x
- val radial = createImg wh wh (fn (x,y) => 256*(sq (x-wh div 2) + sq (y-wh div 2)) * 4 div wh*wh);
- saveBMP "Radial.bmp" (toColorImage radial);
- val wh=512
- (* Hopalong y-sgn(x)*|bx-c|^0.5 a-x *)
- val a = ~11.0;
- val b = 0.3;
- val c = ~0.5;
- val num=100000;
- val zoom=20.0;
- val hopalong = #3(iter num
- (0.0,0.0, createImg wh wh (fn (x,y) => 0))
- (fn (x,y,img) => (
- y-sign(x)*sqrt(abs(b*x-c)),
- a-x,
- setPixel img
- (fn _ => 255)
- (rnd (x*zoom) + wh div 2)
- (rnd (y*zoom) + wh div 2)
- handle _ => img)
- )
- )
- ;
- saveBMP "Hopalong.bmp" (toColorImage hopalong);
- (* Hopalong farbig *)
- val hopalong = #3(iter num
- (0.0,0.0, createImg wh wh (fn (x,y) => (0,0,0)), 0.0)
- (fn (x,y,img,col) => (
- y-sign(x)*sqrt(abs(b*x-c)),
- a-x,
- setPixel img
- (fn _ => hsvToRgb(col,0.8,0.8))
- (rnd (x*zoom) + wh div 2)
- (rnd (y*zoom) + wh div 2)
- handle _ => img,
- col+0.1)
- )
- )
- ;
- saveBMP "HopalongCol.bmp" hopalong;
- (* Mandelbrot Fraktal z^2+c *)
- val verbose=true
- val mandelbrot = fn maxIt => fn wh => createImg wh wh (fn (x,y) =>
- let
- val cx = Real.fromInt(x)/Real.fromInt(wh)*4.0-2.0
- val cy = Real.fromInt(y)/Real.fromInt(wh)*4.0-2.0
- val _ = if verbose andalso x =0 andalso y mod 100 = 0 then print(Int.toString(y)^"\n") else ()
- in
- 10 * #1(first' 0 (0.0,0.0)
- (fn (i,(x,y)) => i>maxIt orelse x*x+y*y>4.0)
- (fn (i,(x,y)) => (x*x-y*y+cx,2.0*x*y+cy)) )
- end
- );
- saveBMP "mandelbrot.bmp" (toColorImage (mandelbrot 20 512));
- (* val mand=mandelbrot 100 8192;
- saveGrayBMP "mandelbrot_t.png" mand; *)
- (* savePNG "mandelbrot.png" (colorGradient flameGradient (normalize2 (toRealImage (mandelbrot 100 4096)))); *)
- (* Burning Ship *)
- val ship = fn maxIt => fn wh => createImg wh wh (fn (x,y) =>
- let
- val cx = Real.fromInt(x)/Real.fromInt(wh)*0.25-1.875
- val cy = Real.fromInt(y)/Real.fromInt(wh)*0.25-0.125
- in
- #1(first' 0 (0.0,0.0)
- (fn (i,(x,y)) => i>maxIt orelse x*x+y*y>4.0)
- (fn (i,(x,y)) => (abs(x*x-y*y+cx),abs(2.0*x*y)+cy)) )
- end
- );
- saveBMP "ship.bmp" (toColorHSVImage 0.8 0.8 (toRealImage (ship 250 512)));
- (* savePNG "ship.png" (colorGradient blueGradient (normalize2 (toRealImage (ship 20 2048)))); *)
- (* Complex Plane *)
- val complex = createImg wh wh (fn (x,y) =>
- (hsvToRgb o complexToHsv)(Real.fromInt(x)/Real.fromInt(wh)*2.0-1.0,
- Real.fromInt(y)/Real.fromInt(wh)*2.0-1.0)
- );
- saveBMP "complex.bmp" complex;
- (* Newton *)
- val maxIt=20;
- (*
- val func = A(M(V "z", M(V "z",V "z")),C(1.0,0.0))
- val func = A(M(M(V "z",V "z"), M(V "z",V "z")),C(1.0,0.0))
- saveBMP "newtonZ4.bmp" newton;
- val func = A(P(V "z",8),S(M(C(15.0,0.0),P(V "z",4)),C(16.0,0.0)))
- saveBMP "newtonZ8+15z4-16.bmp" newton;
- val func = A(
- A(
- S(
- S(
- P(V "z",5),
- M(C(0.0,3.0),P(V "z",3))
- ),
- M(C(5.0,2.0),P(V "z",2))
- ),
- M(C(3.0,0.0),V "z")),
- C(1.0,0.0))
- val fileName="newtonz5-3iz3-(5+2i)z2+3z+1.bmp"
- val func = S(F("sin", V "z"),C(1.0,0.0))
- val fileName = "newtonSin.bmp"
- *)
- val func = S(P(V "z",3),C(1.0,0.0))
- val fileName = "newtonz3.bmp"
- val deriv = diff "z" func;
- val newton = createImg wh wh (fn (x,y) =>
- let
- val cx = Real.fromInt(x)/Real.fromInt(wh)*2.0-1.0
- val cy = Real.fromInt(y)/Real.fromInt(wh)*2.0-1.0
- in
- (hsvToRgb o complexToHsv)
- (iter maxIt (cx,cy)
- (fn (x,y) =>
- (eval (update empty "z" (x,y)) (S(V "z",D(func,deriv))))
- handle _ => (x,y)
- )
- )
- end
- );
- saveBMP fileName newton;
- (* Flame *)
- (* Perlin Noise *)
- 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);
- fun fillGrid oc img = imgMap (
- fn (x,y,old) =>
- let
- val xi= (x div oc) * oc
- val yi= (y div oc) * oc
- val a = getPixel img xi yi
- val b = getPixel img (xi+oc) yi
- val c = getPixel img xi (yi+oc)
- val d = getPixel img (xi+oc) (yi+oc)
- val px= Real.fromInt(x-xi)/Real.fromInt(oc)
- val py= Real.fromInt(y-yi)/Real.fromInt(oc)
- in
- (d*py+b*(1.0-py))*px+
- (c*py+a*(1.0-py))*(1.0-px)
- end handle _ => old
- ) img
- ;
- 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
- x::xs => List.foldl (fn (v,a) => zipWith op+ v a) x xs
- | _ => raise Subscript)
- ;
- saveBMP "perlinColor.bmp" (colorGradient rgbGradient (normalize2 perlin));
- saveBMP "perlinColor2.bmp" (colorGradient landGradient (normalize2 perlin));
- saveBMP "landscape.bmp" (colorGradient (#[(0,0,0),(34,139,34),(139,69,19),(100,100,100),(255,255,255)]) (normalize2 perlin));
- saveBMP "perlin.bmp" (toColorImage (normalize2 perlin));
- val width=Real.fromInt(wh)
- val height=width
- val marble = imgMap (fn (x,y,a) =>
- rnd(255.0*Real.abs(
- Math.sin(Math.pi*(
- Real.fromInt(x)*5.0/width+
- Real.fromInt(y)*10.0/height+
- 3.0*Real.fromInt(a)/256.0
- ))
- ))
- ) (normalize2 perlin)
- val _ = saveBMP "marble.bmp" (toColorImage marble)
- val wood = imgMap (fn (x,y,a) =>
- case rnd(128.0*Real.abs(
- Math.sin(2.0*12.0*Math.pi*(
- Math.sqrt(
- Math.pow(Real.fromInt(x)/width-0.5,2.0)+
- Math.pow(Real.fromInt(y)/height-0.5,2.0)
- )+
- 0.05*Real.fromInt(a)/256.0
- ))
- )) of c => (80+c,30+c,30)
- ) (normalize2 perlin)
- val _ = saveBMP "wood.bmp" wood
- val fire = imgMap (fn (x,y,a) =>
- Int.min(255,rnd(1.3*Real.fromInt(a*y div wh)))
- ) (stretch (1.0,0.3) (normalize2 perlin))
- val _ = saveBMP "fire.bmp" (colorGradient flameGradient fire)
- val sea = imgMap (fn (x,y,a) =>
- rnd(255.0*Real.abs(Real.rem(
- 5.0*Real.fromInt(y)/height+
- 0.2*Math.sin(Math.pi*5.0*Real.fromInt(x)/width)+
- 1.0*Real.fromInt(a)/256.0
- ,
- 1.0)))
- ) (normalize2 perlin)
- ;
- val _ = saveBMP "sea.bmp" (colorGradient seaGradient sea)
- (* Snowflake *)
- (* don't cheat *)
- (*IFS*)
- val wh=512
- val width=Real.fromInt(wh)
- val height=width
- val sierpinski = #[
- fn (x,y) => (x/2.0,y/2.0),
- fn (x,y) => (x/2.0+0.5,y/2.0),
- fn (x,y) => (x/2.0+0.25,y/2.0+0.5)
- ];
- val farn = #[
- fn (x,y) => (0.0,0.16*x),
- fn (x,y) => (0.2*x-0.26*y,0.23*x+0.22*y+1.6),
- fn (x,y) => (~0.15*x+0.28*y,0.26*x+0.24*y+0.44),
- fn (x,y) => (0.85*x+0.04*y,~0.04*x+0.85*y+1.6)
- ]
- ;
- val koch = #[
- fn (x,y) => (x/3.0,y/3.0),
- 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),
- 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),
- fn (x,y) => (x/3.0+2.0/3.0,y/3.0)
- ]
- ;
- val koch2 = #[
- fn (x,y) => (x/3.0,y/3.0),
- fn (x,y) => (~y/3.0+1.0/3.0,x/3.0),
- fn (x,y) => (x/3.0+1.0/3.0,y/3.0+1.0/3.0),
- fn (x,y) => (~y/3.0+2.0/3.0,x/3.0),
- fn (x,y) => (x/3.0+2.0/3.0,y/3.0)
- ]
- ;
- val IFS = fn f => fn ((xmin,xmax),(ymin,ymax)) => #2(iter 100000
- ((randomReal(),randomReal()), createImg wh wh (fn (x,y) => 0),0)
- (fn (p as (x,y),img,i) => (
- (Vector.sub(f,Random.randRange(0,Vector.length f-1) rand)) p,
- if i<20 then img else
- setPixel2 img
- (fn _ => 255)
- (rnd ((x-xmin)/(xmax-xmin)*width), rnd ((y-ymin)/(ymax-ymin)*height))
- handle _ => img,
- i+1)
- )
- )
- ;
- saveBMP "flame_sierpinski.bmp" (toColorImage (IFS sierpinski ((0.0,1.0),(0.0,1.0))));
- saveBMP "flame_farn.bmp" (toColoredImage (0,1,0) (IFS farn ((~2.0,2.0),(0.0,10.0))));
- saveBMP "flame_koch.bmp" (toColorImage (IFS koch ((0.0,1.0),(0.0,1.0))));
- 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