Mandelbrot set: Difference between revisions

Added alternate F# version, including both text and GUI
(Added alternate F# version, including both text and GUI)
Line 2,582:
let f = new Mandel()
do Application.Run(f)</lang>
 
=== Alternate version, applicable to text and GUI ===
 
==== Basic generation code ====
<lang fsharp>
let getMandelbrotValues width height maxIter ((xMin,xMax),(yMin,yMax)) =
let mandIter (cr:float,ci:float) =
let next (zr,zi) = (cr + (zr * zr - zi * zi)), (ci + (zr * zi + zi * zr))
let rec loop = function
| step,_ when step=maxIter->0
| step,(zr,zi) when ((zr * zr + zi * zi) > 2.0) -> step
| step,z -> loop ((step + 1), (next z))
loop (0,(0.0, 0.0))
let forPos =
let dx, dy = (xMax - xMin) / (float width), (yMax - yMin) / (float height)
fun y x -> mandIter ((xMin + dx * float(x)), (yMin + dy * float(y)))
[0..height-1] |> List.map(fun y->[0..width-1] |> List.map (forPos y))
</lang>
 
==== Text generation ====
<lang fsharp>
getMandelbrotValues 80 25 50 ((-2.0,1.0),(-1.0,1.0))
|> List.map(fun row-> row |> List.map (function | 0 ->" " |_->".") |> String.concat "")
|> List.iter (printfn "%s")
</lang>
 
Results:
{{out}}
<pre>
................................................................................
................................................................................
................................................. .............................
................................................ ...........................
................................................. ...........................
....................................... . ......................
........................................ .................
.................................... ..................
.................................... .................
.......................... ...... ................
....................... ... ................
..................... . ................
................. .................
................. .................
..................... . ................
....................... ... ................
.......................... ...... ................
.................................... .................
.................................... ..................
........................................ .................
....................................... . ......................
................................................. ...........................
................................................ ...........................
................................................. .............................
................................................................................
</pre>
 
==== GUI Generation ====
<lang fsharp>
open System.Drawing
open System.Windows.Forms
 
let showGraphic (colorForIter: int -> Color) (width: int) (height:int) maxIter view =
new Form()
|> fun frm ->
frm.Width <- width
frm.Height <- height
frm.BackgroundImage <-
new Bitmap(width,height)
|> fun bmp ->
getMandelbrotValues width height maxIter view
|> List.mapi (fun y row->row |> List.mapi (fun x v->((x,y),v))) |> List.collect id
|> List.iter (fun ((x,y),v) -> bmp.SetPixel(x,y,(colorForIter v)))
bmp
frm.Show()
 
let toColor = (function | 0 -> (0,0,0) | n -> ((31 &&& n) |> fun x->(0, 18 + x * 5, 36 + x * 7))) >> Color.FromArgb
 
showGraphic toColor 640 480 5000 ((-2.0,1.0),(-1.0,1.0))
</lang]
 
=={{header|Factor}}==
Anonymous user