1 'use strict';
  2 
  3 function Hist2D(nx, xmin, xmax, ny, ymin, ymax)
  4 {
  5     var arr = new Array(nx);
  6 
  7     arr.get = function(x, y)
  8     {
  9         var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
 10         var iy = parseInt(ny*(y-ymin)/(ymax-ymin));
 11 
 12         if (!arr[ix])
 13             return 0;
 14 
 15         if (!arr[ix][iy])
 16             return 0;
 17 
 18         return arr[ix][iy];
 19     }
 20 
 21     arr.fill = function(x, y, w)
 22     {
 23         var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
 24         var iy = parseInt(ny*(y-ymin)/(ymax-ymin));
 25 
 26         if (ix<0 || ix>=nx || iy<0 || iy>=ny)
 27             return false;
 28 
 29         if (!arr[ix])
 30             arr[ix] = new Array(ny);
 31 
 32         if (!arr[ix][iy])
 33             arr[ix][iy] = 0;
 34 
 35         arr[ix][iy] += w ? w : 1;
 36 
 37         return true;
 38     }
 39 
 40     arr.print = function()
 41     {
 42         var line1 = "   |";
 43         for (var ix=0; ix<nx; ix++)
 44             line1 += " %3d ".$(ix);
 45 
 46         var line2 = "---+";
 47         for (var ix=0; ix<nx; ix++)
 48             line2 += "+----";
 49         line2+="+----";
 50 
 51         console.out("", line1, line2);
 52 
 53         var sum = 0;
 54         var sx = [];
 55         for (var ix=0; ix<nx; ix++)
 56             sx[ix] = 0;
 57 
 58         for (var iy=ny-1; iy>=0; iy--)
 59         {
 60             var line = "%3d|".$(iy);
 61 
 62             var sy = 0;
 63             for (var ix=0; ix<nx; ix++)
 64             {
 65                 var val = arr[ix] ? arr[ix][iy] : "";
 66                 line += " %4s".$(val?val:"");
 67 
 68                 if (arr[ix])
 69                 {
 70                     sy     += val?val:0;
 71                     sx[ix] += val?val:0;
 72                 }
 73             }
 74 
 75             sum += sy;
 76 
 77             console.out(line+"|%4d".$(sy));
 78         }
 79 
 80         console.out(line2);
 81 
 82         line = "   |";
 83         for (var ix=0; ix<nx; ix++)
 84             line += " %4d".$(sx[ix]);
 85 
 86         console.out(line+"|%4d".$(sum), "");
 87     }
 88 
 89     return arr;
 90 }
 91