Public
Edited
Apr 22, 2024
Insert cell
Insert cell
guido = {
//let col_top = ['#fff','#ccc','#666','#333','#000','#000','#444','#888','#aaa','#ccc'];
let col_top = ['#fff','#cfc','#696','#030','#363','#363','#474','#8a8','#aea','#cfc'];
let col_down = col_top;
const u = 5; // unit
//const wu = 320; // no. units on width
//const hu = 320; // no. units on height
const wu = 158; // no. units on width
const hu = 158; // no. units on height
const margin = 4*u;
const board_height = hu*u + 2*margin;
//const board_width = wu*u + 2*margin;
const board_width = 109*u + 2*margin;
let data_top, data_down, new_top, new_down, r, l, x, y, digit_top, digit_down;
let shift = 2;
// 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 = '#000';
context.fillRect(0, 0, board_width, board_height);
///////////////// 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, 10);
//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]+shift)%10;
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] + 2)%10;
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 diff = Math.abs(l-r);
let new_d;
if(diff == 0){
new_d = l-1;
} 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 = big -1;}
}
}
return new_d;
}
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