Skip to content

Instantly share code, notes, and snippets.

@dmaltsiniotis
Last active April 22, 2019 16:51
Show Gist options
  • Save dmaltsiniotis/90a29b298109b1e49b52 to your computer and use it in GitHub Desktop.
Save dmaltsiniotis/90a29b298109b1e49b52 to your computer and use it in GitHub Desktop.
A tiny mandelbot fractal generator written in F#
open System.Numerics
open System.Drawing
let mandelbrotTest c z = Complex.Pow(z, 2.0) + c
let rec inSet c iterations z =
let i = iterations - 1
let newz = mandelbrotTest c z
if newz.Magnitude > 2.0 then (c, false, i) else
if iterations > 0 then inSet c i newz else (c, true, i)
[<EntryPoint>]
let main argv =
let p0 = new Complex(0.0, 0.0)
let iterations = 50
let plot =
let stepSize = 0.01
seq { for i in -2.0 .. stepSize .. 2.0 do
for j in -2.0 .. stepSize .. 2.0 do
yield new Complex(i, j)}
let resultsToPlot = Seq.map (fun x -> inSet x iterations p0) plot
let imageResult = new Bitmap(401, 401)
Seq.iter (fun (a : Complex, b, i) -> imageResult.SetPixel(System.Convert.ToInt32(((a.Real + 2.0) * 100.0)), System.Convert.ToInt32(((a.Imaginary + 2.0) * 100.0)), if b then Color.Black else Color.White)) resultsToPlot
imageResult.Save("mandelbrot.bmp")
printf "done"
0
@dmaltsiniotis
Copy link
Author

These 25-ish lines of code produce a very basic Mandlebrot plot. This can easily be extended to add color to the different iteration cycles, or tweaked to scale up the detail and resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment