public ParallelStructMandelbrot(float width, float height, int rows, int columns, ComplexNumberStruct center) { Width = width; Height = height; Rows = rows; Columns = columns; Center = center; }
private static bool BelongsToMandelbrot(ComplexNumberStruct number, int iterations) { var zNumber = new ComplexNumberStruct(0.0f, 0.0f); var accumulator = 0; while (accumulator < iterations && zNumber.Magnitude() < 2.0) { zNumber = zNumber.MultiplyWith(zNumber).AddTo(number); ++accumulator; } return(accumulator == iterations); }
private Action <BitmapData, byte[]> DrawingStrategy() => (bitmapData, pixels) => Parallel.For(0, Columns - 1, column => { for (var row = 0; row < Rows; row++) { var x = Center.ComputeRow(row, Width, Columns); var y = Center.ComputeColumn(column, Height, Rows); var c = new ComplexNumberStruct(x, y); var color = BelongsToMandelbrot(c, 100) ? Color.Black : Color.White; var offset = (column * bitmapData.Stride) + (3 * row); pixels.WriteColors(offset, color); } });
public static float ComputeColumn(this ComplexNumberStruct center, int col, float height, int rows) => center.ImaginaryPart - height / 2.0f + col * height / rows;
public static float ComputeRow(this ComplexNumberStruct center, int row, float width, int columns) => center.RealPart - width / 2.0f + row * width / columns;
public static ComplexNumberStruct MultiplyWith(this ComplexNumberStruct n1, ComplexNumberStruct n2) => new ComplexNumberStruct(n1.RealPart * n2.RealPart - n1.ImaginaryPart * n2.ImaginaryPart, n1.RealPart * n2.ImaginaryPart + n1.ImaginaryPart * n2.RealPart);
public static ComplexNumberStruct AddTo(this ComplexNumberStruct n1, ComplexNumberStruct n2) => new ComplexNumberStruct(n1.RealPart + n2.RealPart, n1.ImaginaryPart + n2.ImaginaryPart);
public static double Magnitude(this ComplexNumberStruct number) => Math.Sqrt(number.RealPart * number.RealPart * number.ImaginaryPart * number.ImaginaryPart);