Skip to content

Instantly share code, notes, and snippets.

@fcanas
Created May 13, 2015 01:38
Show Gist options
  • Save fcanas/d3ff6c49099c38ce36e5 to your computer and use it in GitHub Desktop.
Save fcanas/d3ff6c49099c38ce36e5 to your computer and use it in GitHub Desktop.
UIMandelbrot — Appropriate for Xcode Playgrounds
import UIKit
let white = UIColor.whiteColor().CGColor
let black = UIColor.blackColor().CGColor
let w :CGFloat = 105
let h :CGFloat = 60
let size = CGSizeMake(w, h)
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
let context = UIGraphicsGetCurrentContext()
let maxIteration = 15
var pal = [CGColor]()
for var c :CGFloat = 0; c < CGFloat( maxIteration + 1); c = c + 1 {
pal.append(UIColor(white: c / CGFloat(maxIteration), alpha: 1).CGColor)
}
for var Px :CGFloat = 0; Px < w; Px = Px + 1 {
for var Py :CGFloat = 0; Py < h; Py = Py + 1 {
let x0 = ((Px / w) * 3.5) - 2.5
let y0 = ((Py / h) * 2) - 1
var x :CGFloat = 0
var y :CGFloat = 0
var iteration = 0
while ( x*x + y*y < 2*2 && iteration < maxIteration )
{
let xtemp = x*x - y*y + x0
y = 2*x*y + y0
x = xtemp
iteration = iteration + 1
}
CGContextSetFillColorWithColor(context, pal[iteration])
CGContextFillRect(context, CGRectMake(Px, Py, 1, 1))
}
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment