xxxxxxxxxx
57
var w = 10; // width of chart
var h = 10; // height of chart
var extra = 3;
var numLine = [];
function setup() {
createCanvas(800, 800);
textAlign(LEFT, TOP);
stroke(0);
noLoop();
}
function draw() {
background(220);
let dx = width/w;
let dy = height/h;
textSize(dx/3);
// initialize array values to 0
for(let i = 0; i < w*h*extra; i++) {
numLine[i] = 0;
}
// count how many times each number shows up
for(let y = 0; y < h*extra; y++) {
for(let x = 0; x < w*extra; x++) {
text(y*w + x, x*dx, y*dy);
numLine[5*x+8*y]++;
}
}
let max = 0;
for(let i = 0; i < numLine.length; i++) {
if(numLine[i] > max)
max = numLine[i];
}
// color each number based on how many times it shows up
for(let y = 0; y < h; y++) {
for(let x = 0; x < w; x++) {
let index = y*w + x;
fill(255, 0, 0, map(numLine[index], 0, max, 0, 255));
rect(x*dx, y*dy, dx, dy);
}
}
fill(0);
// print out numbers on screen
for(let y = 0; y < h; y++) {
for(let x = 0; x < w; x++) {
text(y*w + x, x*dx, y*dy);
}
}
console.log(max);
} //end of draw