//////////////////////////////////////////////////////////////////// // MandelbrotSet.cs // Copyright © 2006, 2008 Carl Johansen // // Mandelbrot set calculation class // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more // details (). //////////////////////////////////////////////////////////////////// namespace Mandelbrot { class MandelbrotSet { /// /// Performs the Mandelbrot iteration for the complex number a + bi. /// /// The real part of the complex number being tested. /// The imaginary part of the complex number being tested. /// The maximum number of iterations to be performed. /// If Z[n] > 2 then the function returns n (ie the number of iterations before the result /// was known to be unbounded. If Z[n] < 2 after maxIterations iterations, the function returns 0 /// (and c is assumed to be in the Mandelbrot set). /// Performs the Mandelbrot iteration Z[n+1] = Z[n]^2 + c for the complex number c = a + bi. public static int IsInSet(double a, double b, int maxIterations) { double currDistSq, aSq = 0.0, bSq = 0.0; double originalA = a, originalB = b; int numIterations = 0; while (numIterations++ < maxIterations) { aSq = a * a; bSq = b * b; currDistSq = aSq + bSq; if (currDistSq > 4) return numIterations; b = 2 * a * b + originalB; a = (aSq - bSq) + originalA; } return maxIterations; } } }