function results = k_count_plot(loc,sims,b,M,poly,info)

%K_COUNT_PLOT computes the raw K-function for a point population 
%in an areal lattice (with boundary file and reference measure given).
%The P-values under the 'Randomness Hypothesis' with respect to the reference 
%measure are both saved and plotted.

%Written by: TONY E. SMITH, 1/29/01 [Modified 2/04/08]

%Programs Called: k_count.m
%
% INPUTS:
%     (i)   loc  = population location file [loc(i)=(Xi, Yi),i=1..n]
%     (ii)  sims = number of random patterns generated
%     (iii) b    = number of ticks (bins) to use on the distance axis 
%     (iv)  M    = k-vector of measure values for each of k polygons
%     (v)   poly = (n:2) matrix describing boundaries of k polygons in the form:
%
%          [1   n1
%           x11 y11 
%           x12 y12
%           .
%           .
%           x11 y11
%           2   n2
%           x21 x22
%           .
%           .
%           .
%           k   nk
%           xk1 yk1
%           .
%           .
%           xk1 yk1]
%
%     (vi)  info  = (optional) structure of additional options
%
%             info.dist = maximum distance for k-function 
%                         [default = (max pairwise dist)/2 ]
%
%             info.seed = optional specification of random seed. 
%                
%                info.seed = 1 if random seed is to be used.
%                info.seed = 5_digit positive integer to be used as seed 
%                [Default seed = 23311]
%
% DATA OUTPUT:

%    results.Dist = Array of distance values for bins.
%                   [Here each bin, k = 1,.., b, is associated 
%                   with distance value, dist(k) = (k/b)*d.]

%    results.PVal = P-Value under null hypothesis at each distance in D


% SCREEN OUTPUT: (1) Plots PVal against D
%                (2) Also displays:
%                    Dmin = Minimum distance between points in the given
%                           point pattern.(This is useful for interpreting 
%                           the resulting plot.)
%                (3) Dmax = maximum distance used.


% Parse INFO structure if it exists.

dist_flag = 0;

seed_flag = 0;

if exist('info','var') % looks only among variable for 'info'
    
    fields = fieldnames(info);
    
    nf = length(fields);
    
    for i=1:nf
        
        if strcmp(fields{i},'dist') 
            
            d = info.dist;
            
            dist_flag = 1;
            
        elseif strcmp(fields{i},'seed')             
                       
            seed_flag = 1;         
       
        end;
        
   end;
   
end;

%Set Seed

if seed_flag == 0;
   
   seed = 23311;
   
elseif info.seed == 1
   
   seed = 10000*rand;
   
else
   
   seed = info.seed;
   
end

rand('seed',seed);

% Set Dmax

Dist = dist_vec(loc);

if dist_flag == 0;
    
    % Compute Pairwise Distances

    max_Dist = max(Dist);  % Compute Dmax

    d = max_Dist/2;
    
else
    
    d = info.dist;
    
end

% CALL K_COUNT

[PVal,D] = k_count(loc,sims,d,b,M,poly);

results.Dist = D;

results.PVal = PVal;

% COMPUTE Dmin

Dmin = min(Dist);

disp(' ');
disp(['Dmin = ',num2str(Dmin)]);
disp(' ');
disp(['Dmax = ',num2str(d)]);
disp(' ');

results.Dist = D;

results.PVal = PVal;


% PLOT RESULTS

plot(D,PVal,'k.','MarkerSize',20);

hold on;

plot(D,PVal,'k-','LineWidth',2);

hold on;

SIG = .05*ones(length(D),1);

plot(D,SIG,'r--');

plot(D,1-SIG,'r--');

xlabel('Radius','Fontsize',12, 'FontWeight', 'bold');

ylabel('clustered                    P-Value                   dispersed',...
   'Fontsize',12, 'FontWeight', 'bold');


