// gd12.c
// really fake orbiting  server-sat
// This approximates a Kepler orbit with high eccentricity
// 
// Given the way I set up the palette, I would expect a white
// background, but it makes a black one.  Huh?
//
// compile with
// cc -o gd12 gd12.c -lgd -lpng -lz -ljpeg -lfreetype -lm
//
// Uses the libgd library.  For documentation see the file:
//   /usr/share/doc/gd-*/index.html  
//
// or the website:
//   http://www.libgd.org/Reference

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

#define  XSIZE       1024
#define  YSIZE       768
#define  SEMIMAJOR   430
#define  ECCENTRIC   0.7000
#define  CENTERX     200
#define  CENTERY     384

int main() {
   gdImagePtr im0, im1, im2;               /* Declare the image */
   FILE *gifout;                           /* Declare output file */
   int black, white, red, green, blue;     /* Declare color indexes */
   int  gray, trans ;
   double  pi2    = 8.0 * atan( 1.0 );
   double  delta  = pi2 / 200 ;
   double  delta2 = pi2 / 400 ;
   double  angle ;
   double  r     ;
   double  r00 = SEMIMAJOR * ( 1.0 - ECCENTRIC*ECCENTRIC ) ; 
   gdPoint points[1000] ;
   int     point ;

   double  object=0.0 ;
   int     ox, oy ;  

   im1 = gdImageCreate(XSIZE, YSIZE );

   /* Allocate the colors black, white, red, green, blue */
   white = gdImageColorAllocate(im1, 255, 255, 255);
   black = gdImageColorAllocate(im1,   0,   0,   0);
   red   = gdImageColorAllocate(im1, 255,   0,   0);
   green = gdImageColorAllocate(im1,   0, 255,   0);
   blue  = gdImageColorAllocate(im1,   0,   0, 255);
   gray  = gdImageColorAllocate(im1, 128, 128, 128);
   trans = gdImageColorAllocate(im1, 1, 1, 1);

   point = 0 ;
   /* compute points for ellipse with line segments */
   for( angle=0.0 ; angle <= pi2 ; ) {
      r  = r00/( 1.0 + ECCENTRIC * cos( angle ));
      points[point].x = CENTERX - (int) ( r * cos( angle ) );
      points[point].y = CENTERY + (int) ( r * sin( angle ) );
      angle += delta * SEMIMAJOR / r ;
      point++ ;
   }

   gifout = fopen( "test12.gif", "wb");

   // loop forever
   gdImageGifAnimBegin( im1, gifout, 1, 0 ) ;

   for( angle=0.0 ; angle <= pi2 ; ) {

      im2 = gdImageCreate( XSIZE, YSIZE ) ;
      gdImagePaletteCopy( im2, im1 );

      /* line thickness of 1 */
      gdImageSetThickness( im2, 1 );

      /* draw orbit */
      gdImagePolygon( im2, points, point, blue );

      ox = CENTERX ;
      oy = CENTERY ;
      /* earth */
      gdImageFilledArc( im2, ox, oy, 130, 130, 0, 360, green, gdArc );

      /* server_sat */
      r  = r00/( 1.0 + ECCENTRIC * cos( angle ));
      ox = CENTERX - (int) ( r * cos( angle ) );
      oy = CENTERY + (int) ( r * sin( angle ) );
 
      gdImageFilledArc(im2, ox    , oy,    60, 60, 0, 360, gray,  gdArc);
      gdImageFilledArc(im2, ox    , oy+40, 20, 20, 0, 360, white, gdArc);
      gdImageFilledArc(im2, ox-35 , oy-20, 20, 20, 0, 360, white, gdArc);
      gdImageFilledArc(im2, ox+35 , oy-20, 20, 20, 0, 360, white, gdArc);

      /* Output the image to the disk file in GIF format. */
      gdImageGifAnimAdd( im2, gifout, 0, 0, 2, 1,
                         gdDisposalRestoreBackground, im1 );

      angle += delta2 * pow( ( SEMIMAJOR / r ), 2) ;
      im0 = im1 ;
      im1 = im2 ;
      gdImageDestroy(im0);
   }
   gdImageGifAnimEnd(gifout);

   /* Close the files. */
   fclose(gifout);
   gdImageDestroy(im1);
}
