Генерация изображения множества Мандельброта

Я пытался создать изображение множества Мандельброта, но что-то явно не так. Вот изображение, которое создает этот код: http://puu.sh/csUDd/bdfa6c1d98.png Пользуюсь Scala и обрабатываю. Техника окрашивания очень проста, но я не думаю, что это главная проблема, если посмотреть на форму изображения. Спасибо.

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) 

  }
}

Вот класс для комплексных чисел

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)
}

person user3723987    schedule 27.10.2014    source источник


Ответы (2)


Ваш метод plus не добавляется к another, я думаю, так и должно быть

def plus(another: Complex) = new Complex(real + another.real, imag + another.imag)
person hcs    schedule 27.10.2014

Хорошо, я понял это. Произошла ошибка в классе комплексных чисел.

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

Вот результат для всех, кому интересно http://puu.sh/csYbb/b2a0d882e1.png

person user3723987    schedule 27.10.2014