Link to home
Start Free TrialLog in
Avatar of Chris Mason
Chris Mason

asked on

Beginners help in Visual Studio/c# needed

I am recreating a project I did over 20 years ago to calculate the mandelbrot set.  The code I have written is very simple yet is behaving strangely in two ways:

1) Where I have put in the statusLabel.text = "Working" - it does not change the value on screen.  Even if I put a sleep command in after the change, it still remains at the original value.

2) At the moment, the program will draw a block of orange pixels (haven't figured out the mathematics yet) but only if the Console.WriteLine commands are there.  If I comment them out, not a single pixel is drawn.

---
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;
using System.Threading;

namespace Mandelbrot3
{
    public partial class Mandelbrot : Form
    {
        public Mandelbrot()
        {
            InitializeComponent();
            statusLabel.Text = "Ready";
        }    

        private void goButton_Click(object sender, EventArgs e)
        {
            int Width = 320, Height = 160, x, y, n;

            Bitmap b = new Bitmap(Width, Height);
            mandelbrotPanel.BackgroundImage = (Image)b;

            double MinRe = -2.0;
            double MaxRe = 1.0;
            double MinIM = -1.2;
            double MaxIM = MinIM + (MaxRe - MinRe)*Height / (this.Width);
            double ReFactor = (MaxRe - MinRe) / this.Width-1;
            double ImFactor = (MaxIM - MinIM) / this.Height-1;
            int maxiterations = 150;
            int maxlimit = 50;

            int iteration = 0;

            mandelbrotPanel.BackgroundImageLayout = ImageLayout.None;

            statusLabel.Text = "Working";

            for (y = 0; y < Height; ++y)
            {
                Console.Write("Y is {0}\n", y);
                Boolean isInside;

                double c_im = MaxIM - y * ImFactor;
                for (x = 0; x < Width; ++x)
                {
                    double c_re = MinRe + x * ReFactor;
                    double Z_re = c_re, Z_im = c_im;
                    isInside = true;
                    for (n = 1; n < maxiterations; n++)
                    {
                        iteration = n;
                        double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im;
                        if (Z_re2 + Z_im2 > maxlimit)
                        {
                            isInside = false;
                            break;
                        }
                        Z_im = (2 * Z_re * Z_im) + c_im;
                        Z_re = (Z_re2 - Z_im2) + c_re;
                    }
                    if (isInside)
                    {
                        b.SetPixel(x, y, Color.Black);
                        Console.WriteLine("Inside X is {0}", x);
                    }
                    else
                    {
                        b.SetPixel(x, y, Color.DarkOrange);
                        Console.WriteLine("Outside X is {0}", x);
                    }
                }
            }
            statusLabel.Text = "Done";
        }
    }
}

--- Thanks for the help in advance.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

1) Simple to explain.  The update will only happen when the thread is idle.  In the loop the thread is busy and a sleep stops the thread totally for that time (=busy).  Use Application.DoEvents();

2) Not certain why that is happening but you are drawing to a bitmap.  You could try mandelbrotPattern.Invalidate(); along with an Application.DoEvents();
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chris Mason
Chris Mason

ASKER

Solution works really well - lots of new concepts that I have to get used to.  Now I just need to sort out my mathematics so I don't get a completely orange rectangle appearing!!!

Thanks.
I hope you gave my suggestions a try - they would help you understand why your first attempt didn't work.
Not a problem Chris, glad to help.