Advertisement
elvecent

Simple heat transfer simulation

Jul 10th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shuffle(array) {
  2.     var m = array.length, t, i;
  3.     while (m) {
  4.         i = Math.floor(Math.random() * m--);
  5.         t = array[m];
  6.         array[m] = array[i];
  7.         array[i] = t;
  8.     }
  9.     return array;
  10. }
  11. var Grid = (function () {
  12.     function Grid(width, height) {
  13.         this.width = width;
  14.         this.height = height;
  15.         this.content = new Array(width);
  16.         var i;
  17.         for (i = 0; i < this.width; i++)
  18.             this.content[i] = new Array(height);
  19.         this.reset();
  20.         this.traversal = 0;
  21.     }
  22.     Grid.prototype.reset = function () {
  23.         var i;
  24.         var j;
  25.         for (i = 0; i < this.width; i++)
  26.             for (j = 0; j < this.height; j++)
  27.                 this.content[i][j] = 0;
  28.     };
  29.     Grid.prototype.draw = function (ctx) {
  30.         var i;
  31.         var j;
  32.         for (i = 0; i < this.width; i++)
  33.             for (j = 0; j < this.height; j++) {
  34.                 ctx.fillStyle = "rgb(" + this.content[i][j] + ",0,0)";
  35.                 ctx.fillRect(i * GRID_STEP, j * GRID_STEP, GRID_STEP, GRID_STEP);
  36.             }
  37.     };
  38.     Grid.prototype.adjacent = function (i, j) {
  39.         var res = [];
  40.         if (i > 0)
  41.             res.push([i - 1, j]);
  42.         if (i < this.width - 1)
  43.             res.push([i + 1, j]);
  44.         if (j > 0)
  45.             res.push([i, j - 1]);
  46.         if (j < this.height - 1)
  47.             res.push([i, j + 1]);
  48.         return res;
  49.     };
  50.     Grid.prototype.get = function (p) {
  51.         return this.content[p[0]][p[1]];
  52.     };
  53.     Grid.prototype.set = function (p, n) {
  54.         this.content[p[0]][p[1]] = n;
  55.     };
  56.     Grid.prototype.processCell = function (i, j) {
  57.         var adj = this.adjacent(i, j);
  58.         var min = 255;
  59.         var p;
  60.         for (p = 0; p < adj.length; p++) {
  61.             if (this.get(adj[p]) < min)
  62.                 min = this.get(adj[p]);
  63.         }
  64.         var adjMin = adj.filter(function (p) { return this.get(p) == min; }, this);
  65.         if (adjMin != [] && this.content[i][j] > 0 && min < 255) {
  66.             var delta = Math.floor((this.content[i][j] - min) / 2);
  67.             var r = shuffle(adjMin)[0];
  68.             this.set(r, this.get(r) + delta);
  69.             this.content[i][j] -= delta;
  70.         }
  71.     };
  72.     Grid.prototype.dissipate = function () {
  73.         var i;
  74.         var j;
  75.         if (this.traversal == 0) {
  76.             for (i = this.width - 1; i >= 0; i--)
  77.                 for (j = this.height - 1; j >= 0; j--)
  78.                     this.processCell(i, j);
  79.         }
  80.         else if (this.traversal == 1) {
  81.             for (i = 0; i < this.width; i++)
  82.                 for (j = 0; j < this.height; j++)
  83.                     this.processCell(i, j);
  84.         }
  85.         else if (this.traversal == 2) {
  86.             for (i = 0; i < this.width; i++)
  87.                 for (j = this.height - 1; j >= 0; j--)
  88.                     this.processCell(i, j);
  89.         }
  90.         else {
  91.             for (i = this.width - 1; i >= 0; i--)
  92.                 for (j = 0; j < this.height; j++)
  93.                     this.processCell(i, j);
  94.         }
  95.         this.traversal = (this.traversal + 1) % 4;
  96.     };
  97.     return Grid;
  98. }());
  99. var GRID_STEP = 10;
  100. var w = Math.floor(window.innerWidth / (GRID_STEP * 2));
  101. var h = Math.floor(window.innerHeight / (GRID_STEP + 1));
  102. function canvasInit() {
  103.     var canvas = document.getElementById("canvas");
  104.     canvas.width = w * GRID_STEP;
  105.     canvas.height = h * GRID_STEP;
  106.     return canvas;
  107. }
  108. var canvas = canvasInit();
  109. var ctx = canvas.getContext("2d");
  110. var grid = new Grid(w, h);
  111. var mouseDown = false;
  112. function onMouseMove(e) {
  113.     if (mouseDown) {
  114.         var x = e.pageX - canvas.offsetLeft;
  115.         var y = e.pageY - canvas.offsetTop;
  116.         var i = Math.floor(x / GRID_STEP);
  117.         var j = Math.floor(y / GRID_STEP);
  118.         grid.content[i][j] = 255;
  119.         grid.draw(ctx);
  120.     }
  121. }
  122. function onMouseDown(e) {
  123.     mouseDown = true;
  124. }
  125. function onMouseUp(e) {
  126.     mouseDown = false;
  127. }
  128. function onKeyDown(e) {
  129.     if (e.keyCode == 32) {
  130.         grid.dissipate();
  131.         grid.draw(ctx);
  132.     }
  133. }
  134. grid.draw(ctx);
  135. canvas.addEventListener("mousedown", onMouseDown);
  136. canvas.addEventListener("mouseup", onMouseUp);
  137. canvas.addEventListener("mousemove", onMouseMove);
  138. window.onkeydown = onKeyDown;
  139.  
  140.  
  141. <html>
  142.     <head>
  143.         <title>code</title>
  144.     </head>
  145.     <body>
  146.         <p align=center>
  147.             <canvas id=canvas></canvas>
  148.         </p>
  149.         <style type="text/css">
  150.             canvas { display:block; }
  151.         </style>
  152.         <script type="text/javascript" src="main.js"></script>
  153.     </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement