Created
January 2, 2015 01:24
-
-
Save jonelf/965eed2ca03257c04335 to your computer and use it in GitHub Desktop.
Lorenz Attractor in Swift
This file contains 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
// Swift Playground example of the Lorenz Attractor | |
// Change platform to OS X when opening Playground. | |
// alt + cmd + enter to show Assistant editor and see resulting image. | |
import Cocoa | |
import XCPlayground | |
let width = 600.0, height = 500.0 | |
class CustomView: NSView { | |
override init(frame: NSRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder); | |
} | |
override func drawRect(dirtyRect: NSRect) { | |
lorentzAttractor() | |
} | |
func lorentzAttractor() { | |
var x = 0.1 , y = 0.0, z = 0.0, t = 0.0 | |
for i in 1...4242 { | |
let x1 = x + 0.01 * 10 * (y - x) | |
let y1 = y + 0.01 * (x * (28 - z) - y) | |
let z1 = z + 0.01 * (x * y - 2.66 * z) | |
x = x1 | |
y = y1 | |
z = z1 | |
let phys_x = width / 2 + 12 * x | |
let phys_y = 25 + 8 * z | |
NSRectFill(NSMakeRect(CGFloat(phys_x), CGFloat(phys_y), 1, 1)) | |
} | |
} | |
} | |
XCPShowView("Lorenz Attractor", CustomView(frame: | |
NSRect(x: 0, y: 0, width: width, height: height))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a Cocoa application https://github.com/jonelf/Lorenz-Attractor/tree/master