close all
clear all

%int minx[1000],miny[1000];
%for(i=0;i<1000;i++)    //fill min arrays with maximum value
%{ minx[i]=32767;
% miny[i]=32767;
%}
minx = 32767*ones(1000,1);
miny = 32767*ones(1000,1);


% 
% //for(x=0;x<32767;x++)  //cycle through all point in first octant
% //  for(y=0;y<=x;y++)
% //    {
% //     fx=x;
% //     fy=y;
% //     mag=trunc(sqrt(pow(fx,2)+pow(fy,2))); //calculate the magnitude in float
% //     if(mag>32699.0) //if it is bigger than 32700
% //     {     //find the smalles value
% //      if(x<minx[i])minx[i]=x; //this becomes coslimit
% //      if(y<miny[i])miny[i]=y; //and this sinlimit
% //     }
% //    }

Limit = 32000;
LimitAt45 = floor(Limit/sqrt(2));
disp(['Limit = ' num2str(Limit)]);
disp(['LimitAt45 = ' num2str(LimitAt45)]);


for x = LimitAt45-20:32767
    disp(num2str(x));
    for y = 0:x  
    i=bitshift(x,-8);
    i = floor(y/i);
    if(i>255) 
        i = 256;
    end
    
      mag = floor((sqrt(x^2+y^2)));
      if(mag > Limit)
         if(x < minx(i+1))
             minx(i+1) = x;
         end
      if(y < miny(i+1))
             miny(i+1) = y;
      end
         
      
      end
    end
end
   
fid = fopen('LimiterLookUpTables.h','w');

fprintf(fid,'int coslimit[] = { \n');

for i = 1:257
    fprintf(fid,'%d,\n',minx(i));
end
        fprintf(fid,'%d};\n\n\n',minx(258));

        
fprintf(fid,'int sinlimit[] = { \n');

        
 for i = 1:257
    fprintf(fid,'%d,\n',miny(i));
end
    fprintf(fid,'%d};\n\n\n',miny(258));
        
   
fprintf(fid,'int LimitAt45 = %d;\n',LimitAt45);
        
fclose(fid);

        
      