【问题标题】:Mandelbrot Fractal not working曼德布罗分形不工作
【发布时间】:2014-03-20 00:26:12
【问题描述】:

我尝试制作这个 Mandelbrot 分形生成器,但是当我运行它时,我得到一个像圆圈一样的输出。 不知道为什么会发生这种情况。我认为我的着色可能有问题,但即使是这样,形状也是不正确的。

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;
    }

【问题讨论】:

    标签: c# .net fractals


    【解决方案1】:

    这里有一个错误:

    var boundSquared = bound ^ 2;
    

    这应该是:

    var boundSquared = bound * bound;
    

    ^ 运算符表示xor

    【讨论】:

    • 哇。我不敢相信像这样一个简单的小错误会毁掉整个事情。这解决了它。虽然我仍然有着色问题。
    • 着色是一个不同的问题(有自己的主题)。但是当你能画出黑白曼德布洛特时,你就少了一个问题:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2017-12-03
    相关资源
    最近更新 更多