// t04.c
// v0.1 KHL 2009-04-21
// gcc -o t04 t04.c -lm -lgd ; ./t04
// static drawing, though slow to compute
// earth and toroid around orbit

#define TITLE    "M288, 1 center and  4 toroid orbits"
#define GIFOUT   "t04.gif"
#define NORB      4
#define DELTANG 0.2
#define ORBWIDTH  9
#define ANG0      0
#define ANG1     90
#define ANG2    142
#define NELIP    32

#include "tor00.hc"

int main () {
   double  t_ang ;
   int     e_cnt ;
   int     orb ;
   int     norb ;
 
   gdPoint ellipse[NELIP+2];

   displaystart();
   torstart();

   gdImageGifAnimBegin( im1, gifout, 1, -1 ) ; // no repeat
   // gdImageGifAnimBegin( im1, gifout, 1,  4 ) ; // repeat 4 times
   // gdImageGifAnimBegin( im1, gifout, 1,  0 ) ; // continuous repeat

   framestart(    90, TITLE );

   // draw back portion of the orbit toroid
  
   double acolor =  0.0 ;
   if(  NORB > 0 ) { norb = 1 + NORB ; }
   else            { norb = 1 - NORB ; }

   for( t_ang=ANG0 ; t_ang < ANG2 ; t_ang += DELTANG ) {

      double ecolor ;
      if( acolor < 0.5 ) { ecolor = gray ; }
      else {
         ecolor = lgray ;
         if( acolor > 14.5 ) { acolor -= 15.0 ; }
      }
      acolor += DELTANG ;

      double angle_rad=DEG2RAD( t_ang );

      // ellipse disk - since it is at an angle, we will have to draw it
      // as a filled polygon
    
      // ellipse disk on left
      for( e_cnt=0 ; e_cnt < NELIP ; e_cnt++ ) {
         double toa = angle_rad + (2.0*PI*e_cnt)/NELIP  ;
         double tos = td[ norb-1 ] * step ;                 // step size
         double tor = tos * cos( toa );
         double toy = tos * sin( toa );
         double tox = -(major+tor)*sin( angle_rad );
	 double toz = -(major+tor)*cos( angle_rad );
	 ellipse[e_cnt] = sr0( tox, toy, toz );
      }
      if( ( NORB < 0 ) && ( t_ang > ( ANG2-DELTANG ) ) ) {
         gdImageFilledPolygon( im, ellipse, NELIP, black );
      }
      gdImagePolygon( im, ellipse, NELIP, ecolor );


      // ellipse disk on right
      for( e_cnt=0 ; e_cnt < NELIP ; e_cnt++ ) {
         double toa = -angle_rad + (2.0*PI*e_cnt)/NELIP  ;
         double tos = td[ norb-1 ] * step ;                 // step size
         double tor = tos * cos( toa );
         double toy = tos * sin( toa );
         double tox = -(major+tor)*sin( -angle_rad );
         double toz = -(major+tor)*cos( -angle_rad );
         ellipse[e_cnt] = sr0( tox, toy, toz );
      }
      if( ( NORB < 0 ) && ( t_ang > ( ANG2-DELTANG ) ) ) {
         gdImageFilledPolygon( im, ellipse, NELIP, black );
      }
      gdImagePolygon( im, ellipse, NELIP, ecolor );


      for( orb=0 ; orb < norb ; orb++ ) {
         // orbit spot on left
         int    tcolor = tc[orb]; 
         double toa = angle_rad +  DEG2RAD( ta[orb] ) ;
         double tos = step * td[ orb ] ;                 // step size
         double tor = tos * cos( toa );
         double toy = tos * sin( toa );
         double tox = -(major+tor)*sin( angle_rad );
         double toz = -(major+tor)*cos( angle_rad );
         gdPoint pto = sr0( tox, toy, toz );
         gdImageFilledEllipse( im, pto.x, pto.y, ORBWIDTH, ORBWIDTH, tcolor);
         
         // orbit spot on right
         toa = -angle_rad +  DEG2RAD( ta[orb] ) ;
         tos = step * td[ orb ] ;                 // step size
         tor = tos * cos( toa );
         toy = tos * sin( toa );
         tox = -(major+tor)*sin( -angle_rad );
         toz = -(major+tor)*cos( -angle_rad );
         pto = sr0( tox, toy, toz );
         gdImageFilledEllipse( im, pto.x, pto.y, ORBWIDTH, ORBWIDTH, tcolor);
      }  
   }      
   drawearth();

   if( NORB > 0 ) {
      // draw the rest of the center orbit
      for( t_ang=ANG2 ; t_ang < 360.0-ANG2 ; t_ang += DELTANG ) {
         double  angle_rad=DEG2RAD( t_ang );
         double  tox = -major*sin( angle_rad );
         double  toz = -major*cos( angle_rad );
         gdPoint pto = sr0( tox, 0.0, toz );
         gdImageFilledEllipse( im, pto.x, pto.y, ORBWIDTH, ORBWIDTH, tc[0]);
      }
   }
   sprintf( bottom, "%5.0fkm spacing between orbits", 6378.0*step/re );

   frameend();
   displayend();
   return 0;
}
