// gd10.c
//  orbiting  circle

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

#define  SEMIMAJOR   430
#define  ECCENTRIC   0.7000
#define  CENTERX     200
#define  CENTERY     384

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

   double  object=0.0 ;
   int     ox, oy ;  

   im = gdImageCreate(1024, 768 );

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

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

   point = 0 ;
   /* draw 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++ ;
   }
   gdImagePolygon( im, points, point, blue );

   ox = CENTERX ;
   oy = CENTERY ;
   gdImageFilledArc( im, ox, oy, 130, 130, 0, 360, green, gdArc );

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

   /* JPEG-format file. */
   gifout = fopen("test10.gif", "wb");

   /* Output the image to the disk file in GIF format. */
   gdImageGif(im, gifout);

   /* Close the files. */
   fclose(gifout);

   /* Destroy the image in memory. */
   gdImageDestroy(im);
}
