Created
February 26, 2019 14:05
-
-
Save matux/fc6419bcec31745712b49b1ea35bd43a to your computer and use it in GitHub Desktop.
Mandelbrot fractal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import func Darwin.sin | |
import func Darwin.cos | |
import Swift | |
public typealias ℤ = Int | |
public typealias ℝ = Double | |
public typealias ℂ = Complex | |
public func ρmap(_ ρ: ℤ) -> String { | |
switch ρ { | |
case 41... : return " " | |
case 7... : return "." | |
case 5... : return "+" | |
case 3... : return "*" | |
case _ : return "#" | |
} | |
} | |
extension BinaryFloatingPoint { | |
func signum() -> Self { | |
return Self(signOf: self, magnitudeOf: self == 0 || isNaN ? 0 : 1) | |
} | |
public var abs: Self { | |
return self * signum() | |
} | |
} | |
public struct Complex { | |
public var ℜ: ℝ | |
public var ℑ: ℝ | |
public var abs: ℝ { return ℜ * ℜ + ℑ * ℑ } | |
public init() { (ℜ, ℑ) = (0, 0) } | |
public init(r: ℝ, i: ℝ) { (ℜ, ℑ) = (r, i) } | |
public init(x: ℝ, θ: ℝ) { (ℜ, ℑ) = (x * cos(θ), x * sin(θ)) } | |
public static func + (lhs: ℂ, rhs: ℂ) -> ℂ { | |
return ℂ(r: lhs.ℜ + rhs.ℜ, i: lhs.ℑ + rhs.ℑ) | |
} | |
public static func * (lhs: ℂ, rhs: ℂ) -> ℂ { | |
return ℂ(r: lhs.ℜ * rhs.ℜ - lhs.ℑ * rhs.ℑ, | |
i: lhs.ℜ * rhs.ℑ + lhs.ℑ * rhs.ℜ) | |
} | |
} | |
import Swift | |
func print(fractal ƒc: (_ c: ℂ) -> ℤ, x: (ℝ, ℝ), y: (ℝ, ℝ)) { | |
var Δ: (ℝ, ℝ) = ((x.1 - x.0) / 40, (y.1 - y.0) / 80) | |
for x in stride(from: x.0, to: x.1, by: Δ.0) { | |
for y in stride(from: y.0, to: y.1, by: Δ.1) { | |
let ρ = ρmap(ƒc(ℂ(r: y, i: x))) | |
print(ρ, terminator: "") | |
} | |
print() | |
} | |
} | |
/// 𝑓𝑐(zₙ₊₁) = 𝑧²+𝑐, z₀=0 | |
func mandelbrot(_ c: ℂ) -> ℤ { | |
var (it, z) = (0, ℂ()) | |
while (it < 200 && z.abs < 4.0) { | |
z = z * z + c | |
it += 1 | |
} | |
return it | |
} | |
/// 𝑓𝑐(zₙ₊₁) = (▏𝑅𝑒(𝑧ₙ)│+𝑖│𝐼𝑚(𝑧ₙ)▕)²+𝑐, z₀=0 | |
func burningShip(_ c: ℂ) -> ℤ { | |
var (it, z) = (0, ℂ()) | |
while (it < 200 && z.abs < 4.0) { | |
let zₙ = ℂ(r: z.ℜ.abs, i: z.ℑ.abs) | |
z = zₙ * zₙ + c | |
it += 1 | |
} | |
return it | |
} | |
print(fractal: mandelbrot, x: (-1.35, 1.40), y: (-2.00, 1.05)) | |
print(" · mandelbrot ·\n") | |
print(fractal: burningShip, x: (-2.00, 1.20), y: (-2.10, 1.20)) | |
print(" · burning ship ·\n") | |
// ################################################################################# | |
// ##############################********************############################### | |
// ########################********************************######################### | |
// ####################***************************+++**********##################### | |
// #################****************************++...+++**********################## | |
// ##############*****************************++++......+************############### | |
// ############******************************++++.......+++************############# | |
// ##########******************************+++++.... ...++++************########### | |
// ########******************************+++++.... ..++++++**********########## | |
// #######****************************+++++....... .....++++++**********######## | |
// ######*************************+++++....... . .. ............++*********####### | |
// #####*********************+++++++++... .. . ... ..++*********###### | |
// ####******************++++++++++++..... ..++**********##### | |
// ###***************++++++++++++++... . ...+++**********#### | |
// ##**************+++................. ....+***********### | |
// ##***********+++++................. .++***********### | |
// #**********++++++..... ..... ..++***********### | |
// #*********++++++...... . ..++************## | |
// #*******+++++....... ..+++************## | |
// #++++............ ...+++************## | |
// #++++............ ...+++************## | |
// #******+++++........ ..+++************## | |
// #********++++++..... . ..++************## | |
// #**********++++++..... .... .++************## | |
// #************+++++................. ..++***********### | |
// ##*************++++................. . ..+***********### | |
// ###***************+.+++++++++++..... ....++**********#### | |
// ###******************+++++++++++++..... ...+++*********##### | |
// ####*********************++++++++++.... .. ..++*********###### | |
// #####*************************+++++........ . . . .......+*********####### | |
// #######***************************+++.......... .....+++++++*********######## | |
// ########*****************************++++++.... ...++++++**********######### | |
// ##########*****************************+++++..... ....++++***********########### | |
// ###########******************************+++++........+++***********############# | |
// #############******************************++++.. ...++***********############### | |
// ################****************************+++...+++***********################# | |
// ###################***************************+.+++**********#################### | |
// #######################**********************************######################## | |
// ############################************************############################# | |
// ################################################################################# | |
// · mandelbrot · | |
// ################################################################################# | |
// ################################################################################# | |
// ################################################################################# | |
// #####################################################################*****####### | |
// ################################################################*******+...+*#### | |
// #############################################################**********+...****## | |
// ###########################################################************. .+****## | |
// #########################################################***********++....+.****# | |
// ######################################################************+++......++***# | |
// ##############################*******************###************..... .....+++++# | |
// ########################*******+++*******************+ .+++++ . . ........+*# | |
// ####################**********+.. .+++*******+.+++**+. .....+.+**# | |
// #################**********++++...+...++ .. . . .+ ...+++++.***# | |
// ##############***********++..... . ... . ...++++++****## | |
// ############*************....... . . ...+++********## | |
// ##########***************. .. ...+++*********## | |
// #########***************++. .. . . ...+++*********### | |
// #######*****************. ... ...+++**********### | |
// ######*****************+. ...+++**********#### | |
// #####****************+++ . .....++***********#### | |
// #####**********++..... . ....+++***********#### | |
// ####*********+++.. . ....+++***********##### | |
// ####********++++. ....+++***********##### | |
// ###*******++++. ...++++***********##### | |
// ###**++*+..+... ...+++************##### | |
// ### ...+++************##### | |
// ###*********+++++++++......... ...... ..++************##### | |
// ####****************++++++.................... .++***********###### | |
// #####********************++++++++++++++++........ .+***********###### | |
// ########****************************+++++++++....... ++***********###### | |
// ###########*******************************++++++...... ..++**********####### | |
// ###############*******************************+++++.........++++*********######## | |
// ####################****************************++++++++++++++**********######### | |
// ##########################*************************+++++++++***********########## | |
// ################################**************************************########### | |
// ####################################********************************############# | |
// ########################################***************************############## | |
// ###########################################**********************################ | |
// #############################################*****************################### | |
// ################################################***********###################### | |
// · burning ship · |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment