Public
Edited
Sep 25, 2023
Insert cell
Insert cell
paolo = {
const col_top = ['#f36','#f60','#f93','#fc3','#ff0','#6f0','#6c6','#393','#666','#c33'];
const col_down = ['#000','#333','#666','#999','#ccc','#fff','#ccc','#999','#666','#333'];
const u = 5; // unit
const wu = 100; // no. units on width
const hu = 100; // no. units on height
const margin = 20;
const board_height = hu*u + 2*margin;
const board_width = wu*u + 2*margin;
let data_top, data_down, new_top, new_down, r, l, x, y, digit_top, digit_down;
// Background
const { canvas, context } = createCanvas(board_width, board_height);
const pixelRatio = resize(canvas, board_width, board_height, );
context.scale(pixelRatio, pixelRatio);
context.globalAlpha = 1;
context.fillStyle = '#fff';
context.fillRect(0, 0, board_height, board_width);
///////////////// DATA /////////////////
const base = get_pi_txt(wu+1);
data_top = [[]];
data_down = [[]];
data_top[0] = base;
data_down[0] = base;

for (let h = 1; h < hu; ++h){
data_top[h] = [];
data_down[h] = [];
for (let w = 0; w < wu-h; ++w) {
l = data_top[h-1][w];
r = data_top[h-1][w+1];
new_top = algo(l, r);
//l = data_down[h-1][w];
//r = data_down[h-1][w+1];
//new_down = algo(l, r, 10);
data_top[h][w] = new_top;
data_down[h][w] = new_top;
}
}
//////////////////// DRAW ////////////////
for (let h = 0; h < hu; ++h){
for (let w = 0; w < wu-h; ++w) {
///// TOP
digit_top = data_top[h][w];
x = w*u + margin;
y = w*u + h*u + margin;
context.fillStyle = col_top[digit_top];
context.fillRect(x, y, u, u);
///// DOWN
if(h!=0){
digit_down = data_down[h][w];
x = w*u + h*u + margin;
y = w*u + margin;
context.fillStyle = col_down[digit_down];
context.fillRect(x, y, u, u);
}
}
}
return canvas;
}
Insert cell
algo = (l, r) => {
let modulo = 10;
let new_d;
l = l +1;
r = r +1;
let diff = Math.abs(l-r);
if(diff == 0){
new_d = l;
} else {
new_d = diff;
if(l>1 & l>1){
let small = Math.min(l,r);
let big = Math.max(l,r);
let mod = big % small;
if(mod == 0){ new_d = small;}
else{ new_d = small + mod;}
}
}
return new_d % modulo;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
get_pi_txt(10)
Insert cell
12%1
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more