let globalPointSize = 20;
let pointSizeDecrement = 0.25;
img = loadImage("flower1.jpg");
createCanvas(img.width/3, img.height/3);
if(globalPointSize <= minPointSize) {
let cellWidth = width / gridSize;
let cellHeight = height / gridSize;
for(let i = 0; i < gridSize; i++) {
for(let j = 0; j < gridSize; j++) {
let centerNode = new makeNode(null, {x: i * cellWidth + cellWidth / 2, y: j * cellHeight + cellHeight / 2}, globalPointSize, maxDepth);
allNodes.push(centerNode);
for(let n = 0; n < allNodes.length; n++){
pointSizeDecrement = globalPointSize / 200;
if(pointSizeDecrement < 0.001) pointSizeDecrement = 0.01;
globalPointSize -= pointSizeDecrement;
function makeNode(parentNode, position, radius, depth){
this.parentNode = parentNode;
this.position = position;
let newRadius = this.radius * 0.9;
if(newRadius < minPointSize || this.depth <= 0) return;
let angle = random(TWO_PI);
let newX = this.position.x + this.radius * cos(angle);
let newY = this.position.y + this.radius * sin(angle);
let newNode = new makeNode(this, {x: newX, y: newY}, newRadius, this.depth - 1);
for(let n = 0; n < allNodes.length; n++){
if(newNode.intersects(allNodes[n])){
this.childrenNodes.push(newNode);
this.intersects = function(otherNode){
let dist = sqrt(sq(this.position.x - otherNode.position.x) + sq(this.position.y - otherNode.position.y));
return (this.id != otherNode.id &&
dist < (this.radius + otherNode.radius) * 0.001 &&
!this.childrenNodes.includes(otherNode) &&
(!this.parentNode || !this.parentNode.childrenNodes.includes(otherNode)));
this.display = function() {
let imgX = floor(map(this.position.x, 0, width, 0, img.width));
let imgY = floor(map(this.position.y, 0, height, 0, img.height));
let index = 4 * (imgY * img.width + imgX);
if(index < 0 || index >= img.pixels.length - 2) return;
let col = [img.pixels[index], img.pixels[index+1], img.pixels[index+2], img.pixels[index+3]];
if(col.some(isNaN)) return;
ellipse(this.position.x, this.position.y, this.radius, this.radius);
line(this.position.x, this.position.y, this.parentNode.position.x, this.parentNode.position.y);
saveCanvas('pointil_gspack_bb-' + floor(random(100)), 'png');