
function main(graph,radius,pathlossexp,variance_shadow_fading,PTxdb,bandwidth,pos_max,antenna_gain,reuse,plot_en,webdemo_en,filename)
%Example:main(1,1,3,6,20,180,10,1,3,[0 1 0],1,'frequency_reuse')
%radius=1;%radius of the cell
%variance_shadow_fading = 3; % Variance of underlying Gaussian random variables
%PTx=43 dBm;%power from base station per user.
%bandwidth=180; %bandwidth in KHz for 12 subcarries each of 15khz
%pos_max=100;%Maximum number of users in an cell.
%antenna_gain=1;%antenna gain
%reuse=3;% select the reuse factor
%plot_en=[1 0 0];  %enable plot 0,1 [reuse pattren, CDF of SNIR,CDF of Interference]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % BEGIN, set up graphical output

  % Create an invisible figure.
fh=figure(1);
set(fh,"visible","off");
hold on


  landscape = "-S930,350";
  portrait  = "-S640,480";

  output_format = portrait ;     % default


  set(0, "defaultlinelinewidth", 2);
%webdemo_en=1;
  if (webdemo_en)
    set(0, "defaultaxesfontsize", 8);
   set(0, "defaultaxesfontname", "Helvetica");
    set(0, "defaulttextfontsize", 8);
   set(0, "defaulttextfontname", "Helvetica");
    const_lw = 2;
    const_msize = 4;
  else
    set(0, "defaultaxesfontsize", 10);
    set(0, "defaultaxesfontname", "Helvetica");
    set(0, "defaulttextfontsize", 8);
    set(0, "defaulttextfontname", "Helvetica");
    const_lw = 2;
    const_msize = 2;
  end
% END, set up graphical output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% RGB color definitions, http://www.rapidtables.com/web/color/RGB_Color.htm;   use e.g.  as  magenta(7,:)
  grayc =   [[0,0,0]; [32,32,32]; [64,64,64]; [96,96,96]; [128,128,128]; [160,160,160]; [192,192,192]; [224,224,224]; [255,255,255]]/255;
  violett = [[51,0,25]; [102,0,51]; [153,0,76]; [204,0,102]; [255,0,127]; [255,51,153]; [255,102,178]; [255,153,204]; [255,204,229]]/255;
  magenta = [[51,0,51]; [102,0,102]; [153,0,153]; [204,0,204]; [255,0,255]; [255,51,255]; [255,102,255]; [255,153,255]; [255,204,255]]/255;
  lilac   = [[25,0,51]; [51,0,102]; [76,0,153]; [102,0,204]; [127,0,255]; [153,51,255]; [178,102,255]; [204,153,255]; [229,204,255]]/255;
  blue    = [[0,0,51]; [0,0,102]; [0,0,153]; [0,0,204]; [0,0,255]; [51,51,255]; [102,102,255]; [153,153,255]; [204,204,255]]/255;
  seablue = [[0,25,51]; [0,51,102]; [0,76,153]; [0,102,204]; [0,128,255]; [51,153,255]; [102,178,255]; [153,204,255]; [204,229,255]]/255;
  cyan    = [[0,51,51]; [0,102,102]; [0,153,153]; [0,204,204]; [0,255,255]; [51,255,255]; [102,255,255]; [153,255,255]; [204,255,255]]/255;
  seagreen = [[0,51,25]; [0,102,51]; [0,153,76]; [0,204,102]; [0,255,128]; [51,255,153]; [102,255,178]; [153,255,204]; [204,255,229]]/255;
  green   = [[0,51,0]; [0,102,0]; [0,153,0]; [0,204,0]; [0,255,0]; [51,255,51]; [102,255,102]; [153,255,153]; [204,255,204]]/255;
  lightgreen = [[25,51,0]; [51,102,0]; [76,153,0]; [102,204,0]; [128,255,0]; [153,255,51]; [178,255,102]; [204,255,153]; [229,255,204]]/255;
  yellow  = [[51,51,0]; [102,102,0]; [153,153,0]; [204,204,0]; [255,255,0]; [255,255,51]; [255,255,102]; [255,255,153]; [255,255,204]]/255;
  amber   = [[51,25,0]; [102,51,0]; [153,76,0]; [204,102,0]; [255,128,0]; [255,153,51]; [255,178,102]; [255,204,153]; [255,229,204]]/255;
  red     = [[51,0,0]; [102,0,0]; [153,0,0]; [204,0,0]; [255,0,0]; [255,51,51]; [255,102,102]; [255,153,153]; [255,204,204]]/255;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN check input parameters for consistency

% only one plot at a time

if (sum(plot_en)!=1)
    plot_en = [1 0 0];
    warning("setting  'plot_en = [1 0 0]'");
end

% END check input parameters for consistency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%this is how alpa value is calculated
%carrier frequency
%fc=(2.6)*10.^(9);
%c=(3.8)*10.^(8);
%lamd = c/fc;
%alpa=(lamd/(4*3.14)).^2;







x=0;y=0;r=radius;

x_centers=[x  x-1.5*r x+1.5*r x-1.5*r x+1.5*r x x x+1.5*r x+3*r x+3*r x+3*r x+1.5*r x x-1.5*r x-1.5*r x-3*r x-3*r x-3*r x x+3*r x-3*r];
y_centers=[y y+sqrt(3)*0.5*r y+sqrt(3)*0.5*r y-sqrt(3)*0.5*r y-sqrt(3)*0.5*r y+sqrt(3)*1*r y-sqrt(3)*1*r y+3*sqrt(3)*0.5*r y+sqrt(3)*r y y-sqrt(3)*r y-3*sqrt(3)*0.5*r y-2*sqrt(3)*r y-1.5*sqrt(3)*r y+1.5*sqrt(3)*r y+sqrt(3)*r y-sqrt(3)*r y y+2*sqrt(3)*r y+2*sqrt(3)*r y-2*sqrt(3)*r] ;

x_centers_reuse3=[x-3*r x-3*r x-3*r x-3*r x-1.5*r x-1.5*r x-1.5*r x-1.5*r x x x x x x+1.5*r x+1.5*r x+1.5*r x+1.5*r x+3*r x+3*r x+3*r x+3*r];
y_centers_reuse3=[y+sqrt(3)*r y y-sqrt(3)*1*r y-2*sqrt(3)*r y+1.5*sqrt(3)*r y+0.5*sqrt(3)*r y-0.5*sqrt(3)*r y-1.5*sqrt(3)*r y+2*sqrt(3)*r y+sqrt(3)*r y y-sqrt(3)*r y-2*sqrt(3)*r y+1.5*sqrt(3)*r y+0.5*sqrt(3)*r y-0.5*sqrt(3)*r y-1.5*sqrt(3)*r y+2*sqrt(3)*r y+sqrt(3)*r y y-sqrt(3)*r];

%colors=['r' 'g' 'b' 'c' 'm' 'y' 'w' 'k'];
colors=[[153,255,255]/255;[224,224,224]/255 ; [255,255,153]/255; [255,153,255]/255; [204,153,255]/255; [153,153,255]/255;  [255,153,153]/255;];

n=21; %number of cells
count=0;

if(plot_en(1))
    if reuse==1
        
        for i = 1:n
            x=x_centers_reuse3(i);
            y=y_centers_reuse3(i);
            count=1;
            col=colors(count,:);
		xlabel('x [km]','FontName','Helvetica');
		ylabel('y [km]','FontName','Helvetica');
		xlim([-4.5*radius,4.5*radius]);
		ylim([-4.5*radius,4.5*radius]);
            plotCell(x,y,r,col);
            
        end
        
    elseif reuse==3
        
        
        for i = 1:n
            x=x_centers_reuse3(i);
            y=y_centers_reuse3(i);
            count=count+1;
            col=colors(count,:);
		xlabel('x [km]','FontName','Helvetica');
		ylabel('y [km]','FontName','Helvetica');
		xlim([-4.5*radius,4.5*radius]);
		ylim([-4.5*radius,4.5*radius]);
            plotCell(x,y,r,col);
            if count==3
                count=0;
            end
            
        end
        
    elseif reuse==7
        
        for i = 1:n
            x=x_centers_reuse3(i);
            y=y_centers_reuse3(i);
            count=count+1;
            col=colors(count,:);
		xlabel('x [km]','FontName','Helvetica');
		ylabel('y [km]','FontName','Helvetica');
		xlim([-4.5*radius,4.5*radius]);
		ylim([-4.5*radius,4.5*radius]);
            plotCell(x,y,r,col);
            if count==7
                count=0;
            end
            
        end
        
    end
end

%ploting the users 50m apart
rmin=0;
rmax=1;

[active_user,mspos]=users(rmax,rmin);
mpos = mspos(:,1)+1i* mspos(:,2);
active_user = active_user(:,1)+1i* active_user(:,2);

d=r;
dmin=0.05;

%initialization for the variable used
z_all=[];thetac_all=[];theta_all=[];gain_all=[];rr_all=[];
totalsignalpower_Interference_1x1x1_all=[];
sinr_db_all=[];totalsignalpower_Interference_1x3x1_all=[];
sinr_1x7x1_db_all=[];sinr_1x3x1_db_all=[];totalsignalpower_Interference_1x7x1_all=[];
totalsignalpower_Interference_1x3x1_directional_all=[];
sinr_1x3x1_directional_db_all=[];

%calculating the noise
    %noise
    %'################k'
    K=1.38*10.^(-23);
    %'############temp'
    T=290;
  %'##############bw'
    bw=bandwidth*1000;
   % '#################################### noise'
    Noise=K*T*bw;


%path loss components
alpha=8.43*10^(-5);% path loss    (lambda/4/pi)^2     CONSIDER PATH LOSS EXPONENT!
gamma=-pathlossexp;% pathloss exponent


% converstion from log to linear
	PTx_allusers=10.^(PTxdb/10); %dBm to dB
	PTx=(PTx_allusers/pos_max);
    gain=10.^(antenna_gain/10);

for i=1:pos_max
    %Ploting Random user
    [z] = generate_pos(d,dmin);
    z_all=[z_all;z];
    x_user=real(z);
    y_user=imag(z);
PositionNodes=mpos;
    Dcor=0.05;


    %%%% output N-power in dBmxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    %rr=sqrt((real(z).^2)+(imag(z).^2));% distance of random user from base station
    rr=abs(z);
    %rr_all=[rr_all;rr];

    pl=alpha*rr^(gamma);%path loss
    %shadowfading
    lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);



    %calculating SINR for 1x1x1 frequency reuse using omni directional antenna
     % '##########################signal power'
    signalpower=PTx*gain*shfd*pl;
    %'##################### interference'
    totalsignalpower_Interference_1x1x1 = calculate_interference_omniDirectional_1x1x1(PTx,r,z,variance_shadow_fading,x,y,gamma);
    totalsignalpower_Interference_1x1x1_all = [totalsignalpower_Interference_1x1x1_all;totalsignalpower_Interference_1x1x1];
    %'#################################'
    %'############sinr'
    sinr = signalpower/(Noise+totalsignalpower_Interference_1x1x1);
    sinr_db = 10*log10(sinr);
    sinr_db_all=[sinr_db_all;sinr_db];

    %calculating SINR for 1x3x1 frequency reuse using omni directional

    totalsignalpower_Interference_1x3x1= calculate_interference_omniDirectional_1x3x1(PTx,r,z,variance_shadow_fading,x,y,gamma);
    totalsignalpower_Interference_1x3x1_all = [totalsignalpower_Interference_1x3x1_all;totalsignalpower_Interference_1x3x1];
    sinr_1x3x1 = signalpower/(Noise+totalsignalpower_Interference_1x3x1);
    sinr_1x3x1_db = 10*log10(sinr_1x3x1);
    sinr_1x3x1_db_all=[sinr_1x3x1_db_all;sinr_1x3x1_db];

   %calculating SINR for frequency resue 7 using omni directional

    totalsignalpower_Interference_1x7x1= calculate_interference_omniDirectional_1x7x1(PTx,r,z,variance_shadow_fading,x,y,gamma);
    totalsignalpower_Interference_1x7x1_all = [totalsignalpower_Interference_1x7x1_all;totalsignalpower_Interference_1x7x1];
    sinr_1x7x1 = signalpower/(Noise+totalsignalpower_Interference_1x7x1);
    sinr_1x7x1_db = 10*log10(sinr_1x7x1);
    sinr_1x7x1_db_all=[sinr_1x7x1_db_all;sinr_1x7x1_db];

    %     Angle of the user from directional antenna

    thetac=rad2deg(angle(z));
    theta=thetac-30;
    theta_all=[theta_all;theta];
    antennaGain_1x3x1=10^(1.7-0.1*min(12*theta^2/70^2,20));%1.7 is the antenna gain =17dBi, 0.7 the mobile gain
    signalpower_directionalAntenna=PTx*antennaGain_1x3x1*shfd*pl;
    totalsignalpower_Interference_1x3x1_directional = calculate_interference_Directional_1x3x1(PTx,r,z,variance_shadow_fading,x,y,gamma);
    totalsignalpower_Interference_1x3x1_directional_all = [totalsignalpower_Interference_1x3x1_directional_all;totalsignalpower_Interference_1x3x1_directional];
    sinr_1x3x1_directional = signalpower_directionalAntenna/(Noise+totalsignalpower_Interference_1x3x1_directional);
    sinr_1x3x1_directional_db = 10*log10(sinr_1x3x1_directional);
    sinr_1x3x1_directional_db_all=[sinr_1x3x1_directional_db_all;sinr_1x3x1_directional_db];


end

% plotting all the users in central cell
if(plot_en(1))
    hold on;
axis ("square");
    plot(z_all,'.r');
end






if(plot_en(2))
  if(graph==1)
%plotting effective SINR using frequency reuse 1
'########################graph 1'
    sinr_db_all_plot=sort(sinr_db_all);
    size(sinr_db_all_plot);
    abs_cdf = min(sinr_db_all_plot):0.5:max(sinr_db_all_plot);
    n_elements = histc(sinr_db_all_plot,abs_cdf);
    c_elements = 1/pos_max*cumsum(n_elements);
    hold on;
     %xlabel('SINR [dB]')
     %ylabel('Prob[SINR<x]')

    plot(abs_cdf,c_elements,'LineWidth',2,"o-","markersize",0.5,"color","r");

 %plotting effective SINR using frequency reuse 3

    sinr_1x3x1_db_all_plot=sort(sinr_1x3x1_db_all);
    size(sinr_1x3x1_db_all_plot);
    abs_cdf = min(sinr_1x3x1_db_all_plot):0.5:max(sinr_1x3x1_db_all_plot);
    n_elements = histc(sinr_1x3x1_db_all_plot,abs_cdf);
    c_elements = 1/pos_max*cumsum(n_elements);
    hold on;
    % xlabel('SINR (dB)')
    % ylabel('Prob[SINR < x]')

plot(abs_cdf,c_elements,'LineWidth',2,"x-","markersize",0.5,"color","g");



%plotting effective SINR using frequency reuse 7

	sinr_1x7x1_db_all_plot=sort(sinr_1x7x1_db_all);
	size(sinr_1x7x1_db_all_plot);
	abs_cdf = min(sinr_1x7x1_db_all_plot):0.5:max(sinr_1x7x1_db_all_plot);
	n_elements = histc(sinr_1x7x1_db_all_plot,abs_cdf);
	c_elements = 1/pos_max*cumsum(n_elements);
	hold on;
	% xlabel("SINR (dB)")
	% ylabel("Prob[SINR < x]")

plot(abs_cdf,c_elements,'LineWidth',2,"x-","markersize",0.5,"color","b");



%plotting effective SINR using frequency reuse 3 with directional antenna

sinr_1x3x1_directional_db_plot=sort(sinr_1x3x1_directional_db_all);
size(sinr_1x3x1_directional_db_plot);
abs_cdf = min(sinr_1x3x1_directional_db_plot):0.5:max(sinr_1x3x1_directional_db_plot);
n_elements = histc(sinr_1x3x1_directional_db_plot,abs_cdf);
c_elements = 1/pos_max*cumsum(n_elements);
hold on;
xlabel("SINR [dB]",'FontName','Helvetica')
ylabel("Prob[SINR<x]",'FontName','Helvetica')
xlim([-20,30]);

plot(abs_cdf,c_elements,'LineWidth',2,"o-","markersize",0.5,"color","k");



title('CDF of the effective SINR','FontName','Helvetica');
legend('reuse 1','reuse 3','reuse 7','reuse 3 with sectors','Location','southeast');
end

if(graph==2)


'##########################graph 2'

    sinr_db_all_plot=sort(sinr_db_all);
    size(sinr_db_all_plot);
    abs_cdf = min(sinr_db_all_plot):0.5:max(sinr_db_all_plot);
    n_elements = hist(sinr_db_all_plot,abs_cdf);
    c_elements = 1/pos_max*(n_elements);
    hold on;
     %xlabel('SINR [dB]')
     %ylabel('Prob[SINR<x]')

    plot(abs_cdf,c_elements,'LineWidth',2,"o-","markersize",0.5,"color","r");

 %plotting effective SINR using frequency reuse 3

    sinr_1x3x1_db_all_plot=sort(sinr_1x3x1_db_all);
    size(sinr_1x3x1_db_all_plot);
    abs_cdf = min(sinr_1x3x1_db_all_plot):0.5:max(sinr_1x3x1_db_all_plot);
    n_elements = hist(sinr_1x3x1_db_all_plot,abs_cdf);
    c_elements = 1/pos_max*(n_elements);
    hold on;
    % xlabel('SINR (dB)')
    % ylabel('Prob[SINR < x]')

plot(abs_cdf,c_elements,'LineWidth',2,"x-","markersize",0.5,"color","g");



%plotting effective SINR using frequency reuse 7

	sinr_1x7x1_db_all_plot=sort(sinr_1x7x1_db_all);
	size(sinr_1x7x1_db_all_plot);
	abs_cdf = min(sinr_1x7x1_db_all_plot):0.5:max(sinr_1x7x1_db_all_plot);
	n_elements = hist(sinr_1x7x1_db_all_plot,abs_cdf);
	c_elements = 1/pos_max*(n_elements);
	hold on;
	% xlabel("SINR (dB)")
	% ylabel("Prob[SINR < x]")

plot(abs_cdf,c_elements,'LineWidth',2,"x-","markersize",0.5,"color","b");



%plotting effective SINR using frequency reuse 3 with directional antenna

sinr_1x3x1_directional_db_plot=sort(sinr_1x3x1_directional_db_all);
size(sinr_1x3x1_directional_db_plot);
abs_cdf = min(sinr_1x3x1_directional_db_plot):0.5:max(sinr_1x3x1_directional_db_plot);
n_elements = hist(sinr_1x3x1_directional_db_plot,abs_cdf);
c_elements = 1/pos_max*(n_elements);
hold on;
xlabel("SINR [dB]",'FontName','Helvetica')
ylabel("Prob[SINR<x]",'FontName','Helvetica')
xlim([-20,30]);

plot(abs_cdf,c_elements,'LineWidth',2,"o-","markersize",0.5,"color","k");



title('PDF of the effective SINR','FontName','Helvetica');
legend('reuse 1','reuse 3','reuse 7','reuse 3 with sectors','Location','southeast');

end
end

if(plot_en(3))
    %plotting interference for users using 1x1x1 frequency reuse
    totalsignalpower_Interference_1x1x1_all_db=10*log10(totalsignalpower_Interference_1x1x1_all);
    totalsignalpower_Interference_1x1x1_all_plot=sort(totalsignalpower_Interference_1x1x1_all_db);
    N=size(totalsignalpower_Interference_1x1x1_all_plot,1);
    hold on;

    plot(totalsignalpower_Interference_1x1x1_all_plot,(1:N)/N,'LineWidth',1,"x-","markersize",0.5,"color","r");

%plotting interference for users using 1x3x1 frequency reuse
    totalsignalpower_Interference_1x3x1_all_db=10*log10(totalsignalpower_Interference_1x3x1_all);
    totalsignalpower_Interference_1x3x1_all_plot=sort(totalsignalpower_Interference_1x3x1_all_db);
    N=size(totalsignalpower_Interference_1x3x1_all_plot,1);
    hold on;
    plot(totalsignalpower_Interference_1x3x1_all_plot,(1:N)/N,'LineWidth',2,"x-","markersize",0.5,"color","g");

%plotting interference for users using 1x7x1 frequency reuse
totalsignalpower_Interference_1x7x1_all_db=10*log10(totalsignalpower_Interference_1x7x1_all);
totalsignalpower_Interference_1x7x1_all_plot=sort(totalsignalpower_Interference_1x7x1_all_db);
N=size(totalsignalpower_Interference_1x7x1_all_plot,1);
hold on;
plot(totalsignalpower_Interference_1x7x1_all_plot,(1:N)/N,'LineWidth',2,"x-","markersize",0.5,"color","b");

totalsignalpower_Interference_1x3x1_directional_all_db=10*log10(totalsignalpower_Interference_1x3x1_directional_all);
totalsignalpower_Interference_1x3x1_directional_all_db_plot=sort(totalsignalpower_Interference_1x3x1_directional_all_db);
N=size(totalsignalpower_Interference_1x3x1_directional_all_db_plot,1);
hold on;
plot(totalsignalpower_Interference_1x3x1_directional_all_db_plot,(1:N)/N,'LineWidth',2,"x-","markersize",0.5,"color","k");
xlabel('Inter-Cell Interference (dBm)','FontName','Helvetica');
ylabel('Prob[ICI<x]','FontName','Helvetica');

title('CDF of Interference','FontName','Helvetica');
legend('reuse 1','reuse 3','reuse 7','reuse 3 with sectors','Location','southeast');
xlim([-80,0]);
end



print(filename,"-dpng",output_format,'-F:Helvetica:9');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN, function definitions


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotedcell = plotCell(x,y,r,col)

x_hexagon=[x-r x-r/2 x+r/2 x+r x+r/2 x-r/2 x-r];
y_hexagon=[y+0 y-sqrt(3)/2*r y-sqrt(3)/2*r y y+sqrt(3)/2*r y+sqrt(3)/2*r y];
hold on;
fill(x_hexagon,y_hexagon,col);
plot(x,y,'^');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [active_user,mspos] = users(rmax,rmin)
mspos=[];user=[];
for x = rmin:0.050:rmax
    for y =-rmax:0.050:rmax
        pos=[x y];
        mspos=[mspos; pos];
    end
end
active_user=[];
for i=1:size(mspos,1);
    user=mspos(i,:);
    if (user(2)<=(sqrt(3)/2)*rmax)
        if (user(2)<=((-sqrt(3)*user(1)+sqrt(3))*rmax))
            if (user(2)>=((sqrt(3)*user(1)-sqrt(3))*rmax))
                if (user(2)>=((-user(1)/sqrt(3))*rmax))
                active_user=[active_user;user];
                end
            end
        end
    end
   end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [pos_z] = generate_pos(d,dmin)

loop = 1;

while loop
    
    pos_x= rand*d;
    
    if pos_x < d/2
        pos_y =  rand*(1/2*tan(pi/3)*d - (- tan(pi/6)*pos_x)) + (- tan(pi/6)*pos_x);
    else
        if (pos_x < d*(cos(pi/6))^2)
            pos_y =  rand*(tan(pi/3)*d - tan(pi/3)*pos_x - (- tan(pi/6)*pos_x)) + (- tan(pi/6)*pos_x);
        else
            pos_y = 2*rand*(tan(pi/3)*d - tan(pi/3)*pos_x) - (tan(pi/3)*d- tan(pi/3)*pos_x) ;
        end
    end
    
    z_MS = pos_x + 1i*pos_y;
    r = abs(z_MS);
    
    loop = r < dmin;
end

pos_z = pos_x + 1i*pos_y;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function TotalSignalPower_ = calculate_interference_omniDirectional_1x1x1(PTx,r,z,variance_shadow_fading,x,y,gamma)


x_centers=[x-1.5*r x+1.5*r x-1.5*r x+1.5*r x x x+1.5*r x+3*r x+3*r x+3*r x+1.5*r x x-1.5*r x-1.5*r x-3*r x-3*r x-3*r x x+3*r x-3*r];
y_centers=[y+sqrt(3)*0.5*r y+sqrt(3)*0.5*r y-sqrt(3)*0.5*r y-sqrt(3)*0.5*r y+sqrt(3)*1*r y-sqrt(3)*1*r y+3*sqrt(3)*0.5*r y+sqrt(3)*r y y-sqrt(3)*r y-3*sqrt(3)*0.5*r y-2*sqrt(3)*r y-1.5*sqrt(3)*r y+1.5*sqrt(3)*r y+sqrt(3)*r y-sqrt(3)*r y y+2*sqrt(3)*r y+2*sqrt(3)*r y-2*sqrt(3)*r] ;

k=20;
alpha=8.43*10^(-5);% path loss
%gamma=-2;% pathloss exponent
TotalSignalPower=[];

for i=1:k
    pathLossDistance=sqrt(((real(z)-x_centers(i)).^2)+((imag(z)-y_centers(i)).^2));
    gain=1;
    pathLoss=alpha*pathLossDistance.^(gamma);
    lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
    signalPower=PTx*gain*shfd*pathLoss;
    TotalSignalPower=[TotalSignalPower;signalPower];
    
end

TotalSignalPower_=sum(TotalSignalPower);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function TotalSignalPower = calculate_interference_omniDirectional_1x3x1(PTx,r,z,variance_shadow_fading,x,y,gamma)

x_centers_reuse3=[x-3*r x-1.5*r x-1.5*r x+1.5*r x+1.5*r x+3*r];
y_centers_reuse3=[y y+1.5*sqrt(3)*r y-0.5*sqrt(3)*r y+1.5*sqrt(3)*r y-1.5*sqrt(3)*r y];
% k=size(x_centers_reuse3);
k=6;
alpha=8.43*10^(-5);% path loss
%gamma=-2;% pathloss exponent
TotalSignalPower=[];

for i=1:k
    pathLossDistance=sqrt(((real(z)-x_centers_reuse3(i)).^2)+((imag(z)-y_centers_reuse3(i)).^2));
    gain=1;
    pathLoss=alpha*pathLossDistance.^(gamma);
    lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
    signalPower=PTx*gain*shfd*pathLoss;
    TotalSignalPower=[TotalSignalPower;signalPower];
    
end
TotalSignalPower=sum(TotalSignalPower);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function degree = rad2deg(ang)
degree= ang*180/3.14;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function TotalSignalPower_= calculate_interference_omniDirectional_1x7x1(PTx,r,z,variance_shadow_fading,x,y,gamma)

x_centers_reuse7=[x+3*r x-3*r];
y_centers_reuse7=[y+2*sqrt(3)*r y-2*sqrt(3)*r];
% d=[2 5 8 14 17 20];
% k=size(x_centers_reuse7);
k=2;
alpha=8.43*10^(-5);% path loss
%gamma=-2;% pathloss exponent

TotalSignalPower=[];

for i=1:k

    pathLossDistance=sqrt(((real(z)-x_centers_reuse7(i)).^2)+((imag(z)-y_centers_reuse7(i)).^2));
    gain=1;
    pathLoss=alpha*pathLossDistance.^(gamma);
    lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
    signalPower=PTx*gain*shfd*pathLoss;
    TotalSignalPower=[TotalSignalPower;signalPower];
    
end
TotalSignalPower_=sum(TotalSignalPower);

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function totalsignalpower_inter = calculate_interference_Directional_1x3x1(PTx,r,z,variance_shadow_fading,x,y,gamma)

alpha=8.43*10^(-5);% path loss
%gamma=-2;% pathloss exponent

%1st interfering cell
rr1=sqrt(((real(z)-(-1.5*r)).^2)+((imag(z)-sqrt(3)*0.5*r).^2));
theta1_1=rad2deg(atan((sqrt(3)*0.5*r)-imag(z))/((-1.5*r)-real(z)));
theta1=30+abs(theta1_1);
g1=10^(1.7-0.1*min(12*theta1^2/70^2,20));
pl1=alpha*rr1.^(gamma);
 lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower1=PTx*g1*shfd*pl1;
%2nd interfering cell
rr2=sqrt(((real(z)-(1.5*r)).^2)+((imag(z)-sqrt(3)*0.5*r).^2));
theta2_2=rad2deg(atan((sqrt(3)*0.5*r)-imag(z))/((1.5*r)-real(z)));
theta2=(theta2_2-30)+180;
pl2=alpha*rr2.^(gamma);
g2=10^(1.7-0.1*min(12*theta2^2/70^2,20));
lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower2=PTx*g2*shfd*pl2;
%3rd interfering cell
rr3=sqrt(((real(z)-(-1.5*r)).^2)+((imag(z)-(-sqrt(3)*0.5*r)).^2));
theta3_3=rad2deg(atan((-sqrt(3)*0.5*r)-imag(z))/((-1.5*r)-real(z)));
theta3=(30-theta3_3);
pl3=alpha*rr3.^(gamma);
g3=10^(1.7-0.1*min(12*theta3^2/70^2,20));
lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower3=PTx*g3*shfd*pl3;
%4th interfering cell
rr4=sqrt(((real(z)-(1.5*r)).^2)+((imag(z)-(-sqrt(3)*0.5*r)).^2));
theta4_4=rad2deg(atan((-sqrt(3)*0.5*r)-imag(z))/((1.5*r)-real(z)));
theta4=180-(30+abs(theta4_4));
g4=10^(1.7-0.1*min(12*theta4^2/70^2,20));
pl4=alpha*rr4.^(gamma);
lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower4=PTx*g4*shfd*pl4;
%5th interfering cell
rr5=sqrt(((real(z)).^2)+((imag(z)-sqrt(3)*r).^2));
theta5_5=atand((imag(z)-sqrt(3)*r)/(real(z)-(0)));
theta5=30+abs(theta5_5);
g5=10^(1.7-0.1*min(12*theta5^2/70^2,20));
pl5=alpha*rr5.^(gamma);
lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower5=PTx*g5*shfd*pl5;
%6th interfering cell
rr6=sqrt(((real(z)).^2)+((imag(z)-(-sqrt(3)*r)).^2));
theta6_6=atand((imag(z))-(-sqrt(3)*r)/(real(z)-(0)));
theta6=abs(theta6_6)-30;
g6=10^(1.7-0.1*min(12*theta6^2/70^2,20));
pl6=alpha*rr6.^(gamma);
lognormal_dB = sqrt(variance_shadow_fading)*randn(1,1);
    shfd=10.^(lognormal_dB/10);
signalpower6=PTx*g6*shfd*pl6;   

signalpower_inter=[signalpower1 signalpower2 signalpower3 signalpower4 signalpower5 signalpower6];
totalsignalpower_inter=sum(signalpower_inter);

end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



