Skip to content

Instantly share code, notes, and snippets.

@matux
Created February 26, 2019 14:05
Show Gist options
  • Save matux/fc6419bcec31745712b49b1ea35bd43a to your computer and use it in GitHub Desktop.
Save matux/fc6419bcec31745712b49b1ea35bd43a to your computer and use it in GitHub Desktop.
Mandelbrot fractal
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