Mandelbrot项目输出错误数据

时间:2018-12-05 12:43:55

标签: c# bitmap fractals mandelbrot

我一直在研究一个学校项目,创建Mandelbrot分形并使用位图显示它们。问题是,我不知道为什么它不起作用。 如果有人可以看一下我的代码并告诉我为什么它不输出位图(并且当它输出时,它肯定是 不是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 assignment_2_option_2._0
{
    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 xValueInput = 0;
    double yValueInput = 0;
    double maxValueInput = 0;
    double scaleValueInput = 0;
    //double pixelZoom = 0;
    double xCalcA = 0;
    double yCalcB = 0;
    double mandelbrotNumber;       
    PictureBox mbPicturebox = new PictureBox();
    Bitmap mBitmap = new Bitmap(400, 400);

    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 = ("");
        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 = ("");
        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 = ("");
        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 = ("");
        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;
        //Messing with the mbPanel
        mbPicturebox.Location = new Point(150, 250);
        mbPicturebox.Size = new Size(400, 400);

    }

    public void CalcMandelbrot(object sender, EventArgs e)
    {
        xValueInput = Convert.ToDouble(xInput.Text);
        yValueInput = Convert.ToDouble(yInput.Text);
        maxValueInput = Convert.ToDouble(maxInput.Text);
        scaleValueInput = Convert.ToDouble(scaleInput.Text);
        for (int yCounter = 0; yCounter < 399; yCounter++)
        {
            yCalcB = yValueInput + (yCounter * 0.01) + 2;
            for (int xCounter = 0; xCounter < 399; xCounter++)
            {
                xCalcA = xValueInput + (xCounter*0.01) - 2;
                mandelbrotNumber = calcNum(xCalcA, yCalcB, maxValueInput);
                xCalcA = 0;
                yCalcB = 0;
                Console.WriteLine(mandelbrotNumber);
                mBitmap.SetPixel(xCounter, yCounter, Color.FromArgb(255, (int)colorCalc((int)mandelbrotNumber), (int)colorCalc((int)mandelbrotNumber), (int)colorCalc((int)mandelbrotNumber)));
            }
        }
        mbPicturebox.Image = mBitmap;
        Controls.Add(mbPicturebox);
    }

    public double calcNum(double xCurrent, double yCurrent, double maxLoop)
    {
        double aWork = 0;
        double bWork = 0;
        double distanceXY = 0;

        for (int loopCounter = 0; loopCounter < maxLoop; loopCounter++)
        {
            if (distanceXY <= 2)
            {
                mandelbrotNumber = loopCounter;
                aWork = (aWork * aWork) - (bWork * bWork) + xCurrent;
                bWork = (2 * aWork * bWork) + yCurrent;
                distanceXY = Math.Sqrt(Math.Pow(aWork, 2) + Math.Pow(aWork, 2));
            }
            else
            {
                break;
            }
        }
        return mandelbrotNumber;
    }

    public int colorCalc(int mandelbrotNum)
    {
        int colorVal;
        colorVal = mandelbrotNum * 60;
        if (colorVal > 255)
        {
            return 255;
        }
        else
        {
            return colorVal;
        }
    }

    //Form load method skeleton
    private void Form1_Load(object sender, EventArgs e)
    {

    }
}
}

0 个答案:

没有答案
相关问题