2 people like it.

Here's another fine fractal you've gotten me into

An ASCII Mandelbrot visualisation. (Suggested by a tweet from Jon Harrop, but any bugs are mine.)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
module Mandelbrot

open System

let countIters x0 y0 maxIter =
    let rec iters x y i =
        if (x*x + y*y < 4.0) && (i < maxIter) then
            let x' = x*x - y*y + x0
            let y' = 2.*x*y + y0
            let i' = i+1
            iters x' y' i'
        else
            i
    iters 0. 0. 0

let mandelMap xMin xMax yMin yMax stepSize maxIter =
    let xCount, yCount = (xMax - xMin) / stepSize |> int, (yMax - yMin) / stepSize |> int
    Array2D.init xCount yCount (fun x y -> countIters (float(x)*stepSize+xMin) (float(y)*stepSize+yMin) maxIter)
    
let printMap (map : int[,]) =
    let charMap = [|'.'; ','; '\''; '-'; ':'; '/'; '('; '*'; '|'; '$'; '#'; '@'; '%'; '~'|]
    let charIndex rawIndex =
        rawIndex % (charMap |> Array.length)
    map
    |> Array2D.iteri (fun x y elem -> let char = charMap.[charIndex elem]
                                      if y = 0 then printfn ""
                                      printf "%c" char)
                                      
// Example
mandelMap -2.0 2.0 -2.0 2.0 0.1 1000 |> printMap

//    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
//    ,,,,,,,,,,,,,,''--::(::--'',,,,,,,,,,,,,
//    ,,,,,,,,,,,,''---:::(:::---'',,,,,,,,,,,
//    ,,,,,,,,,,''-----::(((::-----'',,,,,,,,,
//    ,,,,,,,,,''------:/(((/:------'',,,,,,,,
//    ,,,,,,,''''-----://*(*//:-----'''',,,,,,
//    ,,,,,,''''------//(%(%(//------'''',,,,,
//    ,,,,,''''------:**|.(.|**:------'''',,,,
//    ,,,,,''''------/*':(((:'*/------'''',,,,
//    ,,,,''''------:/*%(((((%*/:------'''',,,
//    ,,,'''''-----::/**(((((**/::-----''''',,
//    ,,,'''''----:://*@(((((@*//::----''''',,
//    ,,''''''---:::/(*$,(((,$*(/:::---'''''',
//    ,,'''''----::/(|@#(((((#@|(/::----''''',
//    ,''''''---:::*%%(((((((((%%*:::---''''''
//    ,''''''--:::/|(((((((((((((|/:::--''''''
//    ,''''''--::/(|%(((((((((((%|(/::--''''''
//    ,''''''--:/(|#(((((((((((((#|(/:--''''''
//    ,'''''''-/*$((((((((((((((((($*/-'''''''
//    ,'''''''-/$#(((((((((((((((((#$/-'''''''
//    ,'''''''-:(|:~(((((((((((((~:|(:-'''''''
//    ,'''''''-::/($((((((((((((($(/::-'''''''
//    ,''''''''-::/(%(((((((((((%(/::-''''''''
//    ,''''''''--:/(,(((((%(((((,(/:--''''''''
//    ,'''''''''--:/,*$,-|*|-,$*,/:--'''''''''
//    ,''''''''''--:://(/////(//::--''''''''''
//    ,''''''''''-----:::::::::-----''''''''''
//    ,,''''''''''-----------------'''''''''',
//    ,,''''''''''''-------------'''''''''''',
//    ,,,'''''''''''''---------''''''''''''',,
//    ,,,''''''''''''''''''''''''''''''''''',,
//    ,,,,''''''''''''''''''''''''''''''''',,,
//    ,,,,,''''''''''''''''''''''''''''''',,,,
//    ,,,,,''''''''''''''''''''''''''''''',,,,
//    ,,,,,,''''''''''''''''''''''''''''',,,,,
//    ,,,,,,,''''''''''''''''''''''''''',,,,,,
//    ,,,,,,,,,''''''''''''''''''''''',,,,,,,,
//    ,,,,,,,,,,''''''''''''''''''''',,,,,,,,,
//    ,,,,,,,,,,,,''''''''''''''''',,,,,,,,,,,
//    ,,,,,,,,,,,,,,''''''''''''',,,,,,,,,,,,,
module Mandelbrot
namespace System
val countIters : x0:float -> y0:float -> maxIter:int -> int

Full name: Mandelbrot.countIters
val x0 : float
val y0 : float
val maxIter : int
val iters : (float -> float -> int -> int)
val x : float
val y : float
val i : int
val x' : float
val y' : float
val i' : int
val mandelMap : xMin:float -> xMax:float -> yMin:float -> yMax:float -> stepSize:float -> maxIter:int -> int [,]

Full name: Mandelbrot.mandelMap
val xMin : float
val xMax : float
val yMin : float
val yMax : float
val stepSize : float
val xCount : int
val yCount : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
module Array2D

from Microsoft.FSharp.Collections
val init : length1:int -> length2:int -> initializer:(int -> int -> 'T) -> 'T [,]

Full name: Microsoft.FSharp.Collections.Array2D.init
val x : int
val y : int
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val printMap : map:int [,] -> unit

Full name: Mandelbrot.printMap
val map : int [,]
val charMap : char []
val charIndex : (int -> int)
val rawIndex : int
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
val iteri : action:(int -> int -> 'T -> unit) -> array:'T [,] -> unit

Full name: Microsoft.FSharp.Collections.Array2D.iteri
val elem : int
Multiple items
val char : char

--------------------
type char = Char

Full name: Microsoft.FSharp.Core.char
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Raw view Test code New version

More information

Link:http://fssnip.net/cI
Posted:11 years ago
Author:Kit Eason
Tags: learning f#; mandelbrot