Created
March 23, 2015 14:03
-
-
Save jonelf/c5adac26bba63c376b0a to your computer and use it in GitHub Desktop.
Mandelbrot in Swift
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
for line in 0...32 { | |
println("".join((0...78).map { (col: Int) -> String in | |
var x = 0.0, y = x, i = 0 | |
do { | |
x = x * x - y * y + Double(col) / 20.0 - 2.3 | |
y = 2 * x * y + Double(line) / 10.0 - 1.5 | |
i++ | |
} while (x * x + y * y) < 4 && i < 78 | |
return "\(UnicodeScalar(0x1f35b + i))" | |
}) | |
) | |
} |
In Swift 2.2.1
for line in 0...32 {
print((0...78).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
repeat {
x = x * x - y * y + Double(col) / 20.0 - 2.3
y = 2 * x * y + Double(line) / 10.0 - 1.5
i = i + 1
} while (x * x + y * y) < 4 && i < 78
return "\(UnicodeScalar(0x1f35b + i))"
}.joinWithSeparator(""))
}
In Swift 3.0
for line in 0...32 {
print((0...65).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
repeat {
x = x * x - y * y + Double(col) / 20.0 - 2.1
y = 2 * x * y + Double(line) / 10.0 - 1.5
i = i + 1
} while (x * x + y * y) < 4 && i < 78
return "\(UnicodeScalar(0x1f35b + i))"
}.joined(separator: ""))
}
https://swiftlang.ng.bluemix.net/#/repl/5759332a83847baf29642974
for line in 0...42 {
print((0...65).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
repeat {
x = x * x - y * y + Double(col) / 20.0 - 2.1
y = 2 * x * y + Double(line) / 13.0 - 1.5
i = i + 1
} while (x * x + y * y) < 4 && i < 78
return String(UnicodeScalar(i + 0x1f35b)!)
}.joined(separator: ""))
}
This one works in Swift 5.9.2. Save and run with xcrun swift filename.swift
Open a big macOS terminal and run
echo 'for l in 0...42{print((0...65).map{(col: Int)->String in var x=0.0,y=x,i=0;repeat{x=x*x-y*y+Double(col)/20.0-2.1;y=2*x*y+Double(l)/13.0-1.5;i=i+1}while(x*x+y*y)<4&&i<78;return String(UnicodeScalar(i+0x1f35b)!)}.joined(separator:""))}'|swift
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/jonelf/status/580007606481215488
http://swiftstub.com/198900712/