Advertisement
Guest User

Mandelbrot F#

a guest
Nov 17th, 2010
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.74 KB | None | 0 0
  1. open System
  2. open System.Drawing
  3. open System.Drawing.Imaging
  4. open System.Runtime.InteropServices
  5.  
  6. let CalcZ (coord:float * float) =
  7.  
  8.     let maxIterations = 255
  9.  
  10.     let rec CalcZHelper (xCoord:float) (yCoord:float) // line break inserted
  11.            (x:float) (y:float) iters =
  12.         let newx = x * x + xCoord - y * y
  13.         let newy = 2.0 * x * y + yCoord
  14.         match newx, newy, iters with
  15.         | _ when Math.Abs newx > 2.0 -> iters
  16.         | _ when Math.Abs newy > 2.0 -> iters
  17.         | _ when iters = maxIterations -> iters
  18.         | _ -> CalcZHelper xCoord yCoord newx newy (iters + 1)
  19.  
  20.     CalcZHelper (fst coord) (snd coord) (fst coord) (snd coord) 0
  21.  
  22. let colorArray = Array.init 256 (fun i -> [|byte i;byte i;byte i;255uy|])
  23. let xDim = 1920
  24. let yDim = 1200
  25. let scale = 1./500.
  26. let conv (x,y) =
  27.     (float (x - xDim / 2 - (xDim / 4))) * scale
  28.     ,(float (y - yDim / 2)) * scale
  29. // Append this code with one of the following...
  30.  
  31.    
  32. // Fast version:
  33. let screen =
  34.     Array.init (xDim*yDim) (fun i -> CalcZ <| conv (i % xDim, i / xDim))
  35.     |> Array.collect (fun i -> colorArray.[i])
  36. let gch = GCHandle.Alloc(screen, GCHandleType.Pinned)
  37. let bmp = new Bitmap(xDim, yDim, xDim * 4, PixelFormat.Format32bppArgb, gch.AddrOfPinnedObject())
  38. gch.Free()
  39. bmp.Save(@"C:\Documents and Settings\All Users\Desktop\out.png")
  40.  
  41.    
  42. // Low memory version:
  43. let screen =
  44.     Seq.init (xDim*yDim) (fun i -> CalcZ <| conv (i % xDim, i / xDim))
  45.     |> Seq.collect (fun i -> colorArray.[i])
  46.     |> Seq.toArray
  47. let gch = GCHandle.Alloc(screen, GCHandleType.Pinned)
  48. let bmp = new Bitmap(xDim, yDim, xDim * 4, PixelFormat.Format32bppArgb, gch.AddrOfPinnedObject())
  49. gch.Free()
  50. bmp.Save(@"C:\Documents and Settings\All Users\Desktop\out.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement