/// <summary> /// Divides this ComplexNum by a given one /// </summary> /// <param name="n">The ComplexNum to divide with</param> public void Divide(ComplexNum n) { double a = real; double b = complex; double c = n.GetReal(); double d = n.GetComplex(); //Trust me on this one real = (a * c + b * d) / (c * c + d * d); complex = (b - a * d) / (c * c + d * d); }
/// <summary> /// Finds the product of a ComplexNum and this one /// </summary> /// <param name="n">The ComplexNum to be multiplied</param> public void Multiply(ComplexNum n) { double a = real; double b = complex; double c = n.GetReal(); double d = n.GetComplex(); //Using FOIL to determine new values //(a + bi)(c + di) real = (a * c) - (b * d); complex = (a * d) + (b * c); }
/// <summary> /// Recursive check if a complex number is within the Mandelbrot set /// </summary> /// <param name="z"></param> /// <param name="c"></param> /// <param name="recursions"></param> /// <param name="maxRecursions"></param> /// <returns>TRUE if "c" is within Mandelbrot Set, FALSE otherwise</returns> private bool RecursiveCheck(ComplexNum z, ComplexNum c, int recursions, int maxRecursions) { if (Math.Sqrt(z.GetReal() * z.GetReal() + z.GetComplex() * z.GetComplex()) > MAX_DIST) { //Number blew up return(false); } else if (recursions > maxRecursions) { //Number probably does not blow up return(true); } else { //Run it again z.Squared(); z.Add(c); return(RecursiveCheck(z, c, recursions + 1, maxRecursions)); } }
/// <summary> /// Iterative check if a complex number is within the Mandelbrot set /// </summary> /// <param name="z"></param> /// <param name="c"></param> /// <param name="maxIterations"></param> /// <returns>TRUE if "c" is within Mandelbrot Set, FALSE otherwise</returns> private int IterativeCheck(ComplexNum z, ComplexNum c, int maxIterations) { int iterations = 0; while (true) { if (z.Magnitude() > MAX_DIST) { //Number blew up return(iterations); } else if (iterations >= maxIterations) { //Number probably does not blow up return(maxIterations); } //Run it through the function again z.Multiply(z); z.Add(c); iterations++; } }
/// <summary> /// Generates set upon clicking button /// </summary> private void generateBtn_Click(object sender, EventArgs e) { //Objects used to draw the set Graphics objGraphics = this.CreateGraphics(); Brush objBrush = new SolidBrush(System.Drawing.Color.Blue); //Setting size of array based on size of window domainSize = Math.Min(this.Size.Height, this.Size.Width); //Parsing input for values, if it is valid double.TryParse(topTxt.Text, out renderTop); double.TryParse(leftTxt.Text, out renderLeft); double.TryParse(sizeTxt.Text, out renderSideLength); int.TryParse(bailOutTxt.Text, out bailOut); //Showing values topTxt.Text = renderTop.ToString(); leftTxt.Text = renderLeft.ToString(); sizeTxt.Text = renderSideLength.ToString(); bailOutTxt.Text = bailOut.ToString(); //Complex numbers ComplexNum z = new ComplexNum(0, 0); ComplexNum c = new ComplexNum(0, 0); //Going through each point in the pointSet for (int b = 0; b < domainSize; b++) { for (int a = 0; a < domainSize; a++) { //Setting complex value c.SetReal(renderSideLength * ((double)a / (double)domainSize) + renderLeft); c.SetComplex(renderSideLength * ((double)b / (double)domainSize) + renderTop); z.SetReal(0); z.SetComplex(0); //Setting blow-up value for point pointValue = IterativeCheck(z, c, bailOut); if (pointValue == bailOut) { objBrush = new SolidBrush(System.Drawing.Color.Black); } else { //Setting brush shade float s = (float)pointValue / (float)bailOut; int t = (int)(Math.Log10(10 * s + 1) * 255); if (t > 255) { t = 255; } int red = t; int green = t; int blue = 255 - t; objBrush = new SolidBrush(System.Drawing.Color.FromArgb(red, green, blue)); } //Drawing point objGraphics.FillRectangle(objBrush, a, b, 1, 1); } //Ensures memory does not run out GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// Subtracts a ComplexNum value from this one /// </summary> /// <param name="n">The ComplexNum to be subtracted</param> public void Subtract(ComplexNum n) { real -= n.GetReal(); complex -= n.GetComplex(); }
/// <summary> /// Adds a ComplexNum value to this one /// </summary> /// <param name="n">The ComplexNum to be added</param> public void Add(ComplexNum n) { real += n.GetReal(); complex += n.GetComplex(); }