Guest User

Untitled

a guest
Apr 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Numerics;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class ImageProcessor
  12.     {
  13.         private int height;
  14.         private int width;
  15.         private double xRadius;
  16.         private double yRadius;
  17.         private double xInterval;
  18.         private double yInterval;
  19.         private double xStart;
  20.         private double yStart;
  21.         private double magMax;
  22.         private ColourCalculator calculator;
  23.  
  24.         public ImageProcessor(int width, int height, double xRadius, double yRadius, double magMax = -1, double xStart = 0, double yStart = 0)
  25.         {
  26.             this.height = height;
  27.             this.width = width;
  28.             this.xRadius = xRadius;
  29.             this.yRadius = yRadius;
  30.             this.calculator = new ColourCalculator(xRadius, yRadius);
  31.             this.xInterval = (2*(double)xRadius) / (double)width;
  32.             this.yInterval = (2*(double)yRadius) / (double)height;
  33.             this.xStart = xStart;
  34.             this.yStart = yStart;
  35.             this.magMax = magMax;
  36.         }
  37.  
  38.         public void DrawIterative(Equation equation, string filepath, bool BW = true)
  39.         {
  40.             Bitmap map = new Bitmap(width, height);
  41.             double x = xRadius + xStart;
  42.             double y = yRadius + yStart;
  43.             Color[,] rawData = new Color[width,height];
  44.             for (int i = 0; i < width; i++)
  45.             {
  46.                 for (int j = 0; j < height; j++)
  47.                 {
  48.                     try
  49.                     {
  50.                         map.SetPixel(i, j, calculator.CalculateColor(equation.ComputeIterate(new Complex(x, y)), equation.itMax, BW));
  51.                     }
  52.                     catch (DivideByZeroException)
  53.                     {
  54.                         map.SetPixel(i, j, Color.White);
  55.                     }
  56.                     y -= yInterval;
  57.                 }
  58.                 y = yRadius;
  59.                 x -= xInterval;
  60.             }
  61.             map.Save(filepath);
  62.         }
  63.  
  64.         public void DrawImage(Equation equation, string filepath, bool shade = false)
  65.         {
  66.             if (magMax < 0 && shade)
  67.             {
  68.                 calculator.maxMag = CalculateMaxMag(equation);
  69.             }
  70.             else if (shade)
  71.             {
  72.                 calculator.maxMag = magMax;
  73.             }
  74.             Bitmap map = new Bitmap(width, height);
  75.             double x = xRadius + xStart;
  76.             double y = yRadius + yStart;
  77.             for (int i = 0; i < width; i++)
  78.             {
  79.                 for (int j = 0; j < height; j++)
  80.                 {
  81.                     try
  82.                     {
  83.                         map.SetPixel(i, j, calculator.CalculateColor(equation.Compute(new Complex(x, y)), shade));
  84.                     }
  85.                     catch (DivideByZeroException)
  86.                     {
  87.                         map.SetPixel(i, j, Color.White);
  88.                     }
  89.                     y -= yInterval;
  90.                 }
  91.                 y = yRadius + yStart;
  92.                 x -= xInterval;
  93.             }
  94.             map.Save(filepath);
  95.         }
  96.  
  97.         private double CalculateMaxMag(Equation equation)
  98.         {
  99.             double average = 0;
  100.             double max = 0;
  101.             double x = xRadius;
  102.             double y = yRadius;
  103.             for (int i = 0; i < width; i++)
  104.             {
  105.                 for (int j = 0; j < height; j++)
  106.                 {
  107.                     try
  108.                     {
  109.                         double magnitude = equation.Compute(new Complex(x, y)).Magnitude;
  110.                         if (magnitude > max)
  111.                         {
  112.                             max = magnitude;
  113.                         }
  114.                         average += magnitude;
  115.                     }
  116.                     catch (DivideByZeroException)
  117.                     {
  118.  
  119.                     }
  120.                     y -= yInterval;
  121.                 }
  122.                 y = yRadius;
  123.                 x -= xInterval;
  124.             }
  125.             average = average / (height * width);
  126.             return average;
  127.         }
  128.     }
  129. }
Add Comment
Please, Sign In to add comment