This is a Swift Playground that shows an image of the Lorenz Attractor. I have three questions. I will consider this question answered if only the first question gets answered.
- It's not that fast to begin with but after about 8000-9000 iterations it gets extremely slow. Any ideas why?
- Why is drawRect called twice?
- Are there any recommended ways for decent pixel pushing performance?
// 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)))