Advertisement
Guest User

Mandelbrot.bat

a guest
Apr 12th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.37 KB | None | 0 0
  1. <!-- : class="re0">diese Zeile nicht  löschen
  2. @echo off
  3. title %~nx0
  4. echo  this will draw a "Mandelbrot-Fraktal" as mutlithreaded HTML5 Site.
  5. echo  it uses the highest available IE-version running by mshta.exe .
  6. echo  at Windows 10  and  the Edge-Browser this will run very fast.
  7. echo  Edge uses by default extensions like asm.js...
  8. echo  the Code of  this  program itself is not optimized for speed.
  9. echo  this code will calculate brute-force up to 10.000 iterations per point!
  10. echo.
  11. echo  you  can dive into the fractal by clicking at the desired areal.
  12. echo  the calculation-deep is limited by the floating point precision of javascript.
  13. echo  so it will produce weird output in far deep...
  14. echo.
  15. echo  now press any key to run...
  16. pause>nul
  17.  
  18.  
  19. ::there are  3 Files in in this one... 1 Batch, 1 HTML and 1 Javascript
  20. ::to work "multithreaded" the Html needs an external workerfile.
  21. ::so it's impossible?  to include the workercode into the Html.
  22. ::extract webworker-file from end of this File
  23. call :extract_worker "%~f0"
  24. mshta "%~f0"
  25. del worker.js
  26. exit /b 0
  27.  
  28. :extract_worker
  29. call :find1stLine2read "%~dpnx1"
  30. for /F "skip=%firstLine2Read% delims=" %%a in ('type "%~1"') do (
  31. echo. %%a >>worker.js
  32. )
  33. exit /b
  34. :find1stLine2read
  35. for /f "usebackq delims=:" %%a in (`findstr /nic:"::::worker" "%~1"`) do (
  36.   set /a "firstLine2Read=%%a"
  37. )
  38. exit /b
  39.  
  40. und diese auch nicht -->
  41. <!DOCTYPE html>
  42. <meta http-equiv="x-ua-compatible" content="ie=edge">
  43. <html><head>
  44.     <title>Mandelbrot</title>
  45.     <style type="text/css">
  46.       html, body {
  47.         width: 100%;
  48.         height: 100%;
  49.         margin: 0px;
  50.         padding: 0px;
  51.       }
  52.     </style>
  53.    
  54.     <script type="text/javascript">
  55.       window.addEventListener("load", function (event) {
  56.           var canvas = document.getElementById("fractal")
  57.           var maxTheads = 6;
  58.           mandelbrot = new Mandelbrot(canvas, maxTheads);
  59.           // This will resize the canvas and kick off the initial redraw.
  60.           mandelbrot.resize_to_parent();
  61.       }, false);
  62.      
  63. //engine      
  64.     var Mandelbrot = function (canvas, n_workers) {
  65.     var self = this; // for use in closures where 'this' is rebound
  66.     this.canvas = canvas;
  67.     this.ctx = canvas.getContext("2d");
  68.     this.row_data = this.ctx.createImageData(canvas.width, 1);
  69.     this.canvas.addEventListener("click", function(event) {
  70.         self.click(event.clientX + document.body.scrollLeft +
  71.                    document.documentElement.scrollLeft - canvas.offsetLeft,
  72.                    event.clientY + document.body.scrollTop +
  73.                    document.documentElement.scrollTop - canvas.offsetTop);
  74.     }, false);
  75.     window.addEventListener("resize", function(event) {
  76.         self.resize_to_parent();
  77.     }, false);
  78.  
  79.     this.workers = [];
  80.     for (var i = 0; i < n_workers; i++) {
  81.         var worker = new Worker("worker.js");
  82.         worker.onmessage = function(event) {
  83.                 self.received_row(event.target, event.data)
  84.         }
  85.         worker.idle = true;
  86.         this.workers.push(worker);
  87.     }
  88.     this.i_max = 1.5;
  89.     this.i_min = -1.5;
  90.     this.r_min = -2.5;
  91.     this.r_max = 1.5;
  92.     this.max_iter = 10000;
  93.     this.escape = 100;
  94.  
  95.  
  96.     this.generation = 0;
  97.     this.nextrow = 0;
  98.  
  99.     this.make_palette()
  100. }
  101.  
  102. Mandelbrot.prototype = {
  103.     make_palette: function() {
  104.         this.palette = []
  105.         // wrap values to a saw tooth pattern.
  106.         function wrap(x) {
  107.             x = ((x + 256) & 0x1ff) - 256;
  108.             if (x < 0) x = -x;
  109.             return x;
  110.         }
  111.         for (i = 0; i <= this.max_iter; i++) {
  112.             this.palette.push([wrap(7*i), wrap(5*i), wrap(11*i)]);
  113.         }
  114.     },
  115.  
  116.     draw_row: function(data) {
  117.         var values = data.values;
  118.         var pdata = this.row_data.data;
  119.         for (var i = 0; i < this.row_data.width; i++) {
  120.             var pixel;
  121.             pdata[4*i+3] = 255;
  122.             if (values[i] < 0) {
  123.                 pdata[4*i] = pdata[4*i+1] = pdata[4*i+2] = 0;
  124.             } else {
  125.                 var colour = this.palette[values[i]];
  126.                 pdata[4*i] = colour[0];
  127.                 pdata[4*i+1] = colour[1];
  128.                 pdata[4*i+2] = colour[2];
  129.             }
  130.         }
  131.         this.ctx.putImageData(this.row_data, 0, data.row);
  132.     },
  133.  
  134.     received_row: function (worker, data) {
  135.         if (data.generation == this.generation) {
  136.             // Interesting data: display it.
  137.             this.draw_row(data);
  138.         }
  139.         this.process_row(worker);
  140.     },
  141.  
  142.     process_row: function(worker) {
  143.         var row = this.nextrow++;
  144.         if (row >= this.canvas.height) {
  145.             worker.idle = true;
  146.         } else {
  147.             worker.idle = false;
  148.             worker.postMessage({
  149.                 row: row,
  150.                 width: this.row_data.width,
  151.                 generation: this.generation,
  152.                 r_min: this.r_min,
  153.                 r_max: this.r_max,
  154.                 i: this.i_max + (this.i_min - this.i_max) * row / this.canvas.height,
  155.                 max_iter: this.max_iter,
  156.                 escape: this.escape,
  157.            })
  158.         }
  159.     },
  160.  
  161.     redraw: function() {
  162.         this.generation++;
  163.         this.nextrow = 0;
  164.         for (var i = 0; i < this.workers.length; i++) {
  165.             var worker = this.workers[i];
  166.             if (worker.idle)
  167.                 this.process_row(worker);
  168.         }
  169.     },
  170.  
  171.     click: function(x, y) {
  172.         var width = this.r_max - this.r_min;
  173.         var height = this.i_min - this.i_max;
  174.         var click_r = this.r_min + width * x / this.canvas.width;
  175.         var click_i = this.i_max + height * y / this.canvas.height;
  176.         var zoomf = 10;
  177.         this.r_min = click_r - width/zoomf;
  178.         this.r_max = click_r + width/zoomf;
  179.         this.i_max = click_i - height/zoomf;
  180.         this.i_min = click_i + height/zoomf;
  181.         this.redraw()
  182.     },
  183.  
  184.     resize_to_parent: function() {
  185.         this.canvas.width = window.innerWidth;
  186.         this.canvas.height = window.innerHeight;
  187.  
  188.         // Adjust the horizontal scale to maintain aspect ratio
  189.         var width = ((this.i_max - this.i_min) *
  190.                      this.canvas.width / this.canvas.height);
  191.         var r_mid = (this.r_max + this.r_min) / 2;
  192.         this.r_min = r_mid - width/2;
  193.         this.r_max = r_mid + width/2;
  194.  
  195.         // Reallocate the image data object used to draw rows.
  196.         this.row_data = this.ctx.createImageData(this.canvas.width, 1);
  197.  
  198.         this.redraw();
  199.     },
  200. }
  201.  
  202.     </script>
  203.   </head>
  204.   <body>
  205.     <canvas id="fractal" width="1404" height="743"></canvas>
  206.  
  207.  
  208. </body></html>
  209.  
  210.  
  211. <!--- don't remove/change the  next  line it's a marker  for the extract-routine
  212. ::::worker
  213. self.onmessage = function (event) {
  214.     var data = event.data;
  215.     var c_i = data.i;
  216.     var max_iter = data.max_iter;
  217.     var escape = data.escape * data.escape;
  218.     data.values = [];
  219.     for (var i = 0; i < data.width; i++) {
  220.         var c_r = data.r_min + (data.r_max - data.r_min) * i / data.width;
  221.         var z_r = 0, z_i = 0;
  222.         for (iter = 0; z_r*z_r + z_i*z_i < escape && iter < max_iter; iter++) {
  223.             // z -> z^2 + c
  224.             var tmp = z_r*z_r - z_i*z_i + c_r;
  225.             z_i = 2 * z_r * z_i + c_i;
  226.             z_r = tmp;
  227.         }
  228.         if (iter == max_iter) {
  229.             iter = -1;
  230.         }
  231.         data.values.push(iter);
  232.     }
  233.     self.postMessage(data);
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement