生成Mandelbrot集的图像

时间:2014-10-27 17:47:09

标签: scala processing fractals mandelbrot

我一直试图生成Mandelbrot集的图像,但显然是非常错误的。这是此代码生成的图像: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)
}

2 个答案:

答案 0 :(得分:4)

您的plus方法无法添加another,我认为应该是

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

答案 1 :(得分:0)

好的,我明白了。复数类中有一个错误。

def plus(另一个:复杂)= new Complex(真实+真实,想象+想象) ---&GT; def plus(另一个:复杂)= new Complex(this.real + another.real,this.imag + another.imag)

这是任何有兴趣的人的结果 http://puu.sh/csYbb/b2a0d882e1.png

相关问题