我一直在试图生成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)
}