#include <stdio.h>
#include <math.h>

//to create table 32700*cos(atan({0..256}/256))
int coslimit[] = {
32700,
32700,
32698,
32697,
32694,
32692,
32688,
32685,
32681,
32676,
32671,
32665,
32659,
32652,
32645,
32637,
32629,
32621,
32611,
32602,
32592,
32581,
32570,
32558,
32546,
32533,
32520,
32510,
32496,
32481,
32466,
32451,
32435,
32419,
32402,
32385,
32367,
32348,
32329,
32310,
32290,
32270,
32256,
32235,
32213,
32191,
32169,
32146,
32122,
32098,
32073,
32048,
32023,
32000,
31982,
31955,
31928,
31900,
31872,
31843,
31814,
31784,
31754,
31739,
31708,
31677,
31645,
31613,
31580,
31547,
31513,
31488,
31465,
31430,
31395,
31359,
31322,
31286,
31248,
31232,
31197,
31159,
31120,
31081,
31041,
31001,
30976,
30949,
30908,
30866,
30824,
30781,
30738,
30720,
30685,
30640,
30596,
30551,
30505,
30464,
30452,
30405,
30358,
30311,
30263,
30214,
30208,
30161,
30112,
30062,
30011,
29960,
29952,
29908,
29856,
29804,
29751,
29698,
29696,
29646,
29593,
29538,
29483,
29440,
29433,
29377,
29321,
29265,
29208,
29184,
29159,
29101,
29043,
28984,
28928,
28928,
28878,
28818,
28757,
28696,
28672,
28652,
28590,
28528,
28466,
28416,
28416,
28360,
28297,
28233,
28168,
28160,
28129,
28064,
27998,
27932,
27904,
27896,
27829,
27762,
27695,
27648,
27648,
27594,
27526,
27457,
27392,
27392,
27358,
27288,
27218,
27147,
27136,
27121,
27050,
26979,
26906,
26880,
26880,
26813,
26740,
26666,
26624,
26624,
26576,
26501,
26427,
26368,
26368,
26340,
26264,
26188,
26112,
26112,
26105,
26028,
25951,
25873,
25856,
25856,
25794,
25716,
25637,
25600,
25600,
25562,
25483,
25403,
25344,
25344,
25333,
25253,
25172,
25090,
25088,
25088,
25026,
24944,
24861,
24832,
24832,
24802,
24720,
24636,
24576,
24576,
24576,
24499,
24415,
24330,
24320,
24320,
24283,
24198,
24112,
24064,
24064,
24064,
23986,
23899,
23812,
23808,
23808,
23779,
23692,
23604,
23552,
23552,
23552,
23490,
23402,
23313,
23296,
23296,
23295,
23206,
23170
	};
//to create table 32700*sin(atan({0..256}/256))

int sinlimit[] = {
0,
127,
254,
381,
508,
635,
762,
889,
1016,
1143,
1270,
1397,
1524,
1651,
1778,
1905,
2032,
2159,
2286,
2413,
2540,
2667,
2794,
2921,
3048,
3175,
3302,
3429,
3528,
3654,
3780,
3906,
4032,
4158,
4284,
4410,
4536,
4662,
4788,
4914,
5040,
5166,
5292,
5377,
5500,
5625,
5750,
5875,
6000,
6125,
6250,
6375,
6500,
6625,
6735,
6820,
6944,
7068,
7192,
7316,
7440,
7564,
7688,
7812,
7872,
7995,
8118,
8241,
8364,
8487,
8610,
8733,
8824,
8906,
9028,
9150,
9272,
9394,
9516,
9638,
9691,
9801,
9922,
10043,
10164,
10285,
10406,
10481,
10560,
10680,
10800,
10920,
11040,
11160,
11209,
11305,
11424,
11543,
11662,
11781,
11887,
11918,
12036,
12154,
12272,
12390,
12508,
12524,
12636,
12753,
12870,
12987,
13104,
13124,
13224,
13340,
13456,
13572,
13688,
13693,
13800,
13915,
14030,
14145,
14236,
14250,
14364,
14478,
14592,
14706,
14753,
14803,
14916,
15029,
15142,
15255,
15249,
15344,
15456,
15568,
15680,
15725,
15762,
15873,
15984,
16095,
16206,
16183,
16280,
16390,
16500,
16610,
16625,
16677,
16786,
16895,
17004,
17051,
17064,
17172,
17280,
17388,
17496,
17463,
17548,
17655,
17762,
17869,
17862,
17914,
18020,
18126,
18232,
18248,
18270,
18375,
18480,
18585,
18690,
18623,
18720,
18824,
18928,
19032,
18987,
19055,
19158,
19261,
19364,
19341,
19380,
19482,
19584,
19686,
19686,
19695,
19796,
19897,
19998,
20099,
20021,
20100,
20200,
20300,
20400,
20347,
20394,
20493,
20592,
20691,
20665,
20678,
20776,
20874,
20972,
21070,
20975,
21049,
21146,
21243,
21340,
21278,
21312,
21408,
21504,
21600,
21696,
21573,
21660,
21755,
21850,
21945,
21861,
21902,
21996,
22090,
22184,
22278,
22142,
22227,
22320,
22413,
22506,
22417,
22448,
22540,
22632,
22724,
22816,
22686,
22750,
22841,
22932,
23023,
22949,
22950,
23040
	};

int limiter( int *vd, int *vq)
{
 int vdt;
 int vqt;
 int quad=0;
 int i;
 int temp;
 int coslook;
 int sinlook;

 //figure out which quaderant and set temp variables to Quad 0

 vdt=*vd;
 vqt=*vq;

  if (vdt<0)
 {
     quad+=2;
     vdt=-vdt;
 }

 if (vqt<0)
 {
     quad+=1;
     vqt=-vqt;
 }

 if (vdt>vqt)
 {
    if(vdt<23122) return(0);
    i=vdt>>8;
    i=vqt/i;
    if (i>255)i=255;
    if ((vdt<coslimit[i+1])||(vqt<sinlimit[i])) return (0);  //magnituide is acceptablle
    switch (quad)
    {
        case 0:
            *vd=coslimit[i+1];  //to big limit
            *vq=sinlimit[i];
            return (1);
        case 1:
            *vd=coslimit[i+1];  //to big limit
            *vq=-sinlimit[i];
            return (1);
        case 2:
            *vd=-coslimit[i+1];  //to big limit
            *vq=sinlimit[i];
            return (1);
        case 3:
            *vd=-coslimit[i+1];  //to big limit
            *vq=-sinlimit[i];
            return (1);
    }
  }
  else
  {
    if(vqt<23122) return(0);
    i=vqt>>8;
    i=vdt/i;
    if (i>255)i=256;
    if ((vqt<coslimit[i+1])||(vdt<sinlimit[i])) return (0);  //magnituide is acceptablle
    switch (quad)
     {
        case 0:
            *vq=coslimit[i+1];  //to big limit
            *vd=sinlimit[i];
            return (1);
        case 1:
            *vq=-coslimit[i+1];  //to big limit
            *vd=sinlimit[i];
            return (1);
        case 2:
            *vq=coslimit[i+1];  //to big limit
            *vd=-sinlimit[i];
            return (1);
        case 3:
            *vq=-coslimit[i+1];  //to big limit
            *vd=-sinlimit[i];
            return (1);
     }
  }
 return (-1);
 }


int main()
{
 int x,y;
 int lx,ly;
 int active;
 double fx,fy;
 double mag,magl;
 double magmax;
 int x1,y1,i,int1,rem1,rem2,rat;
 int minx[1000],miny[1000];

 for(x=0;x<257;x++)
  {
   fx=x;
   fy=(fx+0.5)/256.0;
   fx=sqrt(pow(32700,2)/(1+pow(fy,2)));
   fy=sqrt(pow(32700,2)-pow(fx,2));
   y=fy;
//   printf("%d,\n\r",y);
  }
 for(x=0;x<257;x++)
  {
   fx=x;
   fy=(fx+0.5)/256.0;
   fx=sqrt(pow(32700,2)/(1+pow(fy,2)));
   fy=sqrt(pow(32700,2)-pow(fx,2));
   y=fy;
//   printf("%d,\n\r",y);
  }

for(i=0;i<1000;i++)
{ minx[i]=32767;
 miny[i]=32767;
}

magmax=0;
 printf("x,y,lx,ly,mag,magl\n\r");
// for(x=0;x<23167;x++)
//  for(y=0;y<23122;y++)

for(x=256;x<32767;x++)
  for(y=0;y<=x;y++)
    {

     fx=x;
     fy=y;
     fx=fy/fx*256.0;
     rat=fx;
     int1=x/256;
     rem2=x-int1*256;
     i=y/int1;
     rem1=y-i*int1;
     fy=i;

     fx=x;
     fy=y;
     mag=trunc(sqrt(pow(fx,2)+pow(fy,2)));
     lx=x;
     ly=y;
     active=limiter(&lx,&ly);
     fx=lx;
     fy=ly;
     magl=trunc(sqrt(pow(fx,2)+pow(fy,2)));
     if(magl>magmax) magmax=magl;
//     if (magl> mag) printf("%d,%d\n\r",x,y);

     if(mag>32699.0)
     {
      if(x<minx[i])minx[i]=x;
      if(y<miny[i])miny[i]=y;
     }
//     if((i==136)) printf("%d,%d\n\r",x,y);
// printf("%d,%d,%d,%d,%d,%d\n\r",x,y,i,rat,rem2,rem1);
//     if(magl>mag) printf("%d,%d,%d,%d,%.0f,%.0f\n\r",x,y,lx,ly,mag,magl);
//     if((magl)>32700) printf("%d,%d,%d,%d,%.0f,%.0f\n\r",x,y,lx,ly,mag,magl);

//     printf("%d,%d,%d,%d,%.0f,%.0f\n\r",x,y,lx,ly,mag,magl);
//     if(active>0) printf("%d,%d,%d,%d,%.0f,%.0f\n\r",x,y,lx,ly,mag,magl);
//     if((i==40)&&(x>=32280)) printf("%d,%d,%d,%d,%.0f,%.0f\n\r",x,y,lx,ly,mag,magl);
//     if (active<0) printf("Ouch %d %d\n\r",x,y);
    }

printf("%f\n\r",magmax);

for(i=0;i<258;i++)
 printf("%d,%d,%d\n\r",i,minx[i],miny[i]);
//for(i=0;i<257;i++)
// printf("%d,\n\r",miny[i]);

}
