Techno Fattie

Josh Carroll

So recently I was reading secretGeek (a very underated blog I enjoy), and there was this interesting post about an F# program that Mr. Bambrick tweaked a little. The variation was interesting, and it piqued my interests into the cryptic style behind F#. However, just before I could get away with another quick read my geekish manhood was challenged.

"Your mission!
Next step: do the same thing rendering pixels to a bitmap instead... then i could try breaking that up amongst multiple cores... no... sleep time.

The interested reader is invited to finish this problem for me ;-)"


-- secretGeek



I had to do something or I could never again return to secretGeek without being pushed around by all the other readers (they think they're so cool with their big brains). So... I went home and geeked out in front of my laptop, which is kind of like Hulking out for nerds except for the muscles and awesomeness.


After installing the F# components and firing up VS, I copied the necessary file and began to make some modifications. After 3 hours of staring at documentation (Oh what I wouldn't give for some curly braces) and about 30 seconds of coding I came up with a pretty simple solution that works nicely.


Here are the relevant code changes:



let stepY = 0.1 / 40.0 //Scale goes in increments of 20 pixels for height
let stepX = 0.05 / 20.0
//Scale goes in increments of 60 pixels for width

//Simple declarations to get the height and width of the Bitmap
let numPixelsX = (int)(3.0 / stepX)
let numPixelsY = (int)(2.0 / stepY)
let bmp = new Bitmap(numPixelsX, numPixelsY)

//This is used to figure out what the current X and Y coordinates are
// during processing in order to set the appropriate pixels in the Bitmap

let getPosY y = (int)(Math.Abs((Math.Floor -1.0 - y) / stepY))
let getPosX x = (int)(Math.Abs((Math.Floor -2.0 - x) / stepX))

for y in [-1.0..stepY..1.0] do
    for x in [-2.0..stepX..1.0] do
        match mandelbrot (Complex.Create (x, y)) with
        | DidNotEscape -> bmp.SetPixel(getPosX x, getPosY y, Color.FromArgb(255, 255, 255));
        | Escaped e -> bmp.SetPixel(getPosX x, getPosY y, Color.FromArgb((min 9 e) * 20, 0, 0));
    done;
done;

bmp.Save(String.Format("Images\\mandelBrot_{0}_{1}.bmp", numPixelsX, numPixelsY), ImageFormat.Bmp)
bmp.Save(String.Format("Images\\mandelBrot_{0}_{1}.png", numPixelsX, numPixelsY), ImageFormat.Png)
bmp.Save(String.Format("Images\\mandelBrot_{0}_{1}.jpeg", numPixelsX, numPixelsY), ImageFormat.Jpeg)


And here is a sample of what the output looks like:



Cheers,

Josh

comments powered by Disqus