문제

I tried making this Mandelbrot fractal generator, but when I run this, I get an output like a circle. Not sure exactly why this happens. I think something may be wrong with my coloring, but even if so the shape is also incorrect.

public static Bitmap Generate(
        int width,
        int height,
        double realMin,
        double realMax,
        double imaginaryMin,
        double imaginaryMax,
        int maxIterations,
        int bound)
    {
        var bitmap = new FastBitmap(width, height);
        var planeWidth = Math.Abs(realMin) + Math.Abs(realMax); // Total width of the plane.
        var planeHeight = Math.Abs(imaginaryMin) + Math.Abs(imaginaryMax); // Total height of the plane.
        var realStep = planeWidth / width; // Amount to step by on the real axis per pixel.
        var imaginaryStep = planeHeight / height; // Amount to step by on the imaginary axis per pixel.
        var realScaling = width / planeWidth;
        var imaginaryScaling = height / planeHeight;
        var boundSquared = bound ^ 2;
        for (var real = realMin; real <= realMax; real += realStep) // Loop through the real axis.
        {
            for (var imaginary = imaginaryMin; imaginary <= imaginaryMax; imaginary += imaginaryStep) // Loop through the imaginary axis.
            {
                var z = Complex.Zero;
                var c = new Complex(real, imaginary);
                var iterations = 0;
                for (; iterations < maxIterations; iterations++)
                {
                    z = z * z + c;
                    if (z.Real * z.Real + z.Imaginary * z.Imaginary > boundSquared)
                    {
                        break;
                    }
                }
                if (iterations == maxIterations)
                {
                    bitmap.SetPixel(
                        (int)((real + Math.Abs(realMin)) * realScaling),
                        (int)((imaginary + Math.Abs(imaginaryMin)) * imaginaryScaling),
                        Color.Black);
                }
                else
                {
                    var nsmooth = iterations + 1 - Math.Log(Math.Log(Complex.Abs(z))) / Math.Log(2);
                    var color = MathHelper.HsvToRgb(0.95f + 10 * nsmooth, 0.6, 1.0);
                    bitmap.SetPixel(
                        (int)((real + Math.Abs(realMin)) * realScaling),
                        (int)((imaginary + Math.Abs(imaginaryMin)) * imaginaryScaling),
                        color);
                }
            }
        }
        return bitmap.Bitmap;
    }
도움이 되었습니까?

해결책

Here's one error:

var boundSquared = bound ^ 2;

This should be:

var boundSquared = bound * bound;

The ^ operator means xor.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top