0

I've been trying to generate an image of the Mandelbrot set but something is obviously very wrong. Here's the image this code produces: http://puu.sh/csUDd/bdfa6c1d98.png I'm using Scala and processing. The coloring technique is very simple but i don't think it's the main problem by looking at the image's shape. Thank you.

for (i <- 0 to width){ // 0 to 700
  for (j <- 0 to height){ // 0 to 400

    val x = i/200.toDouble - 2.5 // scaled to -2.5, 1
    val y = j/200.toDouble - 1 // scaled to -1, 1
    val c = new Complex(x, y)
    val iterMax = 1000

    var z = c
    var iterations = 0
    var inSet = false

    while (z.abs < 2 && iterations < iterMax) {         
      z = z.squared.plus(c)
      iterations += 1
      if (iterations == iterMax) {
        inSet = true
      }         
    }     

    // stroke() defines the current rgb color.
    // If the point is in the set, it is coloured black.
    // If not, the point is coloured as such: (iterations^5 mod 255, iterations^7 mod 255, iterations^11 mod 255) 
    // I use 5, 7 and 11 for no specific reason. Using iterations alone results in a black picture.

    if (inSet) stroke(0, 0, 0)
    else stroke(pow(iterations, 5).toInt % 255, pow(iterations, 7).toInt % 255, pow(iterations, 11).toInt % 255) 

    // Finally, draw the point.
    point(i, j) 

  }
}

Here's the class for complex numbers

class Complex(val real: Double, val imag: Double) {
  def squared = new Complex(real*real - imag*imag, 2*real*imag)
  def abs = sqrt(real*real + imag*imag)
  def plus(another: Complex) = new Complex(real + real, imag + imag)
}

2 Answers2

4

Your plus method doesn't add with another, I think it should be

def plus(another: Complex) = new Complex(real + another.real, imag + another.imag)
hcs
  • 1,514
  • 9
  • 14
0

Ok, i figured it out. There was a mistake in the complex number class.

def plus(another: Complex) = new Complex(real + real, imag + imag) ---> def plus(another: Complex) = new Complex(this.real + another.real, this.imag + another.imag)

Here's the result for anyone interested http://puu.sh/csYbb/b2a0d882e1.png