gpt4 book ai didi

java - Mandelbrot 集的视觉表示

转载 作者:行者123 更新时间:2023-11-30 01:49:11 31 4
gpt4 key购买 nike

我想使用 Java 生成 Mandelbrot 集的 PNG 照片,输出应该可以在 Google 图片搜索中轻松找到。

该集合定义为以下序列:

z_n+1 = z_n^2 + c

其中 cz 是复数,并且 z 的模数始终小于 2。

我首先定义一个复数类,其中还包含所需的基本复数运算。

public class ComplexNumber {
private double real;
private double imaginary;

public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

public ComplexNumber add(ComplexNumber z1, ComplexNumber z2) {
ComplexNumber sum = new ComplexNumber(0, 0);
sum.real = z1.real + z2.real;
sum.imaginary = z1.imaginary + z2.imaginary;
return sum;
}

public ComplexNumber square(ComplexNumber z) {
ComplexNumber squared = new ComplexNumber(0, 0);
squared.real = Math.pow(z.real, 2) - Math.pow(z.imaginary, 2);
squared.imaginary = 2 * z.real * z.imaginary;
return squared;
}

public double abs() {
double absolute = Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imaginary, 2));
return absolute;
}
}

然后,我定义了 Mandelbrot 类,它获取一些复数 c(基于像素),使用 mandelbrot 方法检查这些数字是否在 mandelbrot 集中,并将该方法的输出转换为颜色显示。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Mandelbrot {

public static int mandelbrot(ComplexNumber c, ComplexNumber z, int i, int n) {
if (i < n) {
if (c.abs() > 2.0) {
return i;
} else
return 0;
}
return mandelbrot(c, z.square(z).add(z, c), i, n);
}

// Create the Mandelbrot image, fill it and save it as PNG file.
public static void createMandelbrotImage(int tileSize, int maxRecurse) throws IOException {
int height = 2 * tileSize;
int width = 3 * tileSize;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

ComplexNumber z0 = new ComplexNumber(0, 0);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Construct a complex number from the pixel coordinates
float xPos = (x + 0.5f - 2 * tileSize) / tileSize;
float yPos = (y + 0.5f - tileSize) / tileSize;
ComplexNumber c = new ComplexNumber(xPos, yPos);

// Check the Mandelbrot condition for this complex number
int mb = mandelbrot(c, z0, 0, maxRecurse);

// Translate the result to number in a reasonable range and use it as color.
double mbl = mb > 0 ? Math.log(mb) / Math.log(maxRecurse) : 0;
image.setRGB(x, y, (int) (mbl * 255));
}
}

// Save the image as PNG
String OS = System.getProperty("os.name").toLowerCase(); // different for win and unix
String filePath = System.getProperty("user.dir") + (OS.indexOf("win") >= 0 ? "\\" : "/") + "mandelbrot.png";
System.out.println("Writing mandelbrot image to: " + filePath);
ImageIO.write(image, "png", new File(filePath));
}

public static void main(String[] args) throws IOException {

createMandelbrotImage(500, 2 ^ 24);

}
}

问题是这段代码总是输出黑色的空图像,我似乎没有发现错误。

最佳答案

看起来您的递归 mandelbrot 函数的终止条件不正确。

您希望 mandelbrot 函数返回,当

  • z 的绝对值超过 2 或
  • 已达到最大递归深度。

而且,你永远不会增加 i。

所以更新后的函数看起来像这样:

public static int mandelbrot(ComplexNumber c, ComplexNumber z, int i, int n) {
if (i >= n) {
// mandelbrot function does not diverge after n iterations.
// Returning -1 as a magic value to indicate that the point c is in the mandelbrot set.
// Values may already be outside of the mandelbrot set in the 0th iteration, so returning -1 makes more sense.
return -1;
} else if (z.abs() >= 2.0) {
// mandelbrot function is diverging after i iterations.
return i;
} else {
// recursively call mandelbrot function with an updated z and an incremented i.
return mandelbrot(c, z.squared().add(c), i + 1, n);
}
}

最后,如果您选择为 mandelbrot 集中的某个点返回 -1,则必须更新颜色计算以将这些点设置为黑色。

int mb = mandelbrot(c, z0, 0, maxRecurse);

if (mb == -1) {
image.setRGB(x, y, 0);
} else {
// set the color of your image as usual
}

关于java - Mandelbrot 集的视觉表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56724480/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com