Setup

1
2
#r "nuget:SkiaSharp" 
using SkiaSharp;

Mandelbrot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Create an image and fill it blue
int width = 1920;
int height = 1080;

int step =50;
SKBitmap bmp = new(width, height);
SKCanvas canvas = new(bmp);

canvas.Clear(SKColor.Parse("#fff"));

Random rand = new();
SKPaint paint = new() 
{ 
    Color = SKColors.White.WithAlpha(100), 
    IsAntialias = true ,
    StrokeWidth = 2,
    ColorF = SKColor.Parse("#003366")
};
 
double zoom = 1;
double moveX = -0.5;
double moveY = 0;
int maxIterations = 50;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        double zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX;
        double zy = (y - height / 2) / (0.5 * zoom * height) + moveY;
        double cx = zx;
        double cy = zy;
        int iteration = 0;
        double tmp;

        while ((zx * zx + zy * zy < 4) && (iteration < maxIterations))
        {
            tmp = zx * zx - zy * zy + cx;
            zy = 2.0 * zx * zy + cy;
            zx = tmp;
            iteration++;
        }

        if (iteration == maxIterations)
        {
            canvas.DrawPoint(x, y, SKColors.Black);
        }
        else
        {
            // Colorize based on the number of iterations
            var color = SKColor.FromHsv(iteration % 256, 255, 255);
            canvas.DrawPoint(x, y, color);
        }
    }
}
SKFileWStream fs = new("mandlebrot.jpg");
bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
bmp.Display();

images