Chris_M_Thomasson

Iterative Scaling Experiment

Sep 9th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 KB | None | 0 0
  1. /* Iterative Scaling Experiment by:
  2.      Chris M. Thomasson
  3. _________________________________________________________*/
  4.  
  5.  
  6.  
  7. /* Our global 2d plane
  8. _________________________________________________________*/
  9. float g_radius = 4.0; // master zoom
  10. ct_axes_2d g_axes = new ct_axes_2d(0, 0, g_radius);
  11. ct_plane_2d g_plane = null;
  12.  
  13.  
  14. //static int g_anime_frame_n = 24 * 5;
  15. static int g_anime_frame_n = 8 * 10;
  16. static float g_anime_frame_step = (PI * 2) / g_anime_frame_n;
  17. int g_anime_frame_i = 0;
  18. float g_anime_a0 = 0;
  19.  
  20.  
  21. static PFont g_font = null;
  22.  
  23.  
  24.  
  25.  
  26. // Runtime setup, pre-main...
  27. void setup()
  28. {
  29.   // setup our global plane
  30.   //size(3840, 2160);
  31.   //size(1920, 1080);
  32.   size(720, 405);
  33.   g_plane = new ct_plane_2d(g_axes, width, height);
  34.  
  35.   // Initial state
  36.   noFill();
  37.   noLoop();
  38.   background(0);
  39.   colorMode(RGB, 255);
  40.   strokeWeight(3);
  41.   stroke(255, 255, 255, 128);
  42.  
  43.   g_font = createFont("Arial",16,true); // STEP 2 Create Font
  44.  
  45.   // Call main!
  46.   ct_main();
  47. }
  48.  
  49.  
  50. /* Main
  51. _________________________________________________________*/
  52. void ct_main()
  53. {
  54.   ct_mbrot_plane(.1);
  55.  
  56.   stroke(255);
  57.   ct_circle(0, 0, 1);
  58.   //ct_render_grid();
  59.  
  60.   saveFrame("ct_vector_field.jpg");
  61.  
  62.   loop();
  63. }
  64.  
  65. float g_anime_scale = .1;
  66.  
  67. void draw()
  68. {
  69.   if (g_anime_frame_i < g_anime_frame_n)
  70.   {
  71.     float angle = g_anime_frame_step * g_anime_frame_i;
  72.     g_anime_scale = abs(sin(angle / 2)) * .25 + .05;
  73.     clear();
  74.     ct_mbrot_plane(g_anime_scale);
  75.    
  76.     print("g_anime_scale:" + g_anime_scale + ", ");
  77.     ct_save_frame("ct_mbrot");
  78.     ++g_anime_frame_i;
  79.   }
  80.  
  81.   else
  82.   {
  83.     println("Complete!");
  84.     noLoop();
  85.   }
  86. }
  87.  
  88.  
  89. float ct_cmul_x(float x0, float y0, float x1, float y1)
  90. {
  91.   return x0 * x1 - y0 * y1;
  92. }
  93.  
  94. float ct_cmul_y(float x0, float y0, float x1, float y1)
  95. {
  96.   return x0 * y1 + y0 * x1;
  97. }
  98.  
  99.  
  100. void ct_save_frame(String prefix)
  101. {
  102.    String file = "\\movie\\" + prefix + "_" + g_anime_frame_i + ".jpg";
  103.    //saveFrame(file);
  104.    println("frame:" + file);
  105. }
  106.  
  107. // Iterative Re-Scaling
  108. void ct_mbrot_point(
  109.   int xi, int yi,
  110.   float zx, float zy,
  111.   float cx, float cy,
  112.   float scale
  113. ) {
  114.   int n = 73;
  115.   int b = 0;
  116.   float rn = 2; // escape
  117.  
  118.   float o = 99999999999.0;
  119.  
  120.   for (int i = 1; i < n + 1; ++i)
  121.   {
  122.      // z = z^2+c
  123.      float tzx = ct_cmul_x(zx, zy, zx, zy);
  124.      zy = ct_cmul_y(zx, zy, zx, zy);
  125.      zx = tzx;
  126.      
  127.      zx += cx;
  128.      zy += cy;
  129.      
  130.      // r = abs(z);
  131.      float r = sqrt(zx*zx + zy*zy);
  132.      
  133.      // orbit trap for coloring...
  134.      if (r < o) o = r;
  135.      
  136.      // check for escape...
  137.      if (r > rn)
  138.      {
  139.        // retry 5 times
  140.        if (b < 5)
  141.        {
  142.          // scale z back, z *= scale
  143.          zx *= scale;
  144.          zy *= scale;
  145.          ++b;
  146.          
  147.          // increase the next escape radius
  148.          rn += 2 + scale;
  149.          continue;
  150.        }
  151.        
  152.        // Color outside
  153.        //stroke((i * 123) % 256, (i * 4586) % 256, i);
  154.        int red = (int)(o * (i * i));
  155.        stroke(red % 256, (red * 10 * scale) % 256, (i * 453) % 256);
  156.        point(xi, yi);
  157.        
  158.        return;
  159.      }
  160.   }
  161.  
  162.   // Color inside
  163.   //stroke(0, 255, 0);
  164.   int red = (int)(o * 256);
  165.   stroke(red % 256, 0, 0);
  166.   point(xi, yi);
  167. }
  168.  
  169.  
  170. // Iterate the global plane
  171. void ct_mbrot_plane(float scale)
  172. {
  173.    for (int yi = 0; yi < height; ++yi)
  174.    {
  175.      for (int xi = 0; xi < width; ++xi)
  176.      {
  177.        float zx = g_plane.project_x(xi);// * .15 * (1/scale);
  178.        float zy = g_plane.project_y(yi);// * .15 * (1/scale);
  179.        
  180.        ct_mbrot_point(xi, yi, zx, zy, zx, zy, scale);
  181.      }
  182.    }
  183. }
  184.  
  185.  
  186. void ct_render_grid()
  187. {
  188.   // white lines and circle
  189.   stroke(255, 255, 255);
  190.   ct_circle(0, 0, 1);
  191.   ct_rect_center(0, 0, 1, 1);
  192.   ct_line(-1, 1, -2, 2);
  193.   ct_line(1, 1, 2, 2);
  194.   ct_line(1, -1, 2, -2);
  195.   ct_line(-1, -1, -2, -2);
  196.  
  197.   // purple circles and lines
  198.   stroke(255, 255, 0);
  199.   ct_circle(0, 0, 2);
  200.   ct_rect_center(0, 0, 2, 2);
  201.   stroke(255, 0, 255);
  202.   ct_circle(-1.5, 0, .5);
  203.   ct_circle(1.5, 0, .5);
  204.   ct_circle(0, -1.5, .5);
  205.   ct_circle(0, 1.5, .5);
  206.      
  207.   // axes green as x and red as y.
  208.   float oradius = (sqrt(8) - 2) / 2;
  209.   stroke(0, 255, 0);
  210.   ct_line(-oradius * 2 - 2, 0, oradius * 2 + 2, 0);
  211.   stroke(255, 0, 0);
  212.   ct_line(0, -oradius * 2 - 2, 0, oradius * 2 + 2);
  213.  
  214.   // Cyan circles
  215.   stroke(0, 255, 255);
  216.   ct_circle(0, 0, sqrt(8));
  217.   ct_circle(-2 - oradius, 0, oradius);
  218.   ct_circle(2 + oradius, 0, oradius);
  219.   ct_circle(0, -2 - oradius, oradius);
  220.   ct_circle(0, 2 + oradius, oradius);
  221. }
  222.  
  223.  
  224.  
  225. /* 2d Projected Drawing Tools
  226. _________________________________________________________*/
  227. void ct_circle(float x, float y, float r)
  228. {
  229.   float x_proj = g_plane.project_x(x);
  230.   float y_proj = g_plane.project_y(y);
  231.   float r_proj = g_plane.project_h(r) * 2;
  232.  
  233.   ellipse(x_proj, y_proj, r_proj, r_proj);
  234. }
  235.  
  236.  
  237. void ct_rect_center(float x, float y, float w, float h)
  238. {
  239.   ct_rect(x - w, y + h, w, h);
  240. }
  241.  
  242. void ct_text(float x, float y, String b)
  243. {
  244.   float x_proj = g_plane.project_x(x);
  245.   float y_proj = g_plane.project_y(y);
  246.  
  247.   textFont(g_font, 20);                  // STEP 3 Specify font to be used
  248.   fill(255);                         // STEP 4 Specify font color
  249.   text(b, x_proj, y_proj);   // STEP 5 Display Text
  250.   noFill();
  251. }
  252.  
  253.  
  254. void ct_rect(float x, float y, float w, float h)
  255. {
  256.   float x_proj = g_plane.project_x(x);
  257.   float y_proj = g_plane.project_y(y);
  258.   float w_proj = g_plane.project_w(w) * 2;
  259.   float h_proj = g_plane.project_h(h) * 2;
  260.  
  261.   rect(x_proj, y_proj, w_proj, h_proj);
  262. }
  263.  
  264.  
  265. void ct_line(float x0, float y0, float x1, float y1)
  266. {
  267.   float x0_proj = g_plane.project_x(x0);
  268.   float y0_proj = g_plane.project_y(y0);
  269.   float x1_proj = g_plane.project_x(x1);
  270.   float y1_proj = g_plane.project_y(y1);
  271.  
  272.   line(x0_proj, y0_proj, x1_proj, y1_proj);
  273. }
  274.  
  275. void ct_point(float x, float y)
  276. {
  277.   float x_proj = g_plane.project_x(x);
  278.   float y_proj = g_plane.project_y(y);
  279.  
  280.   point(x_proj, y_proj);
  281. }
  282.  
  283.  
  284. /* 2d Axes
  285. _________________________________________________________*/
  286. class ct_axes_2d
  287. {
  288.   float m_xmin;
  289.   float m_xmax;
  290.   float m_ymin;
  291.   float m_ymax;
  292.  
  293.   ct_axes_2d(float xmin, float xmax, float ymin, float ymax)
  294.   {
  295.     m_xmin = xmin;
  296.     m_xmax = xmax;
  297.     m_ymin = ymin;
  298.     m_ymax = ymax;
  299.   }
  300.  
  301.   ct_axes_2d(ct_axes_2d axes)
  302.   {
  303.     m_xmin = axes.m_xmin;
  304.     m_xmax = axes.m_xmax;
  305.     m_ymin = axes.m_ymin;
  306.     m_ymax = axes.m_ymax;
  307.   }
  308.  
  309.   ct_axes_2d(float x, float y, float r)
  310.   {
  311.     m_xmin = x - r;
  312.     m_xmax = x + r;
  313.     m_ymin = y - r;
  314.     m_ymax = y + r;
  315.   }
  316.  
  317.   ct_axes_2d copy_to(ct_axes_2d dest)
  318.   {
  319.     dest.m_xmin = m_xmin;
  320.     dest.m_xmax = m_xmax;
  321.     dest.m_ymin = m_ymin;
  322.     dest.m_ymax = m_ymax;
  323.    
  324.     return dest;
  325.   }
  326.  
  327.   float width() { return m_xmax - m_xmin; }
  328.   float height() { return m_ymax - m_ymin; }
  329. };
  330.  
  331.  
  332. /* 2d Plane
  333. _________________________________________________________*/
  334. class ct_plane_2d
  335. {
  336.   ct_axes_2d m_axes;
  337.   int m_width;
  338.   int m_height;
  339.   float m_aratio;
  340.   float m_xstep;
  341.   float m_ystep;
  342.  
  343.   ct_plane_2d(ct_axes_2d axes, int width_, int height_)
  344.   {
  345.     m_axes = new ct_axes_2d(axes); // copy
  346.     m_width = width_;
  347.     m_height = height_;
  348.    
  349.     float daspect = abs((float)height_ / width_);
  350.     float waspect = abs(m_axes.height() / m_axes.width());
  351.    
  352.     if (daspect > waspect)
  353.     {
  354.       float excess = m_axes.height() * (daspect / waspect - 1);
  355.       m_axes.m_ymax += excess / 2;
  356.       m_axes.m_ymin -= excess / 2;
  357.     }
  358.    
  359.     else if (daspect < waspect)
  360.     {
  361.       float excess = m_axes.width() * (waspect / daspect - 1);
  362.       m_axes.m_xmax += excess / 2;
  363.       m_axes.m_xmin -= excess / 2;
  364.     }
  365.    
  366.     m_xstep = m_axes.width() / ((m_width > 1) ? (m_width - 1) : m_width);
  367.     m_ystep = m_axes.height() / ((m_height > 1) ? (m_height - 1) : m_height);
  368.   }
  369.  
  370.   float project_x(float x)
  371.   {
  372.     return floor((x - m_axes.m_xmin) / m_xstep);
  373.   }
  374.  
  375.   float project_y(float y)
  376.   {
  377.     return floor((m_axes.m_ymax - y) / m_ystep);
  378.   }
  379.  
  380.   float project_w(float w)
  381.   {
  382.     return round(w / m_xstep);
  383.   }
  384.  
  385.   float project_h(float h)
  386.   {
  387.     return round(h / m_ystep);
  388.   }
  389.  
  390.   float project_x(int x)
  391.   {
  392.      return m_axes.m_xmin + x * m_xstep;
  393.   }
  394.  
  395.   float project_y(int y)
  396.   {
  397.      return m_axes.m_ymax - y * m_ystep;
  398.   }
  399. };
Advertisement
Add Comment
Please, Sign In to add comment