c# - Mandelbrot 程序未输出正确的数据

标签 c# mandelbrot

我的类(class)接到一项作业,要制作一个绘制曼德尔布罗图的程序。
我们基本上必须让程序绘制结果的位图。

事实是,我的 CalcMBF 函数仅输出 2 作为 Mandelbrot 数。
我完全不知道为什么会这样。谁能帮帮我吗?

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mandelbrot_Figure
{
    class PreMainClass
    {
        static void main (String[] args)
        {
            Form1 screen;
            screen = new Form1();
            Application.Run(screen);
        }
    }

    public partial class Form1 : Form
    {
        Label xInputLabel = new Label();
        Label yInputLabel = new Label();
        Label maxInputLabel = new Label();
        Label scaleInputLabel = new Label();
        TextBox xInput = new TextBox();
        TextBox yInput = new TextBox();
        TextBox maxInput = new TextBox();
        TextBox scaleInput = new TextBox();
        Button okButton = new Button();
        double xValue = 0;
        double yValue = 0;
        double maxValue = 0;
        double scaleValue = 0;
        double pixVal = 0;
        double xCalc = 0;
        double yCalc = 0;
        double[,,] mArray = new double[400, 400, 1];

        public Form1()
        {
            InitializeComponent();
            BackColor = Color.FromArgb(255, 255, 255);
            Text = "Mandelbrot Figure";
            Size = new System.Drawing.Size(700, 950);
                //Messing with the xInput Box and Label
            xInput.Location = new Point(50, 50);
            xInput.Size = new Size(210, 50);
            xInput.Text = ("Please input desired X coordinate.");
            Controls.Add(xInput);
            xInputLabel.Location = new Point(46, 20);
            xInputLabel.Size = new Size(100, 40);
            xInputLabel.Text = "Middle X:";
            Controls.Add(xInputLabel);
                //Messing with the yInput Box and Label
            yInput.Location = new Point(320, 50);
            yInput.Size = new Size(210, 50);
            yInput.Text = ("Please input desired Y coordinate.");
            Controls.Add(yInput);
            yInputLabel.Location = new Point(316, 20);
            yInputLabel.Size = new Size(100, 40);
            yInputLabel.Text = "Middle Y:";
            Controls.Add(yInputLabel);
                //Messing with the maxInput Box and Label
            maxInput.Location = new Point(50, 126);
            maxInput.Size = new Size(210, 100);
            maxInput.Text = ("Please input desired max value.");
            Controls.Add(maxInput);
            maxInputLabel.Location = new Point(46, 100);
            maxInputLabel.Size = new Size(50, 40);
            maxInputLabel.Text = "Max:";
            Controls.Add(maxInputLabel);
                //Messing with the scaleInput Box and Label
            scaleInput.Location = new Point(320, 126);
            scaleInput.Size = new Size(210, 100);
            scaleInput.Text = ("Please input desired scale value.");
            Controls.Add(scaleInput);
            scaleInputLabel.Location = new Point(316, 100);
            scaleInputLabel.Size = new Size(80, 40);
            scaleInputLabel.Text = "Scale:";
            Controls.Add(scaleInputLabel);
                //Messing with the okButton
            okButton.Location = new Point(560, 49);
            okButton.Size = new Size(100, 100);
            okButton.Text = ("Start");
            Controls.Add(okButton);
            okButton.Click += CalcMandelbrot;
        }

        //Grabs data and drops it into an array
        public void CalcMandelbrot(object sender, EventArgs e)
        {
            xValue = Convert.ToDouble(xInput.Text);
            yValue = Convert.ToDouble(yInput.Text);
            maxValue = Convert.ToDouble(maxInput.Text);
            scaleValue = Convert.ToDouble(scaleInput.Text);
            pixVal = scaleValue * 0.01;
            for (double yCounter = 0; yCounter < 400; yCounter++)
            {
                yCalc = yValue + (200 * pixVal) + (yCounter * pixVal);
                for (double xCounter = 0; xCounter < 400; xCounter++)
                {
                    xCalc = xValue - (200 * pixVal) + (xCounter * pixVal);
                    mArray[Convert.ToInt32(xCounter), Convert.ToInt32(yCounter), 0] = CalcMBF(xCalc, yCalc, maxValue);
                    Console.WriteLine(xCounter + " " + yCounter + " " + " " + xCalc + " " + yCalc + " " + CalcMBF(xCalc, yCalc, maxValue));
                }
            }
        }

        public double CalcMBF(double aOut, double bOut, double maxValue)
        {
            double aWork = aOut;
            double bWork = bOut;
            double maxWork = maxValue;
            double distanceXY = 0;
            int mandelbrotNum = 0;
            for (int loopCounter = 1; loopCounter < maxWork; loopCounter++)
            {
                if (distanceXY <= 2)
                {
                    distanceXY = Math.Sqrt(Math.Pow((aWork), 2) + Math.Pow((bWork), 2));
                    mandelbrotNum = loopCounter;
                    aWork = (aWork * aWork) - (bWork * bWork) + xCalc;
                    bWork = (2 * aWork * bWork) + yCalc;
                }
                else
                {
                    aWork = 0;
                    bWork = 0;
                    break;
                }
            }
            return mandelbrotNum;
        }
    }
}

最佳答案

它的计算效果非常好。只是你把实例变量和参数弄乱了。

CalcMBF ,应该是:

var originala = aWork;
aWork = (aWork * aWork) - (bWork * bWork) + aOut;
bWork = (2 * originala * bWork) + bOut;

你在哪里xCalcyCalc ,这不是CalcMBF本地的。此外,虚部需要用aWork的初始值来计算。有趣的是,它仍然适用于该错误,但它是 different fractal吸引子。

mandelbrot 集在复平面中的 -2<=cr<=1 和 -1<=ci<=1 处有其有趣的区域,因此迭代 2 处的恒定救助可以表明您选择了外部或外部的 c 值在一些无聊的区域,比如湖中央。

如果您需要更快的速度,请删除平方根并比较 distanceXY <= 4相反。

关于c# - Mandelbrot 程序未输出正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53602027/

相关文章:

c# - 为什么 .ToString() 方法会破坏我的代码?

c++ - 写入数组时,最后一个线程比第一个线程执行得慢

java - Mandelbrot刷新速度很慢,有什么办法可以让它更快吗?

c# - OData、Web Api 2 和深度嵌套对象

c - 缩放 MandelBrot 集

java - 如何让多个线程绘制到 AWT 组件上?

c# - 一个类是否可能只继承一些(不是全部)基类成员?

c# - 假人的表达树?

c# - 不需要服务器的 Visual Studio 的源代码管理?