Published
Edited
Apr 16, 2018
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
center = { return { x: wall.w/2, y: wall.h/2 } }
Insert cell
Insert cell
Insert cell
function collisionCheck(base_rect, new_rect) {
let padding = 0.5
if (base_rect.x < new_rect.x - padding + new_rect.w + padding * 2 &&
base_rect.x + base_rect.w > new_rect.x - padding &&
base_rect.y < new_rect.y - padding + new_rect.h + padding * 2 &&
base_rect.h + base_rect.y > new_rect.y - padding) {
return true
} else {
return false
}
}
Insert cell
function findNext() {
let dist = 0.01
let angle = 0;
while (true) {
// Spiral code from https://stackoverflow.com/questions/6824391/drawing-a-spiral-on-an-html-canvas-using-javascript
let incr = angle ? (1/(dist + dist * angle))/2 : dist;
let x = (dist + dist * angle) * Math.cos(angle) + center.x;
let y = (dist + dist * angle) * Math.sin(angle) + center.y;
let temp_w = Math.random() * 10 + 5
let temp_h = Math.random() * 10 + 5
let temp_rect = {x: x - temp_w/2, y: y - temp_h/2, w: temp_w, h: temp_h}
let checks = []
for (let pict of state.pictures) {
let check = collisionCheck(pict, temp_rect)
checks.push(!check)
}
if (checks.every(val => val === true)) {
state.pictures.push(temp_rect)
break;
}
angle += incr
}
}
Insert cell
drawCanvas = function() {
ctx.clearRect(0, 0, m(wall.w), m(wall.h))
state.pictures = []
for (let i=0; i<number_of_pictures; i++) {
findNext()
}
for (let rect of state.pictures) {
ctx.fillStyle = 'black'
ctx.fillRect(m(rect.x), m(rect.y), m(rect.w), m(rect.h))
}
}
Insert cell
Insert cell
Insert cell
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