Mandelbrot Set

Hi, I am trying to generate a Mandelbrot fractal using the draw function to draw CGRects of points included in set. The code doesn't work and I get a circle instead. Any help?

This is the class for my custom view:

import UIKit

class View: UIView {

var pointsToDraw = [Point]()


override func draw(_ rect: CGRect) {

    
    for point in pointsToDraw {
        
        point.color.setFill()
        UIRectFill(point.coordinates)
    }
    
}

}

this is my complex number struct:

struct complexNumber {

var real:Double = 0.0
var imaginary:Double = 0.0


mutating func add(This complexNumberToAdd:complexNumber){
    
    real += complexNumberToAdd.real
    imaginary += complexNumberToAdd.imaginary
    
}

mutating func squared() {
    
    let tempReal = real
    let tempImaginary = imaginary
    
    real = (tempReal * tempReal) - (tempImaginary * tempImaginary)
    imaginary = 2 * tempReal * tempImaginary
    
}

func modulus () -> Double {
    return sqrt(real * real + imaginary * imaginary)
}


mutating func iterate (With C:complexNumber) {
    squared()
    add(This: C)
}

}

and finally, this is my mandelbrot class:

class Mandelbrot {

var points = [Point]()
var xMin:Double, xMax:Double, yMin:Double, yMax:Double
var parentView:View
var Z:complexNumber, C:complexNumber
var maxIterations:Int
var currentIteration:Int

var xPixelValue:Double {
    return (xMax - xMin) / Double(parentView.bounds.maxX)
}

var yPixelValue:Double {
    return (yMax - yMin) / Double(parentView.bounds.maxY)
}

init (InView view:View) {
    
    xMin = -2.0
    xMax = 2.5
    yMin = -2
    yMax = 2.5
    Z = complexNumber()
    C = complexNumber()
    maxIterations = 50
    currentIteration = 0
    
    parentView = view
    
    for i in 1...Int(parentView.bounds.maxX) {
        
        for j in 1...Int(parentView.bounds.maxY) {
            
            Z.real = 0
            Z.imaginary = 0
            currentIteration = 0
            
            C.real = xMin + (Double(i) * xPixelValue)
            C.imaginary = yMax - (Double(j) * yPixelValue)
            
            while (currentIteration < maxIterations)
            {
                Z.iterate(With: C)
                currentIteration += 1
                
                if Z.modulus() > 2 {
                    break
                }
                    
                else
                {
                    points.append(Point(PointX: i, PointY: j))
                }
        
            }
        }
    }
    
    dispatchPoints()
   
}

func dispatchPoints() {
    
    parentView.pointsToDraw.removeAll()
    
    for point in points {
        parentView.pointsToDraw.append(point)
    }
}

}

Thank you,
Mehdi

I think this is more suited for stack overflow (well, it's a debug problem, so idk). Anyhow, I'd suggest that you try other, simpler shapes, like a circle (ha!), or a ring, just to make sure that your drawing mechanism works, before jumping into Mandelbrot.

Hi, sorry if I posted this on the wrong forum.

The drawing mechanism does work, it just seems like I am not getting the correct points to plot.

All the more reason to go to stack overflow. This forum is mainly about Swift usage, but this one seems to be more related to logic problem (not that we definitely won't help, but you'd get better help there).

Terms of Service

Privacy Policy

Cookie Policy