2014-10-27 52 views
0

我一直在試圖生成Mandelbrot集合的圖像,但顯然是非常錯誤的。以下是此代碼生成的圖像:http://puu.sh/csUDd/bdfa6c1d98.png 我正在使用Scala並進行處理。着色技術非常簡單,但我不認爲這是看圖像形狀的主要問題。謝謝。生成Mandelbrot集合的圖像

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

回答

0

好類,我理解了它。複數類錯誤。 (plus + complex)= new Complex(real + real,imag + imag) ---> def plus(another:Complex)= new Complex(this.real + another.real,this。 IMAG + another.imag)

這裏有興趣的人,結果 http://puu.sh/csYbb/b2a0d882e1.png

4

plus方法不能與another補充,我覺得應該是

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