Advertisement
Guest User

multibrot - processing

a guest
Aug 30th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. //============================================================================
  2. //============================================================================
  3. // Complex numbers class so I can take (real) powers.
  4.  
  5. public class Complex {
  6.   float a; // real
  7.   float b; // imaginary
  8.   Complex() {
  9.     a = 0;
  10.     b = 0;
  11.   }
  12.   Complex(float a1, float b1) {
  13.     a = a1;
  14.     b = b1;
  15.   }
  16.   float abs() {
  17.     return sqrt(a*a + b*b);
  18.   }
  19. }
  20.  
  21. // calculates (a+ib)^p and puts the real part in a and the imaginary in b.
  22. Complex complex_pow(Complex c_in, float p) {
  23.   float a = c_in.a;
  24.   float b = c_in.b;
  25.   Complex c_out = new Complex();
  26.   c_out.a = pow(a*a + b*b, p/2.0) * cos(p * atan2(b, a));
  27.   c_out.b = pow(a*a + b*b, p/2.0) * sin(p * atan2(b, a));
  28.   return c_out;
  29. }
  30.  
  31. //============================================================================
  32. //============================================================================
  33. // Code to actually draw the multibrot set (for varying values of p) on the complex plane.
  34.  
  35. // Establish a range of values on the complex plane
  36. // A different range will allow us to "zoom" in or out on the fractal
  37. float xmin = -2;
  38. float ymin = 0;
  39. float w = 3;
  40. float h = 1.5;
  41.  
  42. int w2 = 640;
  43. int h2 = 360;
  44.  
  45. void setup() {
  46.   size(w2, h2);
  47.   background(255);
  48. }
  49.  
  50. void draw() {
  51.   drawM();
  52. }
  53.  
  54. float p = 2.0;
  55.  
  56. void drawM() {
  57.   // Make sure we can write to the pixels[] array.
  58.   // Only need to do this once since we don't do any other drawing.
  59.   loadPixels();
  60.  
  61.   // Maximum number of iterations for each point on the complex plane.
  62.   int maxiterations = 100;
  63.  
  64.   // x goes from xmin to xmax
  65.   float xmax = xmin + w;
  66.   // y goes from ymin to ymax
  67.   float ymax = ymin + h;
  68.  
  69.   // Calculate amount we increment x,y for each pixel.
  70.   float dx = (xmax - xmin) / (width);
  71.   float dy = (ymax - ymin) / (height);
  72.  
  73.   // Start y
  74.   float y = ymin;
  75.   for (int j = 0; j < height; j++) {
  76.     // Start x
  77.     float x = xmin;
  78.     for (int i = 0;  i < width; i++) {
  79.  
  80.       // Now we test, as we iterate z = z^p + c does z tend towards infinity?
  81.       Complex c = new Complex(x,y);
  82.       int n = 0;
  83.       while (n < maxiterations) {
  84.           c = complex_pow(c, p);
  85.           c.a = c.a + x;
  86.           c.b = c.b + y;
  87.         // Infinty in our finite world is simple, let's just consider it 100.
  88.         if (c.abs() > 100.0) {
  89.           break;  // Bail
  90.         }
  91.         n++;
  92.       }
  93.  
  94.       // We color each pixel based on how long it takes to get to infinity
  95.       // If we never got there, let's pick the color black.
  96.       if (n == maxiterations) {
  97.         pixels[i+j*width] = color(0);
  98.       }
  99.       else {
  100.         // The slower this pixel / point in the complex plane went to
  101.         // infinity the lighter its colour.
  102.         pixels[i+j*width] = color(n*3 % 255);
  103.       }
  104.       x += dx;
  105.     }
  106.     y += dy;
  107.   }
  108.   updatePixels();
  109.   if (p <= 6) p += 0.01;
  110.   saveFrame("h###.gif");
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement