Hello everyone

I've written some matlab code that will generate squares (or more exatly the min/max x&y values) and then colour these squares differently according to if there's any overlap or not.

Here's my problem: As the number of squares increases the possibility of non-overlapping squares decreases.

Try running for values between 1 - 10 and see the difference.

So somehow I would like the squares to scatter more somewhat. I have no ideas how to do this

Here's my code if anyone is interested, I know there's improvements to be made.
Code:
function [ ] = rektangelplot( amount )
%REKTANGELPLOT Summary of this function goes here
%   Detailed explanation goes here

%Square generation
x1 = amount*rand(amount, 1);
x2 = amount*rand(amount, 1) + x1;

y1 = amount*rand(amount, 1);
y2 = amount*rand(amount, 1) + y1;

collection = [x1, x2, y1, y2];

%Overlap check

truth_table = zeros(amount, amount);
overlap = 0;
c = 1;
for a = 1:amount
    x1c = (collection(a, 2) + collection(a, 1))/2;
    x1r = x1c - collection(a, 1);
    y1c = (collection(a, 4) + collection(a, 3))/2;
    y1r = y1c - collection(a, 3);
    for b = 1:amount
       if a ~= b
            x2c = (collection(b, 2) + collection(b, 1))/2;
            x2r = x2c - collection(b, 1);
            y2c = (collection(b, 4) + collection(b, 3))/2;
            y2r = y2c - collection(b, 3);
            if(abs(x1c -x2c) < (x1r + x2r) && abs(y1c - y2c) < (y1r + y2r))
                truth_table(a,b) = 1;
                truth_table(b,a) = 1;
                
                overlap_x1 = max(collection(a, 1), collection(b, 1));
                overlap_y1 = max(collection(a, 3), collection(b, 3));
                
                overlap_x2 = min(collection(a, 2), collection(b, 2));
                overlap_y2 = min(collection(a, 4), collection(b, 4));
                
                overlap(c, 1) = overlap_x1;
                overlap(c, 2) = overlap_x2;
                overlap(c, 3) = overlap_y1;
                overlap(c, 4) = overlap_y2;
                
                c = c+1;
            end
        end
    end
end


%Draw squares and overlapses

figure
axis([0 3*amount 0 3*amount]);
hold on

for a = 1:amount
    drawX = collection(a, 1):0.01:collection(a, 2);
    drawY = collection(a, 3):0.01:collection(a, 4);
    
    minY = drawX;
    minY(:) = collection(a, 3);
    
    maxY = drawX;
    maxY(:) = collection(a, 4);
    
    plot(drawX, minY);
    plot(drawX, maxY);
    
    minX = drawY;
    maxX = drawY;
    
    minX(:) = collection(a, 1);
    maxX(:) = collection(a, 2);
    
    plot(minX, drawY);
    plot(maxX, drawY);
end


if overlap ~= 0;
    a = length(overlap(:,1));
    for b = 1:a;
       fill([overlap(b,1) overlap(b,2) overlap(b,2) overlap(b,1)], [overlap(b,3) overlap(b,3) overlap(b,4) overlap(b,4)],'red');
    end
end

for a = 1:amount;                     
     if sum(truth_table(a,:)) == 0;   
         fill([collection(a,1) collection(a,2) collection(a, 2) collection(a,1)] ,[collection(a,3) collection(a,3) collection(a,4) collection(a,4)],'green');
    end
end


hold off
Help much appreciated