Wikipedia

Search results

22 June 2014

Gingerbreadman fractal in Apple Swift

The "Gingerbreadman" fractal is a Pythagorean nature pattern. The following equations are used to generate the Gingerbreadman map for display, requiring constant (k):

x(n+1) = (k(a) * (1 + 2k(b))) - y(n) + abs(x(n) - (k(a) * k(b)))
y(n+1) = x(n)


Implementation in Apple Swift :
   
    var xPos = 0;
    var yPos = 0;
    
    // starting position
    var prevX = 100;
    var prevY = 115;
    
    // konstant
    var ka = 21;
    var kb = 3;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.gingerbread()
    }
    
    func gingerbread() {
        // points per starting position
        for (var j = 0; j < 1000; j++) {
            // here are the actual equations to generate next points
            xPos = (ka * (1 + 2 * kb)) - prevY + abs(prevX - (ka * kb))
            yPos = prevX;
                
            // random color generation
            let numR = arc4random_uniform(235) + 10;
            let numG = arc4random_uniform(235) + 10;
            let numB = arc4random_uniform(235) + 10;
            let color = UIColor(red: Float(numR)/255.0, green: Float(numG)/255.0, blue: Float(numB)/255.0, alpha: 1)
            let rect = CGRectMake(Float(xPos), Float(yPos), 1.0, 1.0);
                
            // set color 1 pixel box at position
            let view : UIView = UIView();
            view.frame = rect;
            view.backgroundColor = color;
            self.view.addSubview(view);
                
            // previous position is current position
            prevX = xPos;
            prevY = yPos;
                
        }
            
        // update current position
        xPos = ( (ka*(1+2*kb)) - prevY + (abs(prevX - (ka * kb)))  );
        yPos = prevX;
    }


At 10^4 iterations delivered the following result:



No comments:

Post a Comment