Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import se.lth.cs.ptdc.fractal.MandelbrotGUI;
  2.  
  3. import java.awt.Color;
  4.  
  5. public class Generator {
  6.  
  7.     public Generator(MandelbrotGUI gui) {
  8.        
  9.     }
  10.    
  11.     public void render(MandelbrotGUI gui) {
  12.         gui.disableInput();
  13.        
  14.         int resDiff = 1;
  15.        
  16.         switch(gui.getResolution()) {
  17.         case MandelbrotGUI.RESOLUTION_VERY_HIGH:
  18.             resDiff = 1;
  19.             break;
  20.         case MandelbrotGUI.RESOLUTION_HIGH:
  21.             resDiff = 3;
  22.             break;
  23.         case MandelbrotGUI.RESOLUTION_MEDIUM:
  24.             resDiff = 5;
  25.             break;
  26.         case MandelbrotGUI.RESOLUTION_LOW:
  27.             resDiff = 7;
  28.             break;
  29.         case MandelbrotGUI.RESOLUTION_VERY_LOW:
  30.             resDiff = 9;
  31.             break;
  32.         }
  33.        
  34.         Complex[][] complex = mesh(gui.getMinimumReal(),
  35.                 gui.getMaximumReal(),
  36.                 gui.getMinimumImag(),
  37.                 gui.getMaximumImag(),
  38.                 gui.getWidth(),
  39.                 gui.getHeight());
  40.  
  41.         Color[][] picture = new Color[gui.getHeight() / resDiff + 1][gui.getWidth() / resDiff + 1];
  42.        
  43.         for (int i = resDiff / 2; i < gui.getHeight(); i += resDiff) {
  44.             for (int j = resDiff / 2; j < gui.getWidth(); j += resDiff) {
  45.                
  46.                 Complex c = complex[i][j];
  47.                
  48.                 if (c.getAbs2() > 1)
  49.                     picture[i / resDiff][j / resDiff] = Color.WHITE;
  50.                 else if (c.re > 0 && c.im >= 0)
  51.                     picture[i / resDiff][j / resDiff] = Color.RED;
  52.                 else if (c.re < 0 && c.im >= 0)
  53.                     picture[i / resDiff][j / resDiff] = Color.BLUE;
  54.                 else if (c.re > 0 && c.im <= 0)
  55.                     picture[i / resDiff][j / resDiff] = Color.YELLOW;
  56.                 else if (c.re < 0 && c.im <= 0)
  57.                     picture[i / resDiff][j / resDiff] = Color.GREEN;
  58.             }
  59.         }
  60.        
  61.         gui.putData(picture, resDiff, resDiff);
  62.        
  63.         gui.enableInput();
  64.     }
  65.  
  66.     private Complex[][] mesh(double minRe, double maxRe, double minIm,
  67.             double maxIm, int width, int height) {
  68.  
  69.         Complex[][] complex = new Complex[height][width];
  70.  
  71.         double resRe = (maxRe - minRe) / width;
  72.         double resIm = (maxIm - minIm) / height;
  73.  
  74.         for (int i = 0; i < height; i++)
  75.             for (int j = 0; j < width; j++)
  76.                 complex[i][j] = new Complex(minRe + j * resRe, maxIm - i * resIm);
  77.  
  78.         return complex;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement