xxxxxxxxxx
// Tariq Rashid
// space filling algorithm v2
// http://makeyourownalgorithmicart.blogspot.co.uk/2018/04/space-filling-dots-part-22.html
var array_of_dots = [];
var array_size = 120;
var draw_size = 500;
var max_radius = 7;
var min_radius = 0.2;
var gap = 0.2;
// outer circle
var outer_circle_x = array_size * 0.5;
var outer_circle_y = array_size * 0.5;
var outer_circle_r = array_size * 0.4;
function setup() {
createCanvas(800, 600);
background('white');
colorMode(HSB);
noLoop();
noStroke();
// initialise array of dots
for (x = 0; x < array_size; x += 1) {
for (y = 0; y < array_size; y += 1) {
// inital dots size 0
array_of_dots[x*array_size + y] = new Dot(x, y, 0);
}
}
// shuffle list
shuffleArray(array_of_dots);
}
function draw() {
// work through grid
for (dot1 of array_of_dots) {
// place dot if not covered
if (dot1.covered == false) {
// new range taking into accuont gap
var radius_range = dot1.nearest - gap;
// only draw if there is space left after gap
if (radius_range > 0) {
var candidate_radius = random(Math.min(radius_range, max_radius));
// only proceed if dot is entirely within boundary circle
if (((candidate_radius + dist(dot1.x, dot1.y, outer_circle_x, outer_circle_y)) < outer_circle_r) && (candidate_radius > min_radius)) {
dot1.radius = candidate_radius;
// update nearest measures for other dots
for (dot2 of array_of_dots) {
// ignore comparing the same ones
if (dot1 != dot2) {
var d = dist(dot1.x, dot1.y, dot2.x, dot2.y);
if (d < (dot1.radius + dot2.radius)) {
// dot2 is covered
dot2.covered = true;
} else {
// dot2 isn't covered, update nearest
dot2.nearest = Math.min(dot2.nearest, d - dot1.radius);
}
}
} // for dot2
} // if candidate_radius
} // if radius_range
}
} // for dot1
// draw dots
for (dot of array_of_dots) {
dot.draw();
}
// border
noFill(); stroke(60); rect(0,0,width-1, height-1);
}
// dot object
function Dot(x, y, r) {
//this.x = x;
//this.y = y;
this.x = x + random(-0.5, 0.5);
this.y = y + random(-0.5, 0.5);
this.radius = r;
this.covered = false;
this.nearest = 500;
this.hue = (random(210, 270) + 360) % 360;
this.draw = function() {
var x_coord = map(this.x, 0, array_size, width/2 - draw_size/2, width/2 + draw_size/2);
var y_coord = map(this.y, 0, array_size, height/2 + draw_size/2, height/2 - draw_size/2);
if (this.radius != 0) {
fill(this.hue, 80, 95);
ellipse(x_coord, y_coord, 2*this.radius * draw_size/array_size);
}
}
}
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}