Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mandelzoom complete
open System.Drawing
open System.Numerics
open System.Windows.Forms
// Absolute Square function for complex numbers
type System.Numerics.Complex with
member this.Length = this.Real * this.Real + this.Imaginary * this.Imaginary
// PART ONE: select any square region of the complex plane to be examined.
// Number of rows and columns in the pic array
let rows = 400
let columns = 400
//let title = "Mandelbrot Set"
//// The coordinates of the southwest corner
let acorner = -2.0
let bcorner = -1.25 // imaginary part
//// Length of each side
let side = 2.5
//let title = "Seahorse Valley"
//// The coordinates of the southwest corner
//let acorner = -0.748766713922161 // real part
//let bcorner = 0.123640844894862 // imaginary part
//// Length of each side
//let side = 0.000000006150404
//let title = "Details"
///// The coordinates of the southwest corner
//let acorner = -0.7436439 - 4.93E-08 / 2.0 // real part
//let bcorner = 0.1318258909 - 4.93E-08 / 2.0 // imaginary part
//// Length of each side
//let side = 4.93E-08
//let title = "More Details"
//// The coordinates of the southwest corner
//let acorner = -0.745195 - 0.00143 / 2.0 // real part
//let bcorner = 0.112675 - 0.00143 / 2.0 // imaginary part
//// Length of each side
//let side = 0.00143
// PART TWO. adjusts the array pic to match the square of interest
// Distance within the square (determined by the southwest corner and length of each side)
// between each adjacent pixels
let gap = side / float (columns)
let maxIterations = 1000
// PART THREE: the heart of the program. Here a search is made for the
// complex numbers c in the Mandelbrot set, and colors are assigned to the // numbers that are, in a special sense, nearby.
// PART THREE: step 1
// Transforms pixel positions into corresponding complex numbers
let transformPixelPositionToComplexNumber x y =
new Complex((float) x * gap + acorner, (float) y * gap - (bcorner + side))
// PART THREE: step 3
// A point in the complex plane c belong to the Mandelbrot Set
// if the sequence Z(n+1) = Z(n)^2 + c stays bounded.
// By definition Z(0) = 0
let rec MandelbrotSeq (z : Complex) c iterationNumber =
if (z.Length > 4.0) then iterationNumber
else if (iterationNumber = maxIterations) then maxIterations
else
let nextValue = z * z + c
MandelbrotSeq nextValue c (iterationNumber + 1)
// The color scheme originates from this StackOverflow discussion and is picked for its
// similarity to the color scheme used in the WikiPedia article about the Mandelbrot set
// http://stackoverflow.com/questions/16500656/which-color-gradient-is-used-to-color-mandelbrot-in-wikipedia
let mandelbrotColorSchemeWikipedia =
Map.ofList [ (0, Color.FromArgb(66, 30, 15)) // brown 3
(1, Color.FromArgb(25, 7, 26)) // dark violett
(2, Color.FromArgb(9, 1, 47)) // darkest blue
(3, Color.FromArgb(4, 4, 73)) // blue 5
(4, Color.FromArgb(0, 7, 100)) // blue 4
(5, Color.FromArgb(12, 44, 138)) // blue 3
(6, Color.FromArgb(24, 82, 177)) // blue 2
(7, Color.FromArgb(57, 125, 209)) // blue 1
(8, Color.FromArgb(134, 181, 229)) // blue 0
(9, Color.FromArgb(211, 236, 248)) // lightest blue
(10, Color.FromArgb(241, 233, 191)) // lightest yellow
(11, Color.FromArgb(248, 201, 95)) // light yellow
(12, Color.FromArgb(255, 170, 0)) // dirty yellow
(13, Color.FromArgb(204, 128, 0)) // brown 0
(14, Color.FromArgb(153, 87, 0)) // brown 1
(15, Color.FromArgb(106, 52, 3)) ] // brown 2
// PART THREE: step 4
// Map number of iterations to convergence to a color
let colorMap n maxIterations =
match n with
| n when (n < maxIterations && n > 0) -> mandelbrotColorSchemeWikipedia.[n % 16]
| _ -> Color.FromArgb(0, 0, 0)
/// Returns whether or not the given complex number is in the set to the
/// best possible accuracy we can get from the given number of iterations
let inMandelbrotSet num =
let res = MandelbrotSeq Complex.Zero num 0
colorMap res maxIterations
/// Sets up the canvas and generates the set on it
let generateBitMapWithMandelbrotSet (rows : int) (columns : int) =
let bmp = new Bitmap(rows, columns)
for m in 0..(rows - 1) do
for n in 0..(columns - 1) do
let c = transformPixelPositionToComplexNumber m n
bmp.SetPixel(m, n, (inMandelbrotSet c))
bmp
// Show the computed Mandelbrot set on the display
let form = new Form(Visible = true, Width = columns, Height = rows, Text = title)
let box = new PictureBox(BackColor = Color.White, Dock = DockStyle.Fill, SizeMode = PictureBoxSizeMode.CenterImage)
let image = generateBitMapWithMandelbrotSet columns rows
//image.Save (@"E:\Mandelzoom\Mandelzoom\" + title)
form.Controls.Add(box)
box.Image <- (image :> Image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment