2

I've been given an assignment for my class to make a program that draws Mandelbrot figures.
We have to basically get the program to draw a bitmap of the result.

Thing is, my CalcMBF function only outputs 2 as the Mandelbrot number.
I have absolutely no idea why that is. Can anyone help me out?

Here is my code:

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;
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61

1 Answers1

3

It computes beautifully. Only you made a mess of instance variables and arguments.

In CalcMBF, it should be:

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

where you had xCalc and yCalc, which are not local to CalcMBF. Furthermore, the imaginary part needs to be computed with the initial value of aWork. Interestingly, it still works with that bug, but it is a different fractal attractor.

The mandelbrot set has its interesting regions in the complex plane at -2<=cr<=1 and -1<=ci<=1, so a constant bailout at iteration 2 can indicate that you chose your c value outside or in some boring region like the middle of the lake.

If you need more speed, remove the square root and compare distanceXY <= 4 instead.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • Thanks for your answer! I replaced the code within the CalcMBF with what you provided, though it's still outputting only 2, even when within the "interesting part" of this fractal. Nevermind the imaginary component, it's not required for this assignment. – thebjornlegacy Dec 03 '18 at 22:19
  • I had -1.8 for xValue, 0.23 for yValue, 200 for maxValue, 0.2 for scaleValue and it gave me a lot of values other than 2. what values did you pick? not necessarily rewrite, you're close to a working solution. – Cee McSharpface Dec 03 '18 at 22:22
  • You're right. Is there something I'm missing? Shouldn't there also be variance in the mandelbrot number for x values between 0 and 2? – thebjornlegacy Dec 03 '18 at 22:30
  • between 0..2 on the real axis? [hardly](https://en.wikipedia.org/wiki/Mandelbrot_set#/media/File:Mandelset_hires.png). this is the lake, and the cardioid pike. not much going on... – Cee McSharpface Dec 03 '18 at 22:34
  • You're a god. Thanks for the help, I really appreciate it! – thebjornlegacy Dec 03 '18 at 22:36
  • 1
    you're welcome, good first question! I feel older than dirt now, just realized that more than 21 years have passed since I wrote my graduation thesis about fractal geometry, basically the reason why I became an IT professional. oh the nostalgia. – Cee McSharpface Dec 03 '18 at 22:45
  • You cant mention your graduation thesis without dropping a link so I can check it out :D – thebjornlegacy Dec 03 '18 at 22:50