Guest User

Untitled

a guest
Jun 18th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace SpecialOlympics.Mandelbrot
  9. {
  10.  
  11.     class Program
  12.     {
  13.  
  14.         interface IFractalPalette<out TColor>
  15.         {
  16.             TColor ColorOf(int iteration);
  17.         }
  18.  
  19.         interface IFractalImageGenerator<out TColor>
  20.         {
  21.             TColor[,] Generate();
  22.         }
  23.  
  24.         class SimpleBlackAndWhitePallete : IFractalPalette<Color>
  25.         {
  26.             private readonly int _maxIterations;
  27.  
  28.             public SimpleBlackAndWhitePallete(int maxIterations)
  29.             {
  30.                 _maxIterations = maxIterations;
  31.             }
  32.  
  33.             public Color ColorOf(int iteration)
  34.             {
  35.                 return iteration == _maxIterations ? Color.Black : Color.White;
  36.             }
  37.         }
  38.  
  39.         class MandelbrotGenerator<TColor> : IFractalImageGenerator<TColor>
  40.         {
  41.             protected readonly int _height;
  42.             protected readonly int _width;
  43.             protected readonly Tuple<float, float> _xDimensions;
  44.             protected readonly Tuple<float, float> _yDimensions;
  45.             protected readonly int _maxIterations;
  46.             protected readonly IFractalPalette<TColor> _palette;
  47.  
  48.             public MandelbrotGenerator(Tuple<float, float> xDimensions, Tuple<float, float> yDimensions, int height, int width, int maxIterations, IFractalPalette<TColor> palette)
  49.             {
  50.                 _xDimensions = xDimensions;
  51.                 _yDimensions = yDimensions;
  52.                 _height = height;
  53.                 _width = width;
  54.                 _maxIterations = maxIterations;
  55.                 _palette = palette;
  56.             }
  57.  
  58.             private TColor ComputeColor(int pixelX, int pixelY)
  59.             {
  60.                 var x0 = _xDimensions.Item1 + (pixelX / (float)_width) * (_xDimensions.Item2 - _xDimensions.Item1);
  61.                 var y0 = _yDimensions.Item1 + (pixelY / (float)_height) * (_yDimensions.Item2 - _yDimensions.Item1);
  62.                 var x = 0.0;
  63.                 var y = 0.0;
  64.                 var iteration = 0;
  65.                 while (x*x + y*y < 2*2 && iteration < _maxIterations)
  66.                 {
  67.                     var xTemp = x*x - y*y + x0;
  68.                     y = 2*x*y + y0;
  69.                     x = xTemp;
  70.                     iteration++;
  71.                 }
  72.  
  73.                 return _palette.ColorOf(iteration);
  74.             }
  75.  
  76.             public virtual TColor[,] Generate()
  77.             {
  78.                 var results = new TColor[_width, _height];
  79.                 for (var x = 0; x < _width; x++)
  80.                 {
  81.                     for (var y = 0; y < _height; y++)
  82.                     {
  83.                         results[x, y] = ComputeColor(x, y);
  84.                     }
  85.                 }
  86.                 return results;
  87.             }
  88.         }
  89.  
  90.         static void Main(string[] args)
  91.         {
  92.             using (var bitmap = new Bitmap(2048, 2048))
  93.             {
  94.                 var pallete = new SimpleBlackAndWhitePallete(2000);
  95.                 var generator = new MandelbrotGenerator<Color>(new Tuple<float, float>(-2.5f, 1f),
  96.                     new Tuple<float, float>(-1.75f, 1.75f), bitmap.Width, bitmap.Height, 2000, pallete);
  97.                 var colors = generator.Generate();
  98.  
  99.                 for (var i = 0; i < bitmap.Width; i++)
  100.                 {
  101.                     for (var j = 0; j < bitmap.Height; j++)
  102.                     {
  103.                         bitmap.SetPixel(i, j, colors[i, j]);
  104.                     }
  105.                 }
  106.                 bitmap.Save("mandelbrot.png", ImageFormat.Png);
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment