为什么此Mandelbrot计算返回圆?

时间:2019-09-30 10:47:29

标签: c# .net asp.net-core bitmap mandelbrot

我尝试使用以下代码计算并显示Mandelbrot集,但它始终返回一个圆。我想我了解计算方法,但是找不到错误。任何帮助都非常欢迎。我知道还有一个或两个类似的问题,但我认为它们不能解决我的错误。

程序的入口点是calcMandel()

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;

namespace Mandelbrot
{
public class MandelbrotCreator
{
    private double accuracy;
    private int width;
    private int height;

    public MandelbrotCreator(double accuracy)
    {
        this.accuracy = accuracy;

        this.width = Convert.ToInt32(3 / this.accuracy);
        this.height = Convert.ToInt32(2 / this.accuracy);
    }

    public void calcMandel()
    {            
        double x = -2;
        double y = 1;

        int[,] values = new int[height,width];

        // Go through all values on the plane
        for (int i = 0; i < this.height; i++)
        {
            for (int j = 0; j < this.width; j++)
            {
                double zn = CalcZn(5, x, y);

                values[i, j] = zn < 2 ? 0 : -16843010;  //Int Color Code for Black

                x += this.accuracy;
            }

            x = -2;
            y -= this.accuracy;
        }

        SaveBitmap(values);
    }

    private double CalcZn (int iterations, double a, double b)
    {
        if (iterations == 0)
        {
            return 0;
        }

        // Zn = Z(n-1)^2 + c
        return Math.Pow(CalcZn(iterations - 1, a, b), 2) + CalcC(a, b);
    }

    private double CalcC (double a, double b)
    {
        // c = Sqrt (a^2 + b^2)
        return Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
    }

    private void SaveBitmap(int[,] integers)
    {
        int stride = this.width * 4;

        // Copy into bitmap
        Bitmap bitmap;

        unsafe
        {
            fixed (int* intPtr = &integers[0,0])
            {
                bitmap = new Bitmap(this.width, this.height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
                bitmap.Save(@".\image.bmp");
            }
        }
    }
}

}

The circle

编辑:

重写了我的代码,这对于任何有兴趣的人现在都可以完美地工作。我还添加了颜色编码以增加美感。

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Mandelbrot
{
public class MandelbrotCreator
{
    private double accuracy;
    private int width;
    private int height;
    private int[] colorCodes;

    public MandelbrotCreator(double accuracy)
    {
        this.accuracy = accuracy;

        this.width = Convert.ToInt32(3 / this.accuracy);
        this.height = Convert.ToInt32(2 / this.accuracy);

        // https://www.shodor.org/stella2java/rgbint.html
        this.colorCodes = new int[] { 16711680, 16716032, 16720384, 16724736, 16729088, 16733440, 16737792, 16742144, 16746496, 16750848, 16755200, 16759552, 16763904, 16768256, 16772608, 16776960, 16777060, 16777160, 16777215, 0 };
    }

    public void calcMandel()
    {            
        double x = -2;
        double y = 1;

        int[,] values = new int[height,width];

        // Go through all values on the plane
        for (int i = 0; i < this.height; i++)
        {
            for (int j = 0; j < this.width; j++)
            {
                int neededIterations = checkForBound(19, x, y);

                values[i,j] = colorCodes[neededIterations - 1];

                x += this.accuracy;
            }

            x = -2;
            y -= this.accuracy;
        }

        SaveBitmap(values);
    }

    // http://warp.povusers.org/Mandelbrot/
    private int checkForBound (int maxIterations, double c_re, double c_im)
    {
        double z_re = c_re;
        double z_im = c_im;

        int n;

        for (n = 1; n <= maxIterations; ++n)
        {
            double z_re2 = z_re * z_re;
            double z_im2 = z_im * z_im;

            if (z_re2 + z_im2 > 4)
            {
                break;
            }

            // z(n) = z^2 + c
            z_im = 2 * z_re * z_im + c_im;
            z_re = z_re2 - z_im2 + c_re;
        }

        return n;
    }

    private void SaveBitmap(int[,] integers)
    {
        int stride = this.width * 4;

        // Copy into bitmap
        Bitmap bitmap;

        unsafe
        {
            fixed (int* intPtr = &integers[0,0])
            {
                bitmap = new Bitmap(this.width, this.height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
                bitmap.Save(@".\image.bmp");
            }
        }
    }
}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

问题已得到有效答案。