Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Mandelbrot计算返回一个圆?_C#_.net_Asp.net Core_Bitmap_Mandelbrot - Fatal编程技术网

C# 为什么Mandelbrot计算返回一个圆?

C# 为什么Mandelbrot计算返回一个圆?,c#,.net,asp.net-core,bitmap,mandelbrot,C#,.net,Asp.net Core,Bitmap,Mandelbrot,我尝试用以下代码计算并显示Mandelbrot集,但它总是返回一个圆。我想我理解这个计算,但我找不到错误。欢迎任何帮助。我知道还有一两个类似的问题,但我认为它们没有解决我的错误 我的程序的入口点是calcMandel() 使用系统; 使用系统诊断; 使用系统图; 使用系统、绘图、成像; 名称空间Mandelbrot { 公共类MandelbrotCreator { 私人双精度; 私有整数宽度; 私人内部高度; 公共MandelbrotCreator(双精度) { 准确度=准确度; this.wi

我尝试用以下代码计算并显示Mandelbrot集,但它总是返回一个圆。我想我理解这个计算,但我找不到错误。欢迎任何帮助。我知道还有一两个类似的问题,但我认为它们没有解决我的错误

我的程序的入口点是
calcMandel()

使用系统;
使用系统诊断;
使用系统图;
使用系统、绘图、成像;
名称空间Mandelbrot
{
公共类MandelbrotCreator
{
私人双精度;
私有整数宽度;
私人内部高度;
公共MandelbrotCreator(双精度)
{
准确度=准确度;
this.width=转换为32(3/此精度);
this.height=转换为32(2/此精度);
}
公共无效calcMandel()
{            
双x=-2;
双y=1;
int[,]值=新的int[高度,宽度];
//检查平面上的所有值
for(int i=0;i

重写我的代码,这对任何感兴趣的人来说都非常有效。我还添加了一个额外的美丽的颜色编码

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");
                }
            }
        }
    }
}
使用系统;
使用系统图;
使用系统、绘图、成像;
名称空间Mandelbrot
{
公共类MandelbrotCreator
{
私人双精度;
私有整数宽度;
私人内部高度;
私有int[]色码;
公共MandelbrotCreator(双精度)
{
准确度=准确度;
this.width=转换为32(3/此精度);
this.height=转换为32(2/此精度);
// https://www.shodor.org/stella2java/rgbint.html
this.colorCodes=newint[]{16711680、16716032、16720384、16724736、16729088、16733440、16737792、16742144、16746496、16750848、16755200、16759552、16763904、16768256、16772608、16776960、16777060、16777160、16777215、0};
}
公共无效calcMandel()
{            
双x=-2;
双y=1;
int[,]值=新的int[高度,宽度];
//检查平面上的所有值
for(int i=0;i

不,您不理解计算。你所做的不是曼德布罗特方程。递归操作很快就会给你带来麻烦。你需要不断迭代,直到复杂的modukus出现为止。我建议你使用(或者更好地学习编写)一个复数抽象类。所以你可以把实际的微积分从算法中分离出来。@Fildor谢谢你,这对我帮助很大,经过进一步的研究,我终于把它弄对了。我用现在可用的代码更新了问题。我已回滚,将您问题的答案放入问题本身,并将答案移动到您发布的内容中。该问题已更新为其他信息-答案。我把问题的答案从问题的开头移到了这个答案上。
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");
                }
            }
        }
    }
}