Public
Edited
Jul 15, 2023
Insert cell
Insert cell
rectangles = new Array(12).fill(0).map((_, i) => {
let x = Math.random() * width;
let y = Math.random() * height;

let roundedX = Math.round(x / 30) * 30;
let roundedY = Math.round(y / 30) * 30;

return {
x: roundedX,
y: roundedY,

targetX: roundedX,
targetY: roundedY,

width: Math.round(Math.random() * 4 + 2) * increment,
height: Math.round(Math.random() * 6 + 2) * increment
};
})
Insert cell
increment = 30
Insert cell
width = 960
Insert cell
height = 540
Insert cell
grid = new Array(height / increment).fill(0).map((_, i) => {
return new Array(width / increment).fill(0).map((_, j) => {
return false;
});
})
Insert cell
function addRectangles(rectangles) {
let grid = createGrid();

for (let i = 0; i < rectangles.length; i++) {
let rectangle = rectangles[i];

for (let j = 0; j < rectangle.width / 30; j++) {
for (let k = 0; k < rectangle.height / 30; k++) {
let x = rectangle.x / 30 + j;
let y = rectangle.y / 30 + k;

if (grid[y] !== undefined && grid[y][x] !== undefined) {
grid[y][x] = true;
}
}
}
}

return grid;
}
Insert cell
addRectangles(rectangles)
Insert cell
function createGrid() {
let grid = new Array(height / 30).fill(0).map((_, i) => {
return new Array(width / 30).fill(0).map((_, j) => {
return false;
});
});

return grid;
}
Insert cell
toText(addRectangles(rectangles))
Insert cell
function toText(grid) {
let text = "";
for (let i = 0; i < grid.length; i++) {
let row = grid[i];
for (let j = 0; j < row.length; j++) {
text += row[j] ? "X" : "_";
}
text += "\n";
}

return text;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more