Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created January 2, 2015 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/965eed2ca03257c04335 to your computer and use it in GitHub Desktop.
Save jonelf/965eed2ca03257c04335 to your computer and use it in GitHub Desktop.
Lorenz Attractor in Swift
// 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)))
@jonelf
Copy link
Author

jonelf commented Jan 2, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment