-1

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?

I am a beginner so sorry if I posting this in the wrong place.

This is the class for my custom view:

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

seddouguim
  • 504
  • 3
  • 12
  • Insufficient code. What's a Point in your program? Provide enough information for us to get the program to compile, please. – matt Oct 15 '20 at 19:55

2 Answers2

1

Just to call your attention to it: it may be too much/overkill for you right now, but there is a Swift Numerics package that includes a Complex number type and a Real type, along with a bunch of functions that operate on them.

If you're going to be learning Swift it's good to understand strong Types and the central role types and type inference plays in the language.

If it seems like too much at this point, please do ignore it, but keep the swift.org website in mind.

https://swift.org/blog/numerics/

God of Biscuits
  • 1,312
  • 1
  • 13
  • 27
-1

I was able to solve the issue. It was an issue with the while loop. Here's the updated while-loop code for those that it might interest.

    func generateFractal() {


    for i in 0...Int(parentView.bounds.maxX) {

        for j in 0...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 (Z.modulus() <= 2 && currentIteration < maxIterations) {
                Z.iterate(With: C)
                currentIteration += 1
            }


            if (currentIteration < maxIterations) {

                points.append(Point(PointX: i, PointY: j))
            }
        }
    }
    dispatchPoints()
}

Cheers

seddouguim
  • 504
  • 3
  • 12
  • This makes no sense. There is no `generateFractal()` method in the original code, and you have not made clear where this method goes or how it would be called. – matt Oct 15 '20 at 19:51
  • Yeah sorry, this was 4 months ago. I don't have the code anymore. But looking at it, right now, I guess I just refactored the loops in the generateFractal function. It also seems I didn't include the Point class, but if I remember correctly, it was just inheriting from CGRect. The code I provided was definitely unclear/incomplete because it doesn't show where the generateFractal was called and doesn't even show the initialization of a Mandelbrot object. This was my first post on stackoverflow I think, and I must've missed some lines of code. Sorry – seddouguim Oct 19 '20 at 10:23