Guest User

double parameter julia set processing 3.5.3

a guest
Sep 14th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.59 KB | None | 0 0
  1. int iteration_max = 100;
  2. float escape = 16;
  3. int hypersampling_factor = 5; //only relevant for high quality, warning, quadratic growth in computation time
  4. float xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag;
  5.  
  6. void setup() {
  7.   size(1500, 1000); //aspect ratio 3:2 for symmetry
  8.   background(0);
  9.  
  10.   xup = 2;
  11.   xlow = -2;
  12.   yup = 2;
  13.   ylow = -2;
  14.  
  15.   /*c1_real = -1;
  16.   c1_imag = 0.2;
  17.   c2_real = -0.5;
  18.   c2_imag = 0.5;*/
  19.  
  20.   c1_real = -0.102;
  21.   c1_imag = +0.828;  
  22.   c2_real = 0.810;
  23.   c2_imag = 0.582;
  24.  
  25.   /*c1_real = -0.25155795;
  26.   c1_imag = 0.004885681;
  27.   c2_real = -0.8748981;
  28.   c2_imag = 0.03848663;*/
  29.  
  30.   /*c1_real = -0.5126952;
  31.   c1_imag = -0.11730869;
  32.   c2_real  = 0.20672332;
  33.   c2_imag = -0.58003414;*/
  34.  
  35.   draw_all_sets_std(true, iteration_max, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  36.   draw_all_sets_description();
  37.  
  38.   draw_all_coords(false, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  39.  
  40.   String s = "";
  41.  
  42.   s += "it" + str(iteration_max);
  43.   if (c1_imag >= 0) {
  44.     s += "c1_" + str(c1_real) + "+" + str(c1_imag) + "i";
  45.   } else {
  46.     s += "c1_" + str(c1_real) + str(c1_imag) + "i";
  47.   }
  48.   if (c2_imag >= 0) {
  49.     s += "c2_" + str(c2_real) + "+" + str(c2_imag) + "i";
  50.   } else {
  51.     s += "c2_" + str(c2_real) + str(c2_imag) + "i";
  52.   }
  53.  
  54.   // comment in to save image
  55.   //save("6view_5x5antial_" + s + ".png");
  56. }
  57.  
  58. void draw() {
  59.    
  60. }
  61.  
  62. void color_if_point_in_set(int num, int iteration_max, float z_real, float z_imag, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  63.   if (iteration_max == iterate_switch(num, iteration_max, z_real, z_imag, c1_real, c1_imag, c2_real, c2_imag)) {
  64.       fill(0, 255, 0);
  65.       stroke(0, 255, 0);
  66.     } else {
  67.       fill(255, 0, 128);
  68.       stroke(255, 0, 128);
  69.     }
  70. }
  71.  
  72. void draw_all_coords(boolean lines, float xup, float xlow, float yup, float ylow, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  73.   float x1, y1, x2, y2;
  74.   x1 = map(c1_real, xlow, xup, 0, width/3);
  75.   y1 = map(c1_imag, ylow, yup, 0, height/2);
  76.   x2 = map(c2_real, xlow, xup, 0, width/3);
  77.   y2 = map(c2_imag, ylow, yup, 0, height/2);
  78.  
  79.   textSize(12);
  80.   if ((x1 < width/3) && (0 < x1) && (y1 < height/2) && (0 < y1)) {    
  81.     color_if_point_in_set(0, iteration_max, c1_real, c1_imag, c1_real, c1_imag, c2_real, c2_imag);
  82.     rect(x1 - 2, y1 - 2, 4, 4);
  83.     text("c1", x1 + 2, y1 + 12);
  84.     if (lines) {line(x1, 0, x1, height/2); line(0, y1, width/3, y1);}
  85.    
  86.     color_if_point_in_set(1, iteration_max, c1_real, c1_imag, c1_real, c1_imag, c2_real, c2_imag);
  87.     rect(x1 - 2, y1 + height/2 - 2, 4, 4);
  88.     text("c1", x1 + 2, y1 + height/2 + 12);
  89.     if (lines) {line(x1, height/2, x1, height); line(0, y1 + height/2, width/3, y1 + height/2);}
  90.        
  91.     color_if_point_in_set(2, iteration_max, c1_real, c1_imag, c1_real, c1_imag, c2_real, c2_imag);
  92.     rect(x1 + width/3 - 2, y1 - 2, 4, 4);
  93.     text("c1", x1 + width/3 + 2, y1 + 12);
  94.     if (lines) {line(x1 + width/3, 0, x1 + width/3, height/2); line(width/3, y1, 2*width/3, y1);}
  95.    
  96.     color_if_point_in_set(3, iteration_max, c1_real, c1_imag, c1_real, c1_imag, c2_real, c2_imag);
  97.     rect(x1 + width/3 - 2, y1 + height/2 - 2, 4, 4);
  98.     text("c1", x1 + width/3 + 2, y1 + height/2 + 12);
  99.     if (lines) {line(x1 + width/3, height/2, x1 + width/3, height); line(width/3, y1 + height/2, 2*width/3, y1 + height/2);}
  100.   }
  101.   if ((x2 < width/3) && (0 < x2) && (y2 < height/2) && (0 < y2)) {    
  102.     color_if_point_in_set(0, iteration_max, c2_real, c2_imag, c1_real, c1_imag, c2_real, c2_imag);
  103.     rect(x2 - 2, y2 - 2, 4, 4);
  104.     text("c2", x2 + 2, y2 + 12);
  105.     if (lines) {line(x2, 0, x2, height/2); line(0, y2, width/3, y2);}
  106.    
  107.     color_if_point_in_set(1, iteration_max, c2_real, c2_imag, c1_real, c1_imag, c2_real, c2_imag);
  108.     rect(x2 - 2, y2 + height/2 - 2, 4, 4);
  109.     text("c2", x2 + 2, y2 + height/2 + 12);
  110.     if (lines) {line(x2, height/2, x2, height); line(0, y2 + height/2, width/3, y2 + height/2);}
  111.        
  112.     color_if_point_in_set(2, iteration_max, c2_real, c2_imag, c1_real, c1_imag, c2_real, c2_imag);
  113.     rect(x2 + width/3 - 2, y2 - 2, 4, 4);
  114.     text("c2", x2 + width/3 + 2, y2 + 12);
  115.     if (lines) {line(x2 + width/3, 0, x2 + width/3, height/2); line(width/3, y2, 2*width/3, y2);}
  116.    
  117.     color_if_point_in_set(3, iteration_max, c2_real, c2_imag, c1_real, c1_imag, c2_real, c2_imag);
  118.     rect(x2 + width/3 - 2, y2 + height/2 - 2, 4, 4);
  119.     text("c2", x2 + width/3 + 2, y2 + height/2 + 12);
  120.     if (lines) {line(x2 + width/3, height/2, x2 + width/3, height); line(width/3, y2 + height/2, 2*width/3, y2 + height/2);}
  121.   }
  122. }
  123.  
  124. void draw_all_sets_std(boolean high_quality, int iteration_max, float xup, float xlow, float yup, float ylow, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  125.   if (high_quality) {
  126.     println("0/6");
  127.     draw_set_hq(0, iteration_max, 0, int(width/3), 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  128.     println("1/6");
  129.     draw_set_hq(1, iteration_max, 0, int(width/3), int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  130.     println("2/6");
  131.     draw_set_hq(2, iteration_max, int(width/3), int(2*width/3), 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  132.     println("3/6");
  133.     draw_set_hq(3, iteration_max, int(width/3), int(2*width/3), int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  134.     println("4/6");
  135.     draw_set_hq(4, iteration_max, int(2*width/3), width, 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  136.     println("5/6");
  137.     draw_set_hq(5, iteration_max, int(2*width/3), width, int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  138.     println("6/6");
  139.   } else {
  140.     println("0/6");
  141.     draw_set(0, iteration_max, 0, int(width/3), 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  142.     println("1/6");
  143.     draw_set(1, iteration_max, 0, int(width/3), int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  144.     println("2/6");
  145.     draw_set(2, iteration_max, int(width/3), int(2*width/3), 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  146.     println("3/6");
  147.     draw_set(3, iteration_max, int(width/3), int(2*width/3), int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  148.     println("4/6");
  149.     draw_set(4, iteration_max, int(2*width/3), width, 0, int(height/2), xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  150.     println("5/6");
  151.     draw_set(5, iteration_max, int(2*width/3), width, int(height/2), height, xup, xlow, yup, ylow, c1_real, c1_imag, c2_real, c2_imag);
  152.     println("6/6");
  153.   }
  154.  
  155.   draw_all_sets_description();
  156. }
  157.  
  158. void draw_all_sets_description() {
  159.   stroke(255, 0, 0);
  160.   line(width/3, 0, width/3, height);
  161.   line(2*width/3, 0, 2*width/3, height);
  162.   line(0, height/2, width, height/2);
  163.  
  164.   textSize(12);
  165.   fill(255, 0, 0);
  166.   text("M(c1)", 2, 12);
  167.   text("M'(c1)", 2, height/2 + 12);
  168.   text("M(c2)", width/3 + 2, 12);
  169.   text("M'(c2)", width/3 + 2, height/2 + 12);
  170.   text("J(c1, c2)", 2*width/3 + 4, 12);
  171.   text("J(c2, c1)", 2*width/3 + 4, height/2 + 12);
  172. }
  173.  
  174. void draw_set(int num, int iteration_max, int xleft, int xright, int ytop, int ybottom, float xup, float xlow, float yup, float ylow, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  175.   float z_real, z_imag;
  176.   int i = 0;
  177.   for(int x = xleft; x < xright; x++) {
  178.     for(int y = ytop; y < ybottom; y++) {
  179.       z_real = map(x, xleft, xright, xlow, xup);
  180.       z_imag = map(y, ytop, ybottom, ylow, yup);
  181.      
  182.       i = iterate_switch(num, iteration_max, z_real, z_imag, c1_real, c1_imag, c2_real, c2_imag);
  183.      
  184.       if (i == iteration_max) {
  185.         set(x, y, color(255));
  186.       } else {
  187.         //set(x, y, color(int(map(i, 0, iteration_max, 0, 255))));
  188.         set(x, y, color(int(map(log(float(i)/20+1), 0, log(float(iteration_max)/10+1), 0, 255))));
  189.       }
  190.     }
  191.   }
  192. }
  193.  
  194. void draw_set_hq(int num, int iteration_max, int xleft, int xright, int ytop, int ybottom, float xup, float xlow, float yup, float ylow, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  195.   float z_real, z_imag;
  196.   int i = 0;
  197.   for(int x = xleft; x < xright; x++) {
  198.     for(int y = ytop; y < ybottom; y++) {
  199.       i = 0;
  200.       for (int dx = 0; dx < hypersampling_factor; dx++) {
  201.         for (int dy = 0; dy < hypersampling_factor; dy++) {
  202.           z_real = map(x + float(dx)/hypersampling_factor - float(hypersampling_factor)/2, xleft, xright, xlow, xup);
  203.           z_imag = map(y + float(dy)/hypersampling_factor - float(hypersampling_factor)/2, ytop, ybottom, ylow, yup);
  204.          
  205.           i += iterate_switch(num, iteration_max, z_real, z_imag, c1_real, c1_imag, c2_real, c2_imag);
  206.         }
  207.       }
  208.      
  209.       if (i == iteration_max * sq(hypersampling_factor)) {
  210.         set(x, y, color(255));
  211.       } else {
  212.         //set(x, y, color(int(map(i, 0, iteration_max * sq(hypersampling_factor), 0, 255))));
  213.         set(x, y, color(int(map(log(float(i)/(20 * sq(hypersampling_factor))+1), 0, log(float(iteration_max)/10+1), 0, 255))));
  214.       }
  215.     }
  216.   }
  217. }
  218.  
  219. int iterate_switch(int num, int iteration_max, float z_real, float z_imag, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  220.   switch (num) {
  221.     case -1:
  222.       // M
  223.       return iterate(iteration_max, 0, 0, z_real, z_imag, z_real, z_imag);
  224.     case 0:
  225.       // M(c1)
  226.       return iterate(iteration_max, 0, 0, z_real, z_imag, c1_real, c1_imag);
  227.     case 1:
  228.       // M'(c1)
  229.       return iterate(iteration_max, 0, 0, c1_real, c1_imag, z_real, z_imag);
  230.     case 2:
  231.       // M(c2)
  232.       return iterate(iteration_max, 0, 0, z_real, z_imag, c2_real, c2_imag);
  233.     case 3:
  234.       // M'(c2)
  235.       return iterate(iteration_max, 0, 0, c2_real, c2_imag, z_real, z_imag);
  236.     case 4:
  237.       // J(c1, c2)
  238.       return iterate(iteration_max, z_real, z_imag, c1_real, c1_imag, c2_real, c2_imag);
  239.     case 5:
  240.       // J(c2, c1)
  241.       return iterate(iteration_max, z_real, z_imag, c2_real, c2_imag, c1_real, c1_imag);
  242.     default:
  243.       // should never happen
  244.       return -1;
  245.   }
  246. }
  247.  
  248. int iterate(int iteration_max, float z_real, float z_imag, float c1_real, float c1_imag, float c2_real, float c2_imag) {
  249.   int i = 0;
  250.  
  251.   float zt_real, zt_imag;
  252.  
  253.   while((i < iteration_max) && (sq(z_real)+sq(z_real) < escape)) {
  254.     zt_real = sq(z_real) - sq(z_imag) + c1_real;
  255.     zt_imag = 2 * z_real * z_imag + c1_imag;  
  256.     z_real = sq(zt_real) - sq(zt_imag) + c2_real;
  257.     z_imag = 2 * zt_real * zt_imag + c2_imag;
  258.     i++;
  259.   }
  260.  
  261.   return i;
  262. }
Add Comment
Please, Sign In to add comment