Changeset 18228
- Timestamp:
- 06/18/15 12:18:26 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FACT++_scripts_refactoring/Hist2D.js
r16861 r18228 1 1 'use strict'; 2 2 3 function Hist2D(nx, xmin, xmax, ny, ymin, ymax) 4 { 5 var arr = new Array(nx); 3 if (!("Hist2D" in this)){ 4 var Hist2D = { 6 5 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)); 6 Hist2D : function(nx, xmin, xmax, ny, ymin, ymax) 7 { 8 var arr = new Array(nx); 11 9 12 if (!arr[ix]) 13 return 0; 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 14 15 if (!arr[ix][iy])16 return 0;15 if (!arr[ix]) 16 return 0; 17 17 18 return arr[ix][iy];19 }18 if (!arr[ix][iy]) 19 return 0; 20 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 } 21 return arr[ix][iy]; 73 22 } 74 23 75 sum += sy; 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)); 76 28 77 console.out(line+"|%4d".$(sy));78 }29 if (ix<0 || ix>=nx || iy<0 || iy>=ny) 30 return false; 79 31 80 console.out(line2); 32 if (!arr[ix]) 33 arr[ix] = new Array(ny); 81 34 82 line = " |"; 83 for (var ix=0; ix<nx; ix++) 84 line += " %4d".$(sx[ix]); 35 if (!arr[ix][iy]) 36 arr[ix][iy] = 0; 85 37 86 console.out(line+"|%4d".$(sum), ""); 87 } 38 arr[ix][iy] += w ? w : 1; 88 39 89 return arr; 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 }; 90 95 } 96 else{ 97 console.out("multiple include of 'Hist2D.js'") 98 } 99 100
Note:
See TracChangeset
for help on using the changeset viewer.