source: branches/FACT++_scripts_refactoring/Hist2D.js@ 19094

Last change on this file since 19094 was 18228, checked in by dneise, 9 years ago
turned into library
File size: 2.4 KB
Line 
1'use strict';
2
3if (!("Hist2D" in this)){
4 var Hist2D = {
5
6 Hist2D : function(nx, xmin, xmax, ny, ymin, ymax)
7 {
8 var arr = new Array(nx);
9
10 arr.get = function(x, y)
11 {
12 var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
13 var iy = parseInt(ny*(y-ymin)/(ymax-ymin));
14
15 if (!arr[ix])
16 return 0;
17
18 if (!arr[ix][iy])
19 return 0;
20
21 return arr[ix][iy];
22 }
23
24 arr.fill = function(x, y, w)
25 {
26 var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
27 var iy = parseInt(ny*(y-ymin)/(ymax-ymin));
28
29 if (ix<0 || ix>=nx || iy<0 || iy>=ny)
30 return false;
31
32 if (!arr[ix])
33 arr[ix] = new Array(ny);
34
35 if (!arr[ix][iy])
36 arr[ix][iy] = 0;
37
38 arr[ix][iy] += w ? w : 1;
39
40 return true;
41 }
42
43 arr.print = function()
44 {
45 var line1 = " |";
46 for (var ix=0; ix<nx; ix++)
47 line1 += " %3d ".$(ix);
48
49 var line2 = "---+";
50 for (var ix=0; ix<nx; ix++)
51 line2 += "+----";
52 line2+="+----";
53
54 console.out("", line1, line2);
55
56 var sum = 0;
57 var sx = [];
58 for (var ix=0; ix<nx; ix++)
59 sx[ix] = 0;
60
61 for (var iy=ny-1; iy>=0; iy--)
62 {
63 var line = "%3d|".$(iy);
64
65 var sy = 0;
66 for (var ix=0; ix<nx; ix++)
67 {
68 var val = arr[ix] ? arr[ix][iy] : "";
69 line += " %4s".$(val?val:"");
70
71 if (arr[ix])
72 {
73 sy += val?val:0;
74 sx[ix] += val?val:0;
75 }
76 }
77
78 sum += sy;
79
80 console.out(line+"|%4d".$(sy));
81 }
82
83 console.out(line2);
84
85 line = " |";
86 for (var ix=0; ix<nx; ix++)
87 line += " %4d".$(sx[ix]);
88
89 console.out(line+"|%4d".$(sum), "");
90 }
91
92 return arr;
93 },
94 };
95}
96else{
97 console.out("multiple include of 'Hist2D.js'")
98}
99
100
Note: See TracBrowser for help on using the repository browser.