#light #nowarn "57" //don't warn me about possibly deprecated features. open Microsoft.FSharp.Math open System open System.Drawing open System.Windows.Forms let maxIteration = 50 let modSquared (c : Complex) = c.r * c.r + c.i * c.i //let modSquared (c : Complex) = c.r * c.r + c.i <-- try this interesting variation! type MandelbrotResult = | DidNotEscape | Escaped of int let mandelbrot c = let rec mandelbrotInner z iterations = if(modSquared z >= 4.0) then Escaped iterations elif iterations = maxIteration then DidNotEscape else mandelbrotInner (( z * z ) + c) (iterations + 1) mandelbrotInner c 0 let min a b = if (a > b) then b else a let max a b = if (a < b) then b else a //Determine what color a given location in the complex plane should be (*let colorbrot dx dy = match mandelbrot (Complex.Create (dx, dy)) with | DidNotEscape -> System.Drawing.Color.Black | Escaped e -> System.Drawing.Color.FromArgb(max 0 (255 - 7*e), max 0 (255 - 9*e), max 0 (255 - 16*e))*) let colorbrot dx dy = match mandelbrot (Complex.Create (dx, dy)) with | DidNotEscape -> System.Drawing.Color.White | Escaped e -> System.Drawing.Color.FromArgb((min 255 (10*e)), (min 255 3*e),0)//max 0 (255 - 9*e), max 0 (255 - 16*e)) //given x and y pixel coords, with an offset newX newY, and a zoom ratio -- //return the correct color let color x y newX newY zoom = colorbrot (((float)x - 380.0 + ((float)newX) )/(((float)zoom) * 150.0)) (((float)y - 240.0 + ((float)newY) )/(((float)zoom) * 150.0)) let drawbrot zoom (pictureBox : PictureBox) newX newY = let bm = new Bitmap(pictureBox.Width, pictureBox.Height, Imaging.PixelFormat.Format24bppRgb) for y in [0..pictureBox.Height-1] do for x in [0..pictureBox.Width-1] do bm.SetPixel(x, y, (color x y newX newY zoom)) pictureBox.Image <- bm pictureBox.Invalidate() let form = new Form (Text="Mandelbroth, secretGeek.net, thanks to LukeH", Width=640, Height=494) do let bm = new Bitmap(form.Width, form.Height, Imaging.PixelFormat.Format24bppRgb) for y in [0..bm.Height-1] do for x in [0..bm.Width-1] do bm.SetPixel(x, y, (colorbrot (((float)x - 380.0)/150.0) (((float)y - 240.0)/150.0))) let pictureBox = new PictureBox(Dock=DockStyle.Fill) pictureBox.Image <- bm let slider = new System.Windows.Forms.TrackBar() form.Controls.Add(slider) pictureBox.MouseClick.Add(fun e -> drawbrot slider.Value pictureBox e.X e.Y) slider.Scroll.Add(fun _ -> drawbrot slider.Value pictureBox 0 0 ) slider.Value <- 2 form.Controls.Add(pictureBox) [] do Application.Run(form) //Here's some powershell that generates every combo of x,y where x = 1..3 and y = 4..6 // 1..3 | %{ $x = $_; $x; 4..6 | %{ $y = $_; $y ; $x}} | % {$x; $y}