let randInt = (a, b) => (floor(random(a, b)));
DSatur("col", ["#2b67af", "#f9d531", "#ef562f", "#29ac9f", "#f589a3"], true);
for (let recta of rectangles) {
for (let recta of rectangles) {
function createComposition() {
for (let i = 0; i < nRects; i++) {
let newRecta = generateRectangle(rectangles);
if (newRecta.si > 1 || newRecta.sj > 1) {
rectangles.push(newRecta);
for (let i = margin; i < N-margin; i++) {
for (let j = margin; j < N-margin; j++) {
for (let recta of rectangles) {
if (rectanglesIntersect(newRecta, recta)) {
rectangles.push(newRecta);
function rectanglesIntersect(recta1, recta2) {
return ((recta1.i <= recta2.i && recta1.i+recta1.si > recta2.i) || (recta2.i <= recta1.i && recta2.i+recta2.si > recta1.i)) && ((recta1.j <= recta2.j && recta1.j+recta1.sj > recta2.j) || (recta2.j <= recta1.j && recta2.j+recta2.sj > recta1.j));
function generateRectangle(rectangles) {
let i = randInt(margin, N-margin);
let j = randInt(margin, N-margin);
if (rectangles.length == 0) {
s = min(maxSize, N-i-margin-1, N-j-margin-1);
for (let recta of rectangles) {
if (i+s > N-margin || j+s > N-margin || s > maxSize || rectanglesIntersect({i: i, j: j, si: s, sj: s}, recta)) {
function createConnections() {
let l = rectangles.length;
for (let i = 0; i < l; i++) {
let recta1 = rectangles[i];
if (!recta1.neighbors) recta1.neighbors = [];
for (let j = 0; j < l; j++) {
let recta2 = rectangles[j];
if (!recta2.neighbors) recta2.neighbors = [];
if (i != j && rectanglesTouch(recta1, recta2)) {
recta1.neighbors.push(recta2);
function rectanglesTouch(recta1, recta2) {
if (((recta1.i <= recta2.i && recta1.i+recta1.si == recta2.i) || (recta2.i <= recta1.i && recta2.i+recta2.si == recta1.i)) && ((recta1.j <= recta2.j && recta1.j+recta1.sj > recta2.j) || (recta2.j <= recta1.j && recta2.j+recta2.sj > recta1.j)))
if (((recta1.i <= recta2.i && recta1.i+recta1.si > recta2.i) || (recta2.i <= recta1.i && recta2.i+recta2.si > recta1.i)) && ((recta1.j <= recta2.j && recta1.j+recta1.sj == recta2.j) || (recta2.j <= recta1.j && recta2.j+recta2.sj == recta1.j)))
function DSatur(el, elements, shuffleIt) {
for (let i = 0; i < rectangles.length; i++) {
let v, vSat = -1, vElementsUsed;
for (let recta of rectangles) {
let elementsUsed = elementsUsedByNeighbors(recta, el);
let sat = elementsUsed.length;
vElementsUsed = elementsUsed;
} else if (sat == vSat) {
if (largestDegreeInTheSubgraphInducedByTheUncoloredVertices(recta, el) > largestDegreeInTheSubgraphInducedByTheUncoloredVertices(v, el)) {
vElementsUsed = elementsUsed;
shuffle(elements, shuffleIt);
for (let element of elements) {
if (vElementsUsed.indexOf(element) == -1) {
function elementsUsedByNeighbors(v, el) {
for (let neigh of v.neighbors) {
if (neigh[el]) elementsUsed.push(neigh[el]);
return [...new Set(elementsUsed)];
function largestDegreeInTheSubgraphInducedByTheUncoloredVertices(v, el) {
for (let neigh of v.neighbors) {
let sat = elementsUsedByNeighbors(neigh, el);
if (sat > largestDegree) largestDegree = sat;
function drawRectangle(recta) {
let x = recta.i*u + gap/2, y = recta.j*u + gap/2;
let w = recta.si*u - gap, h = recta.sj*u - gap;
function drawConnections(recta1) {
let x1 = recta1.i*u + recta1.si*u/2, y1 = recta1.j*u + recta1.sj*u/2;
for (let recta2 of recta1.neighbors) {
let x2 = recta2.i*u + recta2.si*u/2, y2 = recta2.j*u + recta2.sj*u/2;