Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created March 17, 2021 18:35
Show Gist options
  • Save GuyInGrey/d7205892ca415b97cbcc6c6a02184585 to your computer and use it in GitHub Desktop.
Save GuyInGrey/d7205892ca415b97cbcc6c6a02184585 to your computer and use it in GitHub Desktop.
using System;
using System.Numerics;
using SixLabors.ImageSharp;
using SixLaborsRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace MandelbrotCPUTest
{
class Program
{
static void Main()
{
Console.WriteLine("Starting...");
var i = Render(new[] { -2.25f, -1.5f, 0.75f, 1.5f }, 2f, 500, 500, 50);
i.Save("test.png");
Console.WriteLine("Done!");
Console.Read();
}
public static Image<SixLaborsRgba32> Render(float[] viewport /*size 4*/, float power, int width, int height, int maxIterations)
{
var image = new Image<SixLaborsRgba32>(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var c = new Complex(
Map(x, 0, width, viewport[0], viewport[2]),
Map(y, 0, height, viewport[1], viewport[3]));
var z = Complex.Zero;
var i = 0;
while (Complex.Abs(z) <= 2 && i < maxIterations)
{
z = Complex.Pow(z, new Complex(power, 0)) + c;
i++;
}
float final;
if (i == maxIterations) { final = maxIterations; }
else
{
final = (float)(i + 1 - Math.Log(Math.Log(Complex.Abs(z))) / Math.Log(2));
}
var color = (byte)((final / maxIterations) * 255);
image[x, y] = Color.FromRgba(color, color, color, 255);
}
}
return image;
}
public static float Lerp(float a, float b, float t) =>
a * (1 - t) + b * t;
public static float Map(float val, float a1, float b1, float a2, float b2) =>
Lerp(a2, b2, (val - a1) / (b1 - a1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment