Почему этот расчет Мандельброта возвращает круг? - PullRequest
0 голосов
/ 30 сентября 2019

Я пытаюсь вычислить и отобразить множество Мандельброта с помощью следующего кода, но оно всегда возвращает круг. Я думаю, что понял расчет, но не могу найти ошибку. Любая помощь очень приветствуется. Я знаю, что есть один или два других похожих вопроса, но я думаю, что они не касаются моей ошибки.

Точка входа в мою программу: 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 голосов
/ 01 октября 2019

Вопрос дополнен рабочим ответом.

...