function plot_design( X, R )
% function plot_design( X, R )
% X : matrix d*n containg n points x(i), i = 1,..., n, all in [0,1]^2
% plots the square [0,1]^2, the n points (small squares) and circles of radius R centered
% at the points
%-----
% Copyright 2021 CNRS [L. Pronzato]
%
% Redistribution and use in source and binary forms, with or without modification, 
% are permitted provided that the following conditions are met:
% 
% 1. Redistributions of source code must retain the above copyright notice, 
% this list of conditions and the following disclaimer.
% 
% 2. Redistributions in binary form must reproduce the above copyright notice, 
% this list of conditions and the following disclaimer in the documentation and/or 
% other materials provided with the distribution.
% 
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
% EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
% OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
% SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
% DAMAGE.

lw_circle=2; % LineWidth of circles

[d,n]=size(X);
plot([0 1 1 0 0],[0 0 1 1 0],'r-','LineWidth',2),  axis('square'), axis('equal'), axis off, hold on

if R>0
    Theta=0:0.01:2*pi; xcircle=R*cos(Theta); ycircle=R*sin(Theta); LTheta=length(Theta);
end    
for i=1:n
    cote=0.015;
    Sqi=[X(1,i)-cote X(1,i)+cote X(1,i)+cote X(1,i)-cote X(1,i)-cote;
        X(2,i)-cote X(2,i)-cote X(2,i)+cote X(2,i)+cote X(2,i)-cote];
    fill(Sqi(1,:),Sqi(2,:),'r')
    
    if R>0
        xi_circle=X(1,i)+xcircle; yi_circle=X(2,i)+ycircle;
        good_index=find(xi_circle<=1 & xi_circle>=0 & yi_circle<=1 & yi_circle>=0);
        LGI=length(good_index);

        % we must make the plot look nice by avoiding closing a cricle when it
        % should not
        if LGI==LTheta | good_index(1)>1
            % all points are good
            plot(xi_circle(good_index),yi_circle(good_index),'b-','LineWidth',lw_circle);
        else 
            % there is a hole within good points, we must plot in two parts
            % find the gap
            GI1=good_index(1:LGI-1); GI2=good_index(2:LGI); DGI=GI2-GI1; gap=find(DGI>1);
            if isempty(gap), gap=LGI; end
            plot(xi_circle(good_index(1:gap)),yi_circle(good_index(1:gap)),'b-','LineWidth',lw_circle);
            plot(xi_circle(good_index(gap+1:LGI)),yi_circle(good_index(gap+1:LGI)),'b-','LineWidth',lw_circle);
        end
    end
    %plot(X(1,i)+xcircle,X(2,i)+ycircle,'b-','LineWidth',1)
    %set(gcf,'DefaultLineMarkerSize',5)

end    
   
hold off
end

