-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelbrot.cs
58 lines (52 loc) · 1.81 KB
/
Mandelbrot.cs
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
using SixLabors.ImageSharp;
using System;
using System.Numerics;
namespace FractalGenerator;
public class Mandelbrot : Fractal
{
public Mandelbrot() : base()
{
}
/// <summary>
/// Calculates the fractal value for each pixel using:
/// Zn = Z(n-1)^2 + C
/// As long as Abs(Z) < MaxRadius and Iterations < MaxIterations
/// </summary>
/// <param name="x">The horizontal pixel.</param>
/// <param name="y">The vertical pixel.</param>
/// <param name="h">The horizontal point in the complex plane.</param>
/// <param name="v">The vertical point in the complex plane.</param>
protected override void Calculate(int x, int y, double h, double v)
{
var z = new Complex(0.0, 0.0);
var c = new Complex(h, v);
ulong iterations = 0;
do
{
z = z * z + c;
iterations++;
}
while (z.Magnitude < Configuration.Radius && iterations < Configuration.MaxIterations);
CollectCommonCalculations(x, y, iterations, z);
}
/// <summary>
/// Returns the color for the desired pixel.
/// </summary>
/// <param name="x">The horizontal position of the pixel.</param>
/// <param name="y">The vertical position of the pixel.</param>
/// <returns>A Color instance representing the RGB color of the pixel.</returns>
protected override Color GetColorForPixel(int x, int y)
{
int colorIndex;
if (double.IsInfinity(_calculationArray[x, y].Iterations))
{
colorIndex = 0;
}
else
{
double it = _calculationArray[x, y].Iterations;
colorIndex = (int)(ColorBitDepth * Math.Pow(it/Configuration.MaxIterations, it/Configuration.MaxIterations));
}
return Configuration.ArrayPalette[colorIndex];
}
}