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

#define TITLE    "M288, 250,000 orbits"
#define GIFOUT   "t27.gif"
#define NORB    -24
#define DELTANG 0.2
#define ORBWIDTH  9
#define ANG0      0
#define ANG1     90
#define ANG2    142
#define NELIP    32
#define RADIUS    0.44367

#include "tor00.hc"

int main () {
   double  t_ang ;
   int     e_cnt ;
   int     orb ;

   gdPoint ellipse0[NELIP+2];
   gdPoint ellipse1[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 ;

   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 = RADIUS          ;
         double tor = tos * cos( toa );
         double toy = tos * sin( toa );
         double tox = -(major+tor)*sin( angle_rad );
	 double toz = -(major+tor)*cos( angle_rad );
	 ellipse0[e_cnt] = sr0( tox, toy, toz );
      }
      gdImagePolygon( im, ellipse0, 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 = RADIUS          ;
         double tor = tos * cos( toa );
         double toy = tos * sin( toa );
         double tox = -(major+tor)*sin( -angle_rad );
         double toz = -(major+tor)*cos( -angle_rad );
         ellipse1[e_cnt] = sr0( tox, toy, toz );
      }
      gdImagePolygon( im, ellipse1, NELIP, ecolor );
   }

   gdImageFilledPolygon( im, ellipse0, NELIP, gray );
   gdImagePolygon( im, ellipse0, NELIP, lgray  );

   gdImageFilledPolygon( im, ellipse1, NELIP, gray );
   gdImagePolygon( im, ellipse1, NELIP, lgray  );

   drawearth();
   sprintf( bottom, "%5.0fkm spacing between orbits", 63.780*step/re );

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